Draw a pie chart in C#

[pie chart]

This example shows how to draw a pie chart by using the techniques described in the post Fill and draw pie slices in C#.

This example uses the following DrawPieChart method to draw a pie chart for an array of values.

// Draw a pie chart.
private static void DrawPieChart(Graphics gr,
    Rectangle rect, Brush[] brushes, Pen[] pens,
    float[] values)
{
    // Get the total of all angles.
    float total = values.Sum();

    // Draw the slices.
    float start_angle = 0;
    for (int i = 0; i < values.Length; i++)
    {
        float sweep_angle = values[i] * 360f / total;
        gr.FillPie(brushes[i % brushes.Length],
            rect, start_angle, sweep_angle);
        gr.DrawPie(pens[i % pens.Length],
            rect, start_angle, sweep_angle);
        start_angle += sweep_angle;
    }
}

The code starts by using the Sum extension method provided by LINQ to get the total of the values. It then loops over the values converting each into a percentage of the total. It coverts each percentage into a sweep angle and draws a corresponding pie slice. The code uses the % modulus operator to determine which colors to use for each slice in case the number of colors doesn’t match the number of slices. For example, this program only uses a single pen to outline every slice.

The following code shows how the program initializes its data.

// Brushes used to fill pie slices.
private Brush[] SliceBrushes =
{
    Brushes.Red,
    Brushes.LightGreen,
    Brushes.Blue,
    Brushes.LightBlue,
    Brushes.Green,
    Brushes.Lime,
    Brushes.Orange,
    Brushes.Fuchsia,
    Brushes.Yellow,
    Brushes.Cyan,
};

// Pens used to outline pie slices.
private Pen[] SlicePens = { Pens.Black };

// The data values to chart.
private float[] Values = new float[10];

// Make some random data.
private void Form1_Load(object sender, EventArgs e)
{
    Random rand = new Random();
    for (int i = 0; i < Values.Length; i++)
    {
        Values[i] = rand.Next(10, 40);
    }
}

The SliceBrushes array holds brushes used when drawing slices. The SlicePens array holds the single pen Pens.Black so all slices are outlined in black. When the form loads, its Load event handler fills the Values array with random values. The following code shows how the program uses the DrawPieChart method when the form repaints.

// Draw the pie chart.
private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.Clear(BackColor);
    if ((ClientSize.Width < 20) || (ClientSize.Height < 20)) return;

    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    Rectangle rect = new Rectangle(
        10, 10, ClientSize.Width - 20, ClientSize.Height - 20);
    DrawPieChart(e.Graphics, rect, SliceBrushes, SlicePens, Values);
}

This code clears the form with its background color. It then makes sure the form’s client area is at least 20 by 20 pixels in size.

The code sets the Graphics object’s SmoothingMode to draw smooth lines. Finally it creates a Rectangle that fills the form minus a 10 pixel margin and calls DrawPieChart to draw the pie chart in that Rectangle.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in algorithms, graphics, mathematics and tagged , , , , , , , , , , , , . Bookmark the permalink.

One Response to Draw a pie chart in C#

  1. Pingback: Draw a labeled pie chart in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *