Home

GDI+ Tutorials: Pies

 

Description

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:

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);
    void DrawPie(Pen ^pen,
                 RectangleF rect,
                 float startAngle,
                 float sweepAngle);
    void DrawPie(Pen ^pen,
                 int x,
                 int y,
                 int width,
                 int height,
                 int startAngle,
                 int sweepAngle);
    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:

System::Void Form1_Paint(System::Object ^  sender, 
			 System::Windows::Forms::PaintEventArgs ^  e)
{
    Pen ^ penCurrent = gcnew Pen(Color::Red);
    e->Graphics->DrawPie(penCurrent, 20, 20, 200, 100, 45, 255);
}

This would produce:

Pie

Practical LearningPractical Learning: Drawing an Ellipse

  1. Start a new Windows Forms 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:
     
    private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    		float Graduates;
    		float Undergraduates;
    		float Certificates;
  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:
     
    System::Void Form1_Paint(System::Object^  sender, 
    	System::Windows::Forms::PaintEventArgs^  e)
    {
        pbxChart->CreateGraphics()->DrawEllipse(gcnew Pen(Color::Red),
    			                  Rectangle(0,
    					            0,
    						    260,
    						    200));
        pbxChart->CreateGraphics()->DrawPie(gcnew Pen(Color::Blue),
    			                  0.0F, 
    					  0.0F,
    					  260.0F,
    					  200.0F, 0.0F, Graduates);
        pbxChart->CreateGraphics()->DrawPie(gcnew Pen(Color::Green),
    			                  0.0F,
    					  0.0F,
    					  260.0F,
    				200.0F, Graduates, Undergraduates);
        pbxChart->CreateGraphics()->DrawPie(gcnew Pen(Color::Fuchsia),
    			                  0.0F, 
    					  0.0F,
    					  260.0F,
    				  200.0F, Graduates + Undergraduates,
    					  Certificates);
    
        e->Graphics->DrawEllipse(gcnew Pen(Color::Blue),
    		                  Rectangle(lblGraduates->Left,
    			            lblGraduates->Top + 20,
    				lblUndergraduates->Width,
    				20));
    				 
        e->Graphics->DrawEllipse(gcnew Pen(Color::Green),
    		                  Rectangle(lblUndergraduates->Left,
    			            lblUndergraduates->Top + 20,
    				lblUndergraduates->Width,
    				20));
    				 
        e->Graphics->DrawEllipse(gcnew Pen(Color::Fuchsia),
    		                  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:
     
    System::Void pbxChart_Paint(System::Object^  sender, 
    			System::Windows::Forms::PaintEventArgs^  e)
    {
        Invalidate();
    }
  9. Return to the form and double-click the Create Chart button
  10. Implement the event as follows:
     
    System::Void btnCreateChart_Click(System::Object^  sender, 
    				  System::EventArgs^  e)
    {
        float grad, under, cert, total;
        float percentGraduates,
    	  percentUndergraduates,
    	  percentCertificates;
    
        grad = float::Parse(txtGraduates->Text);
        under = float::Parse(txtUndergraduates->Text);
        cert = float::Parse(txtCertificates->Text); // 1860;
    
        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 © 2007-2012 FunctionX