Draw Pie Chart For Database Values in VB.NET

The basic ideas of the How to draw a rectangle, How to display a string & also How to draw a pie chart. Now What actually I have done to draw a pie chart for student’s ages… First of all it will retrieve the data of student's age into the list & then depending on the values of the ages it will display pie chart & also display legends of that pie chart with values at given location & given size & also in different-different colors...

# Basic Things Before Understand The Whole Article...


=> How to draw a
Rectangle?
~> Using the "FillRectangle" function you can draw a rectangle, for that you have to pass "Brush" which will draw a rectanlgle & fill color which i have use. after that you have to pass X coordiante location & then y coordinates location. Finally you have to pass height & width of the rectangle...

~> See Below Code :

Dim MyGraphic As Graphics
Using brush
As New SolidBrush(Color.Red)
MyGraphic.FillRectangle(brush, 100, 50, 20, 20)
End Using



=> How to display String?
~> For display string first of all pass string which you want to display & then pass the font in which font style & size you want to display given string, then pass brush which draw a string with given color & Finally pass the location where you want to display string...

~> See Below Code :

Dim MyGraphic As Graphics
Using brush
As New SolidBrush(Color.Black)
   MyGraphic.DrawString("Display String Demo", New Font("Arial", 16), brush, New Point(150, 100))
End Using




=> How to draw a Pie Chart?
~> For display Pie Chart first of all you have to pass brush for draw a pie with filling colors then pass one rectangle in that recatngle pie chart will be drawn 7 for that pass the location & size of rectangle then pass the start angle from which angle pie will be started & finally upto which angle pie will be drawn...

~> See Below Code :

Dim MyGraphic As Graphics
Using brush
As New SolidBrush(Color.FromArgb(rd.Next()))
                  MyGraphic.FillPie(brush, New Rectangle(New Point(10,10), New Size(50,50)), start_angle, upto_angle)
End Using



# Now Original Article For Display Pie Chart Of Ages Of Student's Table From The Database...


Imports
System.Data.OleDb
Imports System.Drawing.Drawing2D
Imports System.Messaging
Imports System.ServiceProcess

Public Class Form1
     Dim Conn
As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mdb1.mdb")
     Dim dr
As OleDbDataReader

     Dim rd
As New Random(255)
     Dim Arr
As New ArrayList
     Dim pos
As Integer

    
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Call "Bind()) function to bind the student table data into the databgrid view
Bind()
End Sub

Public Sub FinalDrawPieChart()
         Dim APiePercent(Arr.
Count) As Integer
        
For index = 0 To Arr.Count - 1
'Store all "Age" of students into the array...
             APiePercent(index) =
Convert.ToInt32(Arr.Item(index))
        
Next

         Using PieGraphic =
Me.CreateGraphics()
'Call "DrawPieChart" function for draw Pie Chart Depending on the ages & locations & colors...
             DrawPieChart(APiePercent, PieGraphic,
New Point(20, 20), New Size(130, 130))
        
End Using
    
End Sub

    
Public Sub DrawPieChart(ByVal PiePercents() As Integer, ByVal PieGraphic As Graphics, ByVal PieLocation As Point, ByVal PieSize As Size)

         Dim sum = 0
        
For index = 0 To Arr.Count - 1
'Do the sum of ages...
             sum += PiePercents(index)
        
Next

         Dim eachper
As Double
         eachper = 0.0
        
For index = 0 To Arr.Count - 1
'Convert each ages into the percentage...
             eachper = CSng((PiePercents(index) * 100) / sum)
         
Next

Dim PiePercentTotal = 0
         Dim percy
As Integer
         percy = 40

        
For PiePercent = 0 To Arr.Count - 1
             Using brush
As New SolidBrush(Color.FromArgb(rd.Next()))
'Draw Pie for each age...
' Here it convert the percentage value into the angle value, so the value will multiply with 360 7 divide by 100...
                 PieGraphic.FillPie(brush,
New Rectangle(Location, PieSize), CSng((PiePercentTotal * 360) / 100), CSng(((PiePercents(PiePercent) * 100) / sum) * 360) / 100)

'Draw Rectangle as Legends of charts...
                 PieGraphic.FillRectangle(brush, 250, percy, 20, 20)

'Display each age bisides to each rectangle...
                 PieGraphic.DrawString(Arr.Item(PiePercent),
New Font("Arial", 16), New SolidBrush(Color.Black), New Point(290, percy))

'Increment the y coordinates by 30 to draw a new rectangles at new location...
                 percy = percy + 30
             
End Using

             PiePercentTotal += ((PiePercents(PiePercent) * 100) / sum)
         
Next
        
Return
    
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Arr.
Clear()
        
Try
             Conn.
Open()
             Dim cmd
As OleDbCommand = New OleDbCommand("Select Age From Student", Conn)
             dr = cmd.ExecuteReader
             
While dr.Read()
'Store each value of ages one by one into arraylist...
                  Arr.
Add(dr.GetValue(0).ToString())
                 pos += 1
             
End While
Catch ex
As Exception
              MessageBox.Show(ex.ToString())
          Finally
              dr.
Close()
             Conn.
Close()
        
End Try

'Finally Call the "FinalDrawPieChart" function which display pie chart...
FinalDrawPieChart()

End Sub





# See Below Image :



By James J   Popularity  (3201 Views)