Drawing a Pie Chart in GDI+

In this article you will learn how to Draw a Pie Chart in GDI+.


This article has been excerpted from book "Graphics Programming with GDI+".

Now let's write an application using ASP.NET. We will provide both fill and draw options.

We create Forms application using Visual Studio .NET. We add some text and two buttons to the page. The final Web page looks like Figure 12.16. The Draw Chart button will draw a pie chart, and the Fill Chart button will fill the chart with different colors.

Now we add some variables (see Listing 12.10). Instead of reading values from the user, we use hard-coded values for the valArrray and clrArray array. The valArray array stored the different portion values of a pie chart, and clrArray stores colors for these portions. If you wish, you can modify the page and add some text boxes to allow users to provide these values at runtime.

Figure-12.16.gif

FIGURE 12.16: A pie chart-drawing application in ASP.NET

LISTING 12.10: User-defined variables

//User-defined variable
public Bitmap curBitmap;
private Rectangle rect =
new Rectangle (250, 150, 200, 200);
public ArrayList sliceList = new ArrayList();
private Color curClr = Color.Black;
int[] valArray = {50, 25, 75, 100, 50};
Color[] clrArray = {Color.Red, Color.Green, Color.Yellow, Color.Pink, Color.Aqua};
int total =0;

Now we add a method called DrawPieChart. It will both draw and fill the chart. The code for the DrawPieChart method is given in Listing 12.11.

We simply read values from the portion and color arrays, and we create SolidBrush and Pen objects, depending on which button is clicked. We create a Bitmap object and set the smoothing mode of the page to AntiAlias. We also initialize the values of the angle and sweep variable.

We also have a Boolean variable called flMode. If flMode is true, the DrawPieChart method calls FillPie to fill the pie chart; otherwise it calls DrawPie, which draws only the boundaries of the chart. In the end, we save the bitmap, send it to the browser, and dispose of the objects.

LISTING 12.11: The DrawPieChart method

        private void DrawPieChart(bool flMode)
        {
            //Create Bitmap and Graphics objects
            Bitmap curBitmap = new Bitmap(500, 300);
            Graphics g = Graphics.FromImage(curBitmap);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            float angle = 0;
            float sweep = 0;
            //Total
            for (int i = 0; i < valArray.Length; i++)
            {
                total += valArray[i];
            }
            //Read color and value from array
            //and calculate sweep
            for (int i = 0; i < valArray.Length; i++)
            {
                int val = valArray[i];
                Color clr = clrArray[i];
                sweep = 360f * val / total;
                //If fill mode, fill pie
                if (flMode)
                {
                    SolidBrush brush = new SolidBrush(clr);
                    g.FillPie(brush, 20.0f, 20.0f, 200, 200, angle, sweep);
                }
                else //if draw mode, draw pie
                {
                    Pen pn = new Pen(clr, 2);
                    g.DrawPie(pn, 20.0f, 20.0f, 200, 200, angle, sweep);
                }
                angle += sweep;
            }
            //Send output to the browser
            curBitmap.Save(this.Response.OutputStream,
            ImageFormat.Jpeg);
            //Dispose of objects
            curBitmap.Dispose();
            g.Dispose();
        }

The Draw Chart button click generates the output shown in Figure 12.17 and the Fill Chart button click fills in the chart, with output as shown in Figure 12.18.

We call the DrawPieChart method from our Draw Chart and Fill Chart buttons with single argument - false or true, respectively - as shown in Listing 12.12.

Figure-12.17.jpg

FIGURE 12.17: The Draw Chart button click in action

LISTING 12.12: The Draw Chart and Fill Chart button click handlers

        private void DrawChart_Click(object sender, System.EventArgs e)
        {
            DrawPieChart(false);
        }
        private void FillChart_Click(object sender, System.EventArgs e)
        {
            DrawPieChart(true);
        }

Figure-12.18.jpg

FIGURE 12.18: The Fill Chart button click in action

book.gif