Mega Code Archive

 
Categories / C# by API / System Drawing
 

Graphics DrawPie

using System; using System.Drawing; using System.Windows.Forms;     class PieChart: Form {      int[] aiValues = { 50, 100, 25, 150, 100, 75 };          public static void Main()      {           Application.Run(new PieChart());      }      public PieChart()      {           Text = "Pie Chart";           ResizeRedraw = true;      }      protected override void OnPaint(PaintEventArgs pea)      {           DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);      }      protected void DoPage(Graphics grfx, Color clr, int cx, int cy)      {           Rectangle rect   = new Rectangle(50, 50, 200, 200);           Pen       pen    = new Pen(clr);           int       iTotal = 0;           float     fAngle = 0, fSweep;               foreach(int iValue in aiValues)                iTotal += iValue;               foreach(int iValue in aiValues)           {                fSweep = 360f * iValue / iTotal;                DrawPieSlice(grfx, pen, rect, fAngle, fSweep);                fAngle += fSweep;           }      }      protected virtual void DrawPieSlice(Graphics grfx, Pen pen,                                           Rectangle rect,                                          float fAngle, float fSweep)      {           grfx.DrawPie(pen, rect, fAngle, fSweep);       } }