Microsoft Chart Controls for the .Net Framework – rendering using a httpHandler

Those of you who have taken a look at the new chart controls might have seen that there are multiple ways of rendering these in ASP.NET.

Since my apps (ajax based, no updatepanels etc) use generic httpHandlers for these kind of things, I went looking for the ‘httphandler way’ of rendering the chart controls – but it does not exist..

So I whipped out reflector and took a look at the chart class, and found the three lines needed for doing this


Imports System.Web
Imports System.Web.Services
Imports System.Web.UI.DataVisualization.Charting
Imports System.IO

Public Class chart
Implements System.Web.IHttpHandler

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
'create the chart
Dim ch As New System.Web.UI.DataVisualization.Charting.Chart
ch.ID = "chart"
ch.Height = 296
ch.Width = 412
ch.ImageType = DataVisualization.Charting.ChartImageType.Png
ch.Palette = DataVisualization.Charting.ChartColorPalette.BrightPastel
ch.BackColor = Drawing.Color.Azure
ch.RenderType = DataVisualization.Charting.RenderType.BinaryStreaming
ch.BorderlineDashStyle = DataVisualization.Charting.ChartDashStyle.Solid
ch.BackGradientStyle = DataVisualization.Charting.GradientStyle.TopBottom
ch.BorderlineWidth = 2
ch.BorderlineColor = Drawing.Color.Blue
ch.BorderSkin.SkinStyle = BorderSkinStyle.Emboss

Dim ser As System.Web.UI.DataVisualization.Charting.Series = ch.Series.Add("Series1")
ser.ChartType = DataVisualization.Charting.SeriesChartType.SplineArea
ser.BorderColor = Drawing.Color.CadetBlue
ser.ShadowOffset = 2

ser.Points.Add(New DataPoint() With {.YValues = New Double() {6}})
ser.Points.Add(New DataPoint() With {.YValues = New Double() {9}})
ser.Points.Add(New DataPoint() With {.YValues = New Double() {3}})
ser.Points.Add(New DataPoint() With {.YValues = New Double() {5}})
ser.Points.Add(New DataPoint() With {.YValues = New Double() {2}})
ser.Points.Add(New DataPoint() With {.YValues = New Double() {7}})
ser.Points.Add(New DataPoint() With {.YValues = New Double() {8}})
ser.Points.Add(New DataPoint() With {.YValues = New Double() {1}})

Dim ca = ch.ChartAreas.Add("Default")
ca.BackColor = Drawing.Color.Transparent
ca.BorderColor = Drawing.Color.Red
ca.AxisX.IsMarginVisible = False

context.Response.ContentType = "image/png"
context.Response.Charset = ""

'here comes the magic
Dim imageStream As New MemoryStream
ch.SaveImage(imageStream)
context.Response.BinaryWrite(imageStream.GetBuffer)

End Sub

ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

End Class

Tags: ,

This entry was posted on Thursday, November 13th, 2008 at 16:51 and is filed under programming, Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

  • And in case anyone is wondering, the reason you have to write to an interim stream (rather than writing directly to the response), is that png rendering of BitMap requires a stream that supports seeking (and the response stream does not).

  • And in case anyone is wondering, the reason you have to write to an interim stream (rather than writing directly to the response), is that png rendering of BitMap requires a stream that supports seeking (and the response stream does not).

  • Jessica Hamilton

    Thanks for your code!

    It made it possible to use in my MVC project without any ASP server controls in my view, nor really need a custom HTTP Handler.

    So happy! 😀

  • Jessica Hamilton

    Thanks for your code!

    It made it possible to use in my MVC project without any ASP server controls in my view, nor really need a custom HTTP Handler.

    So happy! 😀

  • Leszek

    I was struggling with another problem with generating charts and this solves seems to solve it in no time. This is something i’ve been looking for all week. Thanks mate, you made my day!

  • Leszek

    I was struggling with another problem with generating charts and this solves seems to solve it in no time. This is something i’ve been looking for all week. Thanks mate, you made my day!

  • Pingback: ASP.NET MVC Archived Buzz, Page 1()