How to create a Pie chart using ASP.Net and C#

Since .Net 4.0, we no longer need to download and install the Chart Control to get charting capabilities in ASP.Net, Window Forms or WPF. This is becuase the the Charting control is now part of .Net 4.0 itself.

Although all you need to know about Charting in ASP.Net can be found here I thought I would write this and provide a trimmed down version of a simple Pie Chart. The downloadable code from here will render this Pie Chart:

image

There are 4 things you need to do in order to get this configured and working on your machine, remember, however that you will need Visual Studio 2010 and .Net 4.

  • Create a Website project
  • Modify the Web.Config file
  • Modify the Default.aspx file
  • Modify the Default.aspx.cs file

I’ll assume you know how to create a Website project…File, New, Website, ASP.Net project…

The Web.Config file needs to be modified to contain the httpHandler and controls. These configurations enable the asp:Chart tag which is used in the Default.aspx file.

[sourcecode language="xml" padlinenumbers="true" autolinks="false" gutter="false" toolbar="false"]
<httpHandlers>
  <add path="ChartImg.axd"
      verb="GET,HEAD,POST"
      type="System.Web.UI.DataVisualization
                  .Charting
                  .ChartHttpHandler, 
         System.Web.DataVisualization, 
         Version=4.0.0.0, Culture=neutral, 
         PublicKeyToken=31bf3856ad364e35" 
      validate="false"/>
</httpHandlers>
<controls>
  <add tagPrefix="asp"
      namespace="System.Web.UI.DataVisualization.Charting"
      assembly="System.Web.DataVisualization, 
            Version=4.0.0.0, 
            Culture=neutral, 
            PublicKeyToken=31bf3856ad364e35"/>
</controls>
[/sourcecode]

In the Default.aspx file we will add the chart, titles, legend, series and chartareas.

[sourcecode language="xml" autolinks="false" gutter="false" toolbar="false"]
<asp:chart id="Chart1" runat="server"
             Height="300px" Width="400px">
  <titles>
    <asp:Title ShadowOffset="3" Name="Title1" />
  </titles>
  <legends>
    <asp:Legend Alignment="Center" Docking="Bottom"
                IsTextAutoFit="False" Name="Default"
                LegendStyle="Row" />
  </legends>
  <series>
    <asp:Series Name="Default" />
  </series>
  <chartareas>
    <asp:ChartArea Name="ChartArea1"
                     BorderWidth="0" />
  </chartareas>
</asp:chart>
[/sourcecode]

And lastly, we will add the code to the code-behind file to populate and configure the chart. First thing I do is to load the data values for the chart. If this was implemented in a program being used to represent real data, then the values would not be hardcoded. You would connect to a database, run a query and then have your business logic create the contents of the values. For simplicity, I hard coded the values. Then I simply went through and set the properties that created the above Pie Chart.

[sourcecode language="csharp" autolinks="false" gutter="false" toolbar="false"]
protected void Page_Load(object sender, EventArgs e)
{
  double[] yValues = { 71.15, 23.19, 5.66 };
  string[] xValues = { "AAA", "BBB", "CCC" };
  Chart1.Series["Default"].Points.DataBindXY(xValues, yValues);
 
  Chart1.Series["Default"].Points[0].Color = Color.MediumSeaGreen;
  Chart1.Series["Default"].Points[1].Color = Color.PaleGreen;
  Chart1.Series["Default"].Points[2].Color = Color.LawnGreen;
 
  Chart1.Series["Default"].ChartType = SeriesChartType.Pie;
  Chart1.Series["Default"]["PieLabelStyle"] = "Disabled";
  Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
  Chart1.Legends[0].Enabled = true;
}
[/sourcecode]

I like graphing and charting data. Having worked in a metrics driver organization for many years, I have seen the priceless knowledge one can get from having good data represented by a good graphical representation of the data.

Download the source.



Leave a Comment

Your email address will not be published.