Reader Level:
ARTICLE

Creating a Pie Chart on Fly with VB.NET

Posted by Jigar Desai Articles | Visual Basic .NET November 10, 2012
Dot net framework provides strong support for creating graphics with GDI+. Developing graphics application was never that much easier in past.
  • 0
  • 0
  • 8850
Download Files:
 

Dot net framework provides strong support for creating graphics with GDI+. Developing graphics application was never that much easier in past.

You must have tried to create bar chart on your web site with dirty html or pie chart with third party components. In this article I am going to demonstrate GDI+ capabilities by creating component which can create a pie chart on fly for your web pages or for your windows application.

Create-Pie-Chart-in-vb.net.jpg

Pie Chart component is made up of two classes chartData class which encapsulates and  creates various data for creating pie chart and utChart class which contain method which returns stream object. Both of these classes are under namespace utCharting.

ChartData

Data required for creating chart is array of names and Vals which has to be distributed through pie. In new method of this class we take inputs as array of names and values , and using those values we have generated properties which describe percent values for each elements, similarly we determined span of pie for each elements by multiplying fraction of each element with 360 ( circle has 360 degree) and we obtained start angle for each elements by adding up span in loop.

UtChart

This class has one method(getpiechart) which returns stream object which contains pie chart Image in gif formate, this method require inputs as chartdata object and integer object for diameter of pie chart.

We used fillpie method of graphics object to generate pie for all elements, most of the code is for determining size of string to be printed on image and determining size of image, to be created in memory. 

Attached: utCharting.vb , utCharting.dll.

How to use this component

Sample shows aspx page which utilizes charting component, for that copy utcharting.dll to your bin directory of web server and copy createPieChar.aspx to you rootdirectory.

Our aspx page does following things:

  • Collects names, values and pie size data from query string or form.
  • Converts names in to string array.
  • Converts values into single array.

  • Converts piesize into integer. 
  1. Creates instance of chartdata using names and values array.
  2. Creates instance of utChart.
  3. Gets stream object from getPiechart method of utChart.
  4. Sets contenttype of page to "image.gif".
  5. Passes on stream object to browser

Attached: createPieChart.dll.

Summary

Although this sample is elementary, you can extend functionality by changing utchart class, one of things I missed is error handling and ways to make code work faster, you can also add barchart creation to this component. Or make 3D pie chart or cut out chart which shows chosen pie out of circle.

COMMENT USING

Trending up