views:

105

answers:

2

My web service needs to do generate some line charts which will then be added to a PDF report that is streamed to the client.

How do I generate the line chart in the web service?

Thanks!

+1  A: 

If you don't want to use a third party control then you can use the System.Drawing namespace for this.

  1. Create a bitmap of the correct size. It is best to use 32bppargb because that is what GDI+ uses internally.
  2. Get a graphics object from the bitmap using Graphics.FromBitmap
  3. Draw on the graphics object using graphics.DrawLine etc (all the methods are documented on the graphics object)
  4. Create a MemoryStream and save the bitmap to the MemoryStream which you can then use in your PDF writing software, or simply save the bitmap to disk (you will need to give ASP.NET permissions to do this)

Dont forget to dispose the graphics object as soon as you have finished with it (a using block is best)

Because you are using GDI+ from within a web service, you may want to consider using the Singleton pattern to serialise requests to do the drawing.

James Westgate
A: 

Have a look at this webpage here

Drawing Line Charts in ASP.Net

Then instead of requesting the chart from your page_load and returning it in the stream, return it from one of your webservice functions

Edit

Went away and thought about this and then a lightbulb went off - another way of doing this (and also removing you from the GDI+ code) would be to use the GoogleCharts api.

For example http://chart.apis.google.com/chart?cht=p3&chd=t:90,10&chs=250x100&chl=Overflow|Stack renders the following

alt text

Line charts

http://code.google.com/apis/chart/docs/gallery/line_charts.html

Also, if you haven't sorted PDF generation yet, have a look at iTextSharp here

CResults
That example is dangerous because the graphics object is disposed in the destructor. It should be disposed as soon as possible.
James Westgate
Good spot. Didn't see that.
CResults