This week Chuck answers a Builder Australia reader's question on displaying graphs of database data on web sites using VS.NET.

Dear Chuck:
I was doing some development with VS .NET, and we couldn't work out how to display nice graphs of database data on websites.  Do you have any pointers of how to do this?  Any help would be greatly appreciated.

The drawing library with .NET is very good, but if we were to develop our own graphing tool, that's like reinventing the wheel!  We were thinking of using Office components (esp. Excel) so that we do some complex graphing. Does Excel have a specific component that can be registered on the server or do we have to install Office on the server?  We're also thinking of performance issues with using an Excel component, because to pass data to the graph, do you have to instantiate an excel worksheet everytime?

Chuck:
As you note in your question there are a several of ways displaying graphs on web pages so will I look at a couple of different strategies of providing this functionality and the pro's and cons of each. My first recommendation for displaying charts in Web pages is to use the Crystal Reports functionality that comes with Visual Studio.NET.  They have been doing reports a long time and really understand how to write report generators that scale to the largest websites -including charting this data.  The complaint with crystal reports web components with their earlier versions was the fact the reports were displayed using an Active X control, this is no longer the case.   I did write a simple sample to illustrate how to do this(took me less than 5 minutes), but realised my good friends at Crystal would probably do a better job at explaining the process and have asked them to supply content for next week. In this same category of solution be sure and check the many other control vendors that provide similar function at www.gotdotnet.com

My next recommendation, as a developer that loves any excuse to use coolest and latest technologies, would be to use GDI+ class libraries and render it myself; you mentioned this would be like reinventing the wheel.  What a lot of people don't realise how easy the GDI+ class libraries make doing this kind of work.  In 8 lines of code I wrote a simple function that takes four numbers and displays them in a graph. 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Call Graphit(Convert.ToInt16(TextBox1.Text), Convert.ToInt16(TextBox2.Text), Convert.ToInt16(TextBox3.Text), Convert.ToInt16(TextBox4.Text))

End Sub

 

Function Graphit(ByVal one As Integer, ByVal two As Integer, ByVal three As Integer, ByVal four As Integer)

Dim bmp As New Bitmap(200, 220)

        Dim g As Graphics = Graphics.FromImage(bmp)

        'Dim g As Graphics = Me.CreateGraphics

        g.DrawLine(New Pen(Color.Black), 10, 10, 10, 200)

'Draw the Y axis line

        g.DrawLine(New Pen(Color.Black), 10, 200, 200, 200)

'Draw the X axis line

        g.FillRectangle(New SolidBrush(Color.Red), 20, one, 20, 200 - one)

'Draw the first data series

        g.FillRectangle(New SolidBrush(Color.Blue), 60, two, 20, 200 - two)

'Draw the seond data series

        g.FillRectangle(New SolidBrush(Color.Green), 100, three, 20, 200 - three)

'Draw the third data series

        g.FillRectangle(New SolidBrush(Color.Yellow), 140, four, 20, 200 - four)

'Draw the fourth data series

        bmp.Save("c:\inetpub\wwwroot\zdchart\test.bmp")

'persist the image to disk

Image1.ImageUrl = "test.bmp"

Display the image

End Function

 

Of course this isn't really a useful sample as it relies on numbers to be less than 200 and won't scale well to a multi user scenario due to collisions from writing the image to a single file with a hard-coded name.  This brings me to my next suggestion which is to use the Reports Starter Kit for ASP.NET offered on the www.asp.net web site.  ASP.NET Starter kits are sample ASP.NET applications that provide full source code to accomplish common Web development tasks. Each sample is complete and well-documented so that you can use the code to kickstart your ASP.NET development projects today.  There are 5 Starter kits available today:  Community, Commerce, Portal, Time Tracking and the one you are interested in Reports.  The Reports Starter Kit demonstrates a simple data reporting solution for displaying multiple views on data, creating charts, and rendering any type of data in a Web application.  This starter kit still gives you all the richness of GDI+ but saves you the pain of writing it yourself!

You also mentioned using Microsoft Office to display these graphs. Microsoft office does ship a control called the -Chart Control" with Internet Explorer 5.0+ and with the Office Web Components that does an excellent job of supplying this functionality. Your concern about scalability shouldn't be an issue as the processing and rendering of the graphics is done on the client side with an ActiveX control, of course this does require a client that supports ActiveX controls. For more information about the chart control and a set of directions on its use please see: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/html/deovrunderstandingchartcontrol.asp

If you are a developer who is using .NET, considering .NET or is sceptical of using .NET, we ask you to submit your question to Charles Sterling, who has over 10 years of Developer tools experience at Microsoft and is currently the .NET Developer evangelist at Microsoft Australia.

Each week we will publish your questions along with Chuck's answers. All questions should be submitted to builder@zdnet.com.au

Do you need help with Windows? Gain advice from Builder AU forums

Related links

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

Log in


Sign up | Forgot your password?

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!