SIGN UP MEMBER LOGIN:    
ARTICLE

Creating Exploded Pie Chart Having Click Through Functionality in C#

Posted by Jigar Desai Articles | GDI+ & Graphics December 26, 2001
In this article I would like to show you code that would create exploded pie chart and implementing click through functionality to that chart.
Reader Level:
Download Files:
 

In my last article Creating a Pie Chart on Fly with VB.NET, I  showed How to create dynamic pie chart using GdI+ capabilities,  to extend that further I would like to show you code that would create exploded pie chart and implementing click through functionality to that chart.

Run sample code

Pie Chart component is made up of four classes and one enum.

  • pieChartData (class)
  • PieDrawData (class)
  • pieChart (class)
  • colordata (class)
  • imageFormat (enum)

All of the above classes are under namespace UtChart. 

pieChartData.  As name suggests this class will keep all data  required to define chart. Our first requirement to draw chart is array of chart values and legends if click through is required then array of links is required and if exploded chart is required then we will require array of bool type to determine to expload or not. Other then that we also have margins on top, bottom, left and right, piedia for specifying diameater of pie, array colorval for specifying colors of pie, imageformat to specify image formate( gif, jpeg, bmp), ExploadOffset which is Distance as Percentage of pieDia from center for Exploaded pie, chartFont is Font Used in Chart to Draw Legends, pieratio is Percentage of Pie Height to Pie Width and pie3dRatio  is Percentage of Pie Thickness to Piedia. 

As we also want to have click through facility we will require all calculation for drawing to be done separately, and then from that data we can draw chart and also create image map( For click through facility).  Method getDrawdata does same thing once instance of pieChartData is created getDrawdata can provide data for drawing chart. 

Another method getimageMap gives html image map using pieDrawdata object returned from getDrawData method. 

PieDrawData.  This class describes data which is required to draw chart and to get Image map.  Object contains following properties, 'imageWidth' Chart's Image Width in pixels, 'imageHeight' Chart's Image Height in pixels, 'legendWidth' Maximum Width of Legend for Given Font, 'valWidth' Maximum Width of Chart Values for Given Font, 'PercentWidth' Maximum Width of Percentage for Given Font, 'pieRect' Array of Rectangle object for each correspoiding values, 'piewidth' Actual space taken by pie after adding values for exploaded pies, 'fontHeight' Height of font used for Writing on chart and  'LegandMap' Array of Rectangle object which describes actual position of legends on Image ( used for ImageMaping ). 

pieChart.  This class draws image by using graphics, it has only one method getPieChart which takes refrence of pieChartData and returns image as stream.  

colordata. This class contains Array 31 colors, if there are more then 31 elements in charts then Piechartdatas constructor will take care to start again from first after 31st color. Also user can specify there own colors through colorval property of pieChartdata object.  

imageFormat (enum).  Contains three members.

  • 'Gif' Corresponds to "System.Drawing.Imaging.ImageFormat.Gif" , 
  • 'Jpeg' Corresponds to "System.Drawing.Imaging.ImageFormat.Jpeg" and 
  • 'Bmp' Corresponds to "System.Drawing.Imaging.ImageFormat.Bmp"

How to use this component: 

First Compile files pieChart.cs, pieChartData.cs, pieDrawData.cs and chartCommon.cs under namespace Utchart.

Copy UtChart.dll to your bin directory of web server and copy PieCharServer.aspx and pieChartSample.aspx to you rootdirectory.  

We will require two aspx files to present chart. 

pieChartServer.aspx this page will generate Dynamic image using utChart assembly. This page will create pieChartData object first from query string data, and then pass reference of this object to method getPieChart of pieChart object to get stream which contains image. And finally save stream as Response.OutputStream. 

pieChartSample.aspx this is a sample page which shows how to use piechart component, again in this page we will create pieChartData object from data passed through web form then we will use getImageMap method of pieChartData to get Image map. To display pie image on this page we will call pieChartServer.aspx

( <img src='pieChartServer.aspx?Legends=(yahoo.com, rangat.com, google.com)&Vals=(15,18,25)&Expload=(False,False,True)>) 

Summary

Although I have shown only web page sample however you can also use same component for window application, once again I havent done error handling, other thing which comes in my mind is using images instead of colors in pie which you can try.

Login to add your contents and source code to this article
share this article :
post comment
 

Can we implement the same in a Windows Application?

Posted by kamesh.bora Sep 18, 2008

third class

Posted by priya sain Feb 04, 2008

HOw to generate for pie chart with spaces in area

Posted by bhanupriya v Oct 09, 2007

Great post. The image map related code was particularly useful for me. However, there is a problem if pie ratio is not 100 (say 70). If the image size is increased you will see that the image maps overlap adjacent pie sectors. For example, in your sample itself khel.com's link extends into the adjacent region of amazon.com. Things are fine when ratio is 100. Any idea how to solve this?

Posted by arun pisharody Jun 20, 2007

Hi, Really nice work. The only thing is how can I change the chart positioning on the page

Posted by Keyur Raval Jun 18, 2007
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor