Better Dashboards

Working with Microsoft enterprise chart, dashboard and reporting technologies.

Pie Charts in ASP.NET

Posted by milang on January 19, 2009

I started to create samples that build upon the idea of creating a pie chart in Visual Studio, but it is easier if I simply start from scratch with a pie chart, and then show some tricks for how to tweak its appearance.   For this particular example, I’ll use my own random data, but you can data bind values from a database.  The chart samples show several different ways of populating chart data.

How to Add a Pie Chart to your ASP.NET Website

1.    Drop the chart onto a web page.
2.    Copy and paste the following code into your code page.


using System.Web.UI.DataVisualization.Charting;

public partial class PieChart : System.Web.UI.Page
{
    // Create a new Random object
    Random rand = new Random();

    // Create an index that holds the current index of the sales number
    int index = 0;

    // Create variable to hold sales data
    double [] yearlySales = { 93, 100, 22, 23, 43, 54, 44, 9, 10, 1};

    // Create a list of sales people
    string[] salesPeopleTop10 = { "John Smith", "Patrick Johnson", "Michael Berube", "Paul Bradshaw", "Jacob Wright", "Jonathan Rosen", "Robert McDonald", "Joseph Hanson", "Marcel Thompson", "Trey Kelley" };

    protected void Page_Load(object sender, EventArgs e)
    {       
        // Set the chart to be a pie chart
        this.Chart2.Series[0].ChartType = SeriesChartType.Pie;

        foreach (string salesPerson in salesPeopleTop10)
        {
            this.Chart2.Series[0].Points.AddXY(salesPerson, yearlySales[index]);
            index++;
        }
    }
}

The resulting chart should look something like this:
Pie Chart
From here, there are a variety of things that we can do:
–    Show the labels outside the pie chart
–    Collect small slices into one slice
–    Perform several different ways of labeling the chart

3 Responses to “Pie Charts in ASP.NET”

  1. […] Pie Charts in Visual Studio […]

  2. […] by milang on February 4, 2009 Building on a previous post about how to create a pie chart in ASP.NET, let’s look at how to display percentages on a pie chart.  This is surprisingly difficult […]

  3. […] by milang on February 4, 2009 Building on a previous post about how to create a pie chart in ASP.NET, let’s look at how to display percentages on a pie chart.  This is surprisingly difficult […]

Leave a reply to Display Percentages on a Pie Chart « Better Dashboards Cancel reply