Home

GDI+ Shapes: Pies

Introduction

A pie is a fraction of an ellipse delimited by a starting angle and an angle that constitutes the desired portion to make up a pie. It can be illustrated as follows:

Pie Illustration

To draw a pie, you can use the Graphics.DrawPie() method that comes in various versions as follows:

public void DrawPie(Pen pen,
                 Rectangle rect,
                 float startAngle,
                 float sweepAngle);
public void DrawPie(Pen pen,
                 RectangleF rect,
                 float startAngle,
                 float sweepAngle);
public void DrawPie(Pen pen,
                 int x,
                 int y,
                 int width,
                 int height,
                 int startAngle,
                 int sweepAngle);
public void DrawPie(Pen pen,
                 float x,
                 float y,
                 float width,
                 float height,
                 float startAngle,
                 float sweepAngle);

A pie is based on an ellipse (like an arc). The ellipse would fit in a rectangle passed as the rect argument. The rectangle can also be specified by its location (x, y) and its dimensions (width and height).

Inside of the parent rectangle in which an ellipse would be drawn, you set a starting angle. This angle is measured from 0 up counted clockwise (like the numbers of an analog clock). This means that an angle of 90 represents 6 o'clock and not 12 o'clock. This starting angle is passed as the startAngle argument.

After specifying the starting angle, you must specify the amount of angle covered by the pie. This also is measured clockwise. This value is passed as the sweepAngle argument.

Here is an example:

private void Exercise_Paint(object sender, PaintEventArgs e)
{
        Pen penCurrent = new Pen(Color.Red);
        e.Graphics.DrawPie(penCurrent, 20, 20, 200, 100, 45, 255);
}

This would produce:

Pie

ApplicationApplication: Drawing an Ellipse

  1. Start Microsoft Visual C# and create a new Windows Application named SchoolEnrolment1
  2. Design the form as follows:
     

    School Enrolment

    Control Name Text
    Label Label   Enrolment / Program ___________________________
    Label Label   Graduates
    Label Label   Undergraduates
    Label Label   Certificates
    TextBox TextBox txtGraduates 0
    TextBox TextBox txtUndergraduates 0
    TextBox TextBox txtCertificates 0
    Button Button btnCreateChart Create Chart
    PictureBox PictureBox pbxChart  
    Label Label   ____Legend____
    Label Label lblGraduates Graduates
    Label Label lblUndergraduates Undergraduates
    Label Label lblCertificates Certificates
    Button Button btnClose Close
  3. Right-click the form and click View Code
  4. Declare three variables as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace SchoolEnrolment1
    {
        public partial class Form1 : Form
        {
            float Graduates;
            float Undergraduates;
            float Certificates;
    
            public Form1()
            {
                InitializeComponent();
            }
        }
    }
  5. Return to the form and click an unoccupied area of its body. In the Properties window, click the Events button Events
  6. Double-click the Paint field and implement the event as follows:
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
                pbxChart.CreateGraphics().DrawEllipse(new Pen(Color.Red),
                                  new Rectangle(0,  0, 260, 200));
                pbxChart.CreateGraphics().DrawPie(new Pen(Color.Blue),
                                          0.0F, 0.0F, 260.0F, 200.0F, 0.0F, Graduates);
                pbxChart.CreateGraphics().DrawPie(new Pen(Color.Green),
                                          0.0F, 0.0F, 260.0F,
                            200.0F, Graduates, Undergraduates);
                pbxChart.CreateGraphics().DrawPie(new Pen(Color.Fuchsia),
                                          0.0F, 0.0F, 260.0F,
                              200.0F, Graduates + Undergraduates,
                                  Certificates);
    
                e.Graphics.DrawEllipse(new Pen(Color.Blue),
                                      new Rectangle(lblGraduates.Left,
                                    lblGraduates.Top + 20,
                            lblUndergraduates.Width,
                            20));
    
                e.Graphics.DrawEllipse(new Pen(Color.Green),
                                      new Rectangle(lblUndergraduates.Left,
                                    lblUndergraduates.Top + 20,
                            lblUndergraduates.Width,
                            20));
    
                e.Graphics.DrawEllipse(new Pen(Color.Fuchsia),
                                      new Rectangle(lblCertificates.Left,
                                    lblCertificates.Top + 20,
                            lblUndergraduates.Width,
                                20));
    }
  7. Return to the form and click the picture box
  8. In the Events section of the Properties window, double-click Paint and implement its event as follows:
    private void pbxChart_Paint(object sender, PaintEventArgs e)
    {
                Invalidate();
    }
  9. Return to the form and double-click the Create Chart button
  10. Implement the event as follows:
    private void btnCreateChart_Click(object sender, EventArgs e)
    {
                float grad = 0.00F,
                      under = 0.00F,
                      cert = 0.00F,
                      total = 0.00F;
                float percentGraduates,
                  percentUndergraduates,
                  percentCertificates;
    
                try
                {
                    grad = float.Parse(txtGraduates.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid graduate value");
                }
    
                try
                {
                    under = float.Parse(txtUndergraduates.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid graduate value");
                }
    
                try
                {
                    cert = float.Parse(txtCertificates.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid graduate value");
                }
    
                total = grad + under + cert;
                percentGraduates = (grad / total) * 100;
                percentUndergraduates = (under / total) * 100;
                percentCertificates = (cert / total) * 100;
    
                Graduates = (360 * percentGraduates) / 100;
                Undergraduates = (360 * percentUndergraduates) / 100;
                Certificates = (360 * percentCertificates) / 100;
    
                pbxChart.Invalidate();
    }
  11. Execute the application and test the form
     
     School Enrolment
  12. After using it, close the form

Home Copyright © 2010-2020, FunctionX