Pie Chart in ASP.NET Websites Using New Microsoft Chart Controls

Awhile back I mentioned the Free Microsoft Chart Controls:

 

You first download and install the chart control library for use with .NET 3.5 SP1 as well as the Visual Studio 2008 installer to add it into the Toolbox. When you toss in on your ASP.NET Page it will look similar to as follows:

 

ASP.NET Pie Chart in Visual Studio 2008

 

Now we can add some C# code in the Page_Load Event of the Code-Behind to create simple Pie Chart for display on the browser. I call this the “Mind Numbing Pie Chart” as it just displays 4 pieces of a pie labeled A - D :) Not very creative I know, but the code shows you how to easily create the Pie Chart by binding X,Y values where X values are the pieces of the Pie and the Y values are the sizes of each piece.

 

protected void Page_Load(object sender, EventArgs e)

{

    // Display 3D Pie Chart

    Chart1.Series[0].ChartType = SeriesChartType.Pie;

    Chart1.Series[0]["PieLabelStyle"] = "Inside";

    Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;

 

    // Display a Title

    Chart1.Titles.Add("Mind Numbing Pie Chart");

 

    // Add Data to Display

    string[] xValues = { "A", "B", "C", "D" };

    int[] yValues = { 20, 30, 50, 40 };

    Chart1.Series[0].Points.DataBindXY(xValues, yValues);

 

    // Call Out The Letter "D"

    Chart1.Series[0].Points[3]["Exploded"] = "true";

 

    // Display a Legend

    Chart1.Legends.Add(new Legend("Alphabet"));

    Chart1.Legends["Alphabet"].Title = "Letters";

    Chart1.Series[0].Legend = "Alphabet";

}

 

The code actually creates a 3D Pie Chart with the letter “D“ exploding out of the pie.

 

ASP.NET Pie Chart in Visual Studio 2008

 

Not bad for about 10 minutes of effort. You have an easy 3D Pie Chart that you can include in your ASP.NET Web Applications. Of course, you can display more than just pie charts. You can display Area Charts, Bar Charts, Line Charts, etc. using the Chart Control.

Hope this helps.

David Hayden

 

Other Visual Studio Add-Ins and Tools.

 

posted on Tuesday, December 02, 2008 11:15 AM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices