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:
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:
Application: Drawing an Ellipse
Control | Name | Text | |
Label | Enrolment / Program ___________________________ | ||
Label | Graduates | ||
Label | Undergraduates | ||
Label | Certificates | ||
TextBox | txtGraduates | 0 | |
TextBox | txtUndergraduates | 0 | |
TextBox | txtCertificates | 0 | |
Button | btnCreateChart | Create Chart | |
PictureBox | pbxChart | ||
Label | ____Legend____ | ||
Label | lblGraduates | Graduates | |
Label | lblUndergraduates | Undergraduates | |
Label | lblCertificates | Certificates | |
Button | btnClose | Close |
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();
}
}
}
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)); }
private void pbxChart_Paint(object sender, PaintEventArgs e) { Invalidate(); }
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(); }
|
||
Home | Copyright © 2010-2020, FunctionX | |
|