Wednesday, March 23, 2005 - Posts

Simple Pie Chart Generator

Here's a simple Pie Chart Generator that I created today

Create a Bitmap for the Pie chart call DrawPie and then saves as a PNG.


        Bitmap oBmp  = new Bitmap(325, 200, PixelFormat.Format24bppRgb);
        Graphics oGraphic = Graphics.FromImage(oBmp);
        oGraphic.SmoothingMode = SmoothingMode.AntiAlias;
        
        oGraphic.Clear(Color.White);
        
        int [] nVals = new int[5];
        nVals[0] = 32;
        nVals[1] = 23;
        nVals[2] = 26;
        nVals[3] = 12;
        nVals[4] = 41;
        
        DrawPie(nVals, "Some Caption", oGraphic );
        
        string cFileName = DateTime.Now.Ticks.ToString() + ".png";
        
        oBmp.Save( "\\" + cFileName, ImageFormat.Png);
        oGraphic.Dispose();
        oBmp.Dispose();
        
      

 

 

 This is the DrawPie method it pass it a collection of integers and the the Graphics Object and it will append the Chart to it.

    private static void DrawPie(int [] nVals, string cCaption, Graphics oGraphic)
    {
        int nTotalValue = 0;
        for( int i = 0; i < nVals.Length; i++ ){nTotalValue += nVals[i];}
        // find the % that one point is equal to.

        double nPointVal = (nTotalValue / 100.0);
        Pen oPen = new System.Drawing.Pen(Color.Gray, 1);
        float nStartOfsegment = 270;
    
        for(int i = 0; i < nVals.Length; i++ )
        {
            //for each value parsed find out how what % the value of the total amount parsed 
            float nPercentOfTotal =  (float)(nVals[i] / nPointVal) ;
            // and then what % it is of 360 degrees;
            float nPercentOfPie = (float)(nPercentOfTotal * 3.6 );
            // Render the Pie segment.
            
            Brush oBrush;
            switch(i)
            {
                case 0: oBrush = Brushes.Green; break;
                case 1: oBrush = Brushes.Red; break;
                case 2: oBrush = Brushes.Blue; break;
                case 3: oBrush = Brushes.Purple; break;
                case 4: oBrush = Brushes.Orange; break;
                case 5: oBrush = Brushes.OrangeRed; break;
                case 6: oBrush = Brushes.Orchid; break;
                case 7: oBrush = Brushes.DarkBlue; break;
                case 8: oBrush = Brushes.DarkGoldenrod; break;
                default :
                    oBrush = Brushes.Black;
                    break;
            }
            oGraphic.FillPie( oBrush, 10, 10, 100, 100, nStartOfsegment, nPercentOfPie );
            oGraphic.DrawPie( oPen, 10, 10, 100, 100, nStartOfsegment, nPercentOfPie );
                
            if( nStartOfsegment + nPercentOfPie >= 360 )
            {
                nStartOfsegment = nPercentOfPie - ( 360 - nStartOfsegment );
            }
            else
            {
                nStartOfsegment += nPercentOfPie;
            }
        }
        oGraphic.DrawString( cCaption, new Font("verdana", 12, FontStyle.Bold, GraphicsUnit.Pixel), SystemBrushes.WindowText, 23, 130);            
    }

                 
with 1 Comments