Previous | Home | Next |
Introduction
In VB.Net with the help of GDI classes we can design or draw charts or graphs is a very efficient manner in our projects. Charts are the pictorial representation of the data through chart one can easily judge or see the ups and down or profit loss in other words we can say we can show the groth rate of any business through chart.
Slice Charts are consider as cutting a cake into slices based on the weight. In this chart, angle of slice is linearly proportional to weight. With the help of FillPie() method slice chart can be designed.
Private Function FillPie(brush As Brush, x As Integer, y As Integer, width As Integer, height As Integer, startAngle As Integer, _ sweepAngle As Integer) As Void End Function
FillPie function is used to fill as ellipse if sweep angle is 360. To make it a circle, we make height and width both equal to diameter of circle.
Brush brush : It is a brush object used to fill the Pie.
Int x : x co-ordinate of upper left corner of the bounding rectangle that defines the ellipse from which the pie section comes.
So if the x co-ordinate of center of our circle is x0 and radius is r: - x = x0 – r; Int y: Same as x- co-ordinate. y = y0 – r;
Int y : Same as x- co-ordinate. y = y0 – r;
Int width : Width of bounding rectangle that defines the ellipse from which the pie section comes. width = 2*radius;
Int startAngle : Angle in degrees measured clockwise from the x-axis to the first side of the pie section.
So we start with 0 degrees and increment it with the sweepAngle for the next slice to start right after the end of previous slice.
Int sweepAngle : Angle in degrees measured clockwise from the startAngle parameter to the second side of pie section. So this is span of angle based on weight.
So 360 degrees is proportionally divided into 8 parts based on the weight. Example: To draw slice chart we have to take an array named alweight.
Example
To draw slice chart we have to take an array named alweight.
Code for on Form Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load alWeight = New Integer() {13, 23, 33, 15, 20, 10, 4, 11} End Sub
Code for DrawSliceChart methods
Private Sub DrawSliceChart(e As PaintEventArgs, alWeight As Integer()) Dim numberOfSections As Integer = alWeight.Length Dim x0 As Integer = 100 Dim y0 As Integer = 100 Dim radius As Integer = 100 Dim startAngle As Integer = 0 Dim sweepAngle As Integer = 0 Dim height As Integer() = New Integer(numberOfSections - 1) {} Dim total As Integer = SumOfArray(alWeight) Dim rnd As New Random() Dim brush As New SolidBrush(Color.Aquamarine) Dim pen As New Pen(Color.Black) For i As Integer = 0 To numberOfSections - 1 brush.Color = Color.FromArgb(rnd.[Next](200, 255), rnd.[Next](255), rnd.[Next](255), rnd.[Next](255)) If i = numberOfSections - 1 Then sweepAngle = 360 - startAngle Else sweepAngle = (360 * alWeight(i)) \ total End If e.Graphics.FillPie(brush, x0 - height(i), y0 - height(i), 2 * radius, 2 * radius, startAngle, _ sweepAngle) e.Graphics.DrawPie(pen, x0 - height(i), y0 - height(i), 2 * radius, 2 * radius, startAngle, _ sweepAngle) startAngle += sweepAngle brush.Color = Color.FromKnownColor(KnownColor.Black) Next End Sub
Code for SumofArray method
Private Shared Function SumOfArray(intArray As Integer()) As Integer Dim sum As Integer = 0 For i As Integer = 0 To intArray.Length - 1 sum += intArray(i) Next Return sum End Function
Difference between FillPie( ) and DrawPie( )
The only difference between DrawPie and FillPie is that it takes Pen object instead of Brush.
Calling DrawSliceChart Method
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint DrawSliceChart(e, alWeight) End Sub
Previous | Home | Next |