Making A Graphic Pie Chart In C#

Cistoran picture Cistoran · Sep 29, 2009 · Viewed 24.6k times · Source

I'm trying to write a Windows Application that shows a pie chart with seven unequal slices (25%, 20%, 18%, 17%, 10%, 10%, 10%) all of them will be colored differently.

So far I have made Pens and Brushes with colors attached and drawn a circle.

This is what I have so far

private void Form1_Paint(object sender, PaintEventArgs e)
    {
        this.BackColor = Color.White;
        this.Text = "Pie Chart";
        this.Width = 350;
        this.Height = 350;

        Pen black = new Pen(Color.Black);
        Pen blue = new Pen(Color.Blue);
        Pen green = new Pen(Color.Green);
        Pen red = new Pen(Color.Red);
        Pen orange = new Pen(Color.Orange);
        Pen pink = new Pen(Color.Pink);
        Pen purple = new Pen(Color.Purple);
        Pen magenta = new Pen(Color.Purple);
        Brush brBlue = blue.Brush;
        Brush brGreen = green.Brush;
        Brush brRed = red.Brush;
        Brush brOrange = orange.Brush;
        Brush brPink = pink.Brush;
        Brush brPurple = purple.Brush;
        Brush brMagenta = magenta.Brush;
        Graphics g = e.Graphics;

        g.DrawEllipse(black, 20, 10, 300, 300);

    }

My question to you is. What would be the easiest way to draw the wedges of the pie?

Answer

MRG picture MRG · Sep 29, 2009

I will advise you to take a look at ZedGraph.

If you want a sample code to actually draw pieChart using GDI you can check this tutorial.. It uses FillPie Method of Graphics class.