ASP.NET Charting: Rendering Options

With the release of .NET 3.5 SP1, MS has released a sub-set of the Dundas charting component. They also put together a neat little solution that’ll let you run a number of examples/samples locally so you can get your hands dirty. If you’re looking to do any work with the charting components, or just play around getting these samples should definitely be your first step as the basics will be self-explanatory. The link for that is here.

Now that we got the boring introductory stuff out of the way; the .NET charting component gives you a few different choices when it comes to rendering your chart/image.

Temp directory/ImageTag/ImageUrl Method

This is what I’d consider the simple/out-of-the-box method. Basically, when you create your chart either in the designer or in a code-behind you can specify where the chart should live. This means that ASP.NET will create a chart based on some data that you provide, dump it in a temporary directory, and then you’ll refer to it just as you would any other image.

In other words, your designer markup may look like this:

<asp:chart id="Chart1" 
  runat="server" 
  Height="296px" 
  Width="412px" 
  ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)" 
  Palette="BrightPastel" 
  BorderDashStyle="Solid" 
  BackGradientStyle="TopBottom" 
  BorderWidth="2" 
  backcolor="#D3DFF0" 
  BorderColor="26, 59, 105"
>

And, at runtime, the server will spit out something like the following:

<img id="Chart1" 
  BorderDashStyle="Solid" 
  src="/WebSamples/ChartImg.axd?i=chart_00090164d2f6498095a78f42f0b28216_1.png&amp;g=772eca8a160b4cb291bfe41a00e762a7" 
  alt="" 
  style="height:296px;width:412px;border-width:0px;" 
/>

Look familiar? That’s roughly what an ASP:Image control would render to had you dropped one of those onto the page. The only difference is that your SRC tag points to a HttpHandler that you specified in your web.config.

In any case, using this method is very straight forward and your client’s browsers will cache your charts just as they would any other image. Of course, the image file that gets cached will look like this:

ChartImg.axd?i=chart_00090164d2f6498095a78f42f0b28216_0.png&g=ec1fd46ebf42485894656b42606e5b27

…The MSDN documentation states that for static data, this caching improves performance. However, in my experimenting even with static data a refresh of the page causes the querystring parameters to change…which means you will need to pull it back down from the server, which also means the browser cached it for nothing. (Although this is what my experimenting indicated, I’m wondering if I missed something. Information to the contrary is welcome…anyone…?...crickets?)

Binary Streaming Method

Now, if you looked at the MS Chart Samples solution, you’ll notice that I’m skipping a couple of “rendering methods” here. That’s largely because I’m thinking if you’re still reading this, you already know you can render a chart in an iFrame, and because their guidance on rendering charts within legacy websites is essentially “use Binary Streaming”.

Binary Streaming is implemented by doing either one of the following

Keep in mind an asp:Image control is getting translated to an IMG tag anyway. In both cases, SomeChart.aspx contains the designer construct for a chart. Something like the following [which is snipped]:

<img src="SomeChart.aspx" width="412" height="296" alt=""/>

Or…

<asp:Image id="Image1" runat="server" ImageLocation="SomeChart.aspx" 
/>

Keep in mind an asp:Image control is getting translated to an IMG tag anyway. In both cases, SomeChart.aspx contains the designer construct for a chart. Something like the following [which is snipped]:

<%@ Page Language="c#" Inherits="Samples.SomeChart" CodeFile="SomeChart.aspx.cs" %>
<asp:chart id="Chart1" runat="server" height="296px" width="412px" 
    imagetype="Png" palette="BrightPastel" backcolor="#F3DFC1" rendertype="BinaryStreaming"
    borderdashstyle="Solid" backgradientstyle="TopBottom" borderwidth="2" bordercolor="181, 64, 1">
    <series>
        <asp:Series ChartTypeName="SplineArea" Name="Series1" BorderColor="64, 64, 64" ShadowOffset="2">
            <points>
                <asp:DataPoint YValues="6" />

With this method, you’re basically telling the chart that you’d like it to return a Binary Stream (surprise). In essence, the content of the server’s response will be the string representation of the binary object which your browser will render as an image. In a sense, it’s similar to making a request directly to ChartImg.axd directly. The difference here is you’re creating the binary representation of the chart from memory rather than a physical location on disk.

Both of these methods are very easy to set-up and require little to no-thought. I have to admit using a temporary directory feels a little icky but that may just be my OCD kicking in.

There’s another way, though.

If for some reason these two methods don’t jive with you, though, there is another way. It’s sort of hacky – but it works. Basically, you can convert a binary stream to Base64 and return the string directly to the image tag.

<img src='data:image/png;base64, iVBORw0KGgoAAAA[snip]; />
That means that you can generate your chart entirely in a codebehind and then just return the string via an AJAX request.
//Generate your chart
System.Web.UI.DataVisualization.Charting.Chart Chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
//Save it to a stream and return it
System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
Chart1.SaveImage(imageStream, ChartImageFormat.Png);
return Convert.ToBase64String(imageStream.ToArray());

All in all the, binary streaming or the Temp Directory method for chart rendering will probably satisfy your requirements 80% of the time.  It would be interesting to see how each of these scale/perform in a heavily loaded environment.  Hopefully, this provided you with a sufficient high-level overview of your chart rendering options. 

Share this post :

Print | posted @ Thursday, June 18, 2009 2:45 PM

Comments on this entry:

Gravatar # re: ASP.NET Charting: Rendering Options
by Derek Williams at 6/19/2009 5:17 PM

Have you taken a look at the flot javascript library. It's very easy to use and works well with json or xml web services. Since thd data is held client side there are a whole set of interactive options available.
Gravatar # re: ASP.NET Charting: Rendering Options
by SanjayU at 6/19/2009 5:20 PM

@Derek,
Very cool, I haven't seen this yet. I'll definitely play around with it a bit!
Gravatar # re: ASP.NET Charting: Rendering Options
by NTulip at 6/25/2009 3:51 PM

nice - 8 years later we get dundas for free meanwhile everyone has gone to amcharts,flot or other implementations. Quite honestly its using them in winforms that i care about. Images as charts in web apps is out dated.
Gravatar # re: ASP.NET Charting: Rendering Options
by sakthi at 7/1/2009 5:47 AM

Hi....This is sakthi from india working for macrotesting www.macrotesting.com This post is Very cool, I haven't seen this yet. Excellent post that explains the Rendering Options so clearly and its really nice post. Thank you...


Cheers
sakthi
Gravatar # re: ASP.NET Charting: Rendering Options
by fort worth car accident lawyer at 12/28/2009 1:14 PM

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
Gravatar # re: ASP.NET Charting: Rendering Options
by California Mesothelioma Lawyer at 12/28/2009 1:14 PM

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained!
Gravatar # re: ASP.NET Charting: Rendering Options
by three ring binder at 12/28/2009 1:15 PM

Never knew that. Thanks for the info. I think your blog is going to do well. People will always return to read this kind of content. Great job.
Gravatar # re: ASP.NET Charting: Rendering Options
by treatment of mesothelioma at 1/9/2010 2:50 PM

I agree with your conclusions and will eagerly look forward to your next updates.
Gravatar # re: ASP.NET Charting: Rendering Options
by Rapid Share at 1/12/2010 2:00 AM

Can anybody help me with this. I have a problem in rendering asp.net chart page on the 4.6 blackkerry browser.
My asp.net page has chart control. I have tried rendering it as bmp. png, jpeg .... it renders properly on my simulator ... all the simulators ... but does not render on my device Browser ...
Can somebody help me with this?
Gravatar # re: ASP.NET Charting: Rendering Options
by Love poetry at 1/17/2010 9:35 AM

I haven't seen this yet. I'll definitely play around with it a bit!
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 
Twitter