To draw a pie, you can use the Graphics.DrawPie()
method that comes in various versions as follows:
Public Sub DrawPie ( _
pen As Pen, _
rect As Rectangle, _
startAngle As Single, _
sweepAngle As Single _
)
Public Sub DrawPie ( _
pen As Pen, _
rect As RectangleF, _
startAngle As Single, _
sweepAngle As Single _
)
Public Sub DrawPie ( _
pen As Pen, _
x As Integer, _
y As Integer, _
width As Integer, _
height As Integer, _
startAngle As Integer, _
sweepAngle As Integer _
)
Public Sub DrawPie ( _
pen As Pen, _
x As Single, _
y As Single, _
width As Single, _
height As Single, _
startAngle As Single, _
sweepAngle As Single _
)
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:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
End Sub
Private Sub FormPaint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim penCurrent As Pen = New Pen(Color.Red)
e.Graphics.DrawPie(penCurrent, 20, 20, 200, 100, 45, 255)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
This would produce:
Practical
Learning: Drawing an Ellipse
|
|
- Start Microsoft Visual Basic and create a new Windows Application named
SchoolEnrolment1
- Design the form as follows:
|
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 |
|
- Right-click the form and click View Code
- Declare three variables as follows:
Public Class Form1
Private Graduates As Single
Private Undergraduates As Single
Private Certificates As Single
End Class
|
- In the Class Name combo box, select (Form1 Events)
- In the Method Name combo box, select Paint and implement the event as
follows:
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))
End Sub
|
- In the Class Name combo box, select pbxChart
- In the Method Name combo box, select Paint and implement its event as
follows:
Private Sub pbxChart_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles pbxChart.Paint
Invalidate()
End Sub
|
- In the Class Name combo box, select pbxCreateChart
- In the Method Name combo box, select Click and implement the event as
follows:
Private Sub btnCreateChart_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnCreateChart.Click
Dim Grad As Single = 0.0
Dim Under As Single = 0.0
Dim Cert As Single = 0.0
Dim Total As Single = 0.0
Dim PercentGraduates As Single
Dim PercentUndergraduates As Single
Dim PercentCertificates As Single
Try
Grad = CSng(txtGraduates.Text)
Catch ex As FormatException
MsgBox("Invalid graduate value")
End Try
Try
Under = CSng(txtUndergraduates.Text)
Catch
MsgBox("Invalid graduate value")
End Try
Try
Cert = CSng(txtCertificates.Text)
Catch
MsgBox("Invalid graduate value")
End Try
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()
End Sub
|
- Execute the application and test the form
- After using it, close the form
Download
|
|