DrawPie with offset : Graphics « 2D Graphics « C# / C Sharp






DrawPie with offset

 

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()
     {
          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)
     {
          float fSlice = (float)(2 * Math.PI * (fAngle + fSweep / 2) / 360);
   
          rect.Offset((int)(rect.Width  / 10 * Math.Cos(fSlice)),
                      (int)(rect.Height / 10 * Math.Sin(fSlice)));
                      
          grfx.DrawPie(pen, rect, fAngle, fSweep); 
     }
}

 








Related examples in the same category

1.Graphics: TranslateClip
2.Graphics: SetClip
3.Scribble with MouseScribble with Mouse
4.FillPolygon: Alternate and Winding Fill Modes
5.One-Inch Ellipse
6.Graphics: FillRectangles
7.Graphics: FillRectangle
8.Graphics: FillEllipse
9.Wide Polyline: DrawLines
10.Line and Arc Combo
11.Graphics: FillClosedCurve
12.Bezier Art: DrawBeziers
13.Graphics: MeasureString
14.Clear a Graphics
15.Graphics: DrawRectangles
16.Graphics: DrawRectangle
17.Draw Ellipse
18.Graphics: DrawArc
19.Graphics: DrawPie
20.Draw a Rectangle
21.Draw an Image
22.FillEllipse: Red Traffic Light
23.Graphics: Draw an Icon
24.Graphics: DrawString
25.Use different Font object to draw a line of text
26.Graphics: ScaleTransform
27.Graphics: Transform
28.Graphics: TranslateTransform
29.Graphics: Pen Alignment
30.Graphics: PageScale
31.Graphics: PageUnit
32.DpiY and DpiX
33.PixelOffsetMode
34.Paint along points in a list pointsPaint along points in a list points
35.Picture Cube: DrawImage
36.Clipping: ResetClip
37.Rotate the text 45 degrees clockwise then Translate the text 150 pixels horizontally
38.Transform and RotateTransform in a loop
39.Apply the scaling transformation(scale subsequent operations by 2x horizontally and 3x vertically)
40.Translate the text 150 pixels horizontally and 75 vertically
41.Reset the transformation Translate by 30 pixels in vertical direction
42.Rotate the text through 45 degrees
43.Fill the area 'bounded' by the path
44.DrawImage with destination points
45.DrawImage with size
46.Graphics.SmoothingMode
47.TransformPoints
48.Fill a Rectangle with TextureBrush
49.Fill a Rectangle with LinearGradientBrush