G
Guest
I have a code below and its a PIE & BAR CHART.
The values now are all static but I want to be able to pull the values from
a database.
Can you guys give me some ideas to do this?
Thanks
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Declare your object variables
Dim i As Integer
'Build a BitMap that will act as the pallet and container
'for the bar graph. Here 600 is the width and 300 is the height.
'These values could also be passed as parameters.
Dim objBitMap As New Bitmap(400, 200)
'Declare your Graphics objects for painting graphics on you newly
created bitmap.
Dim objGraphics As Graphics
objGraphics = Graphics.FromImage(objBitMap)
'Set the background color to silver
objGraphics.Clear(Color.Silver)
'Build an array of values for the bar and pie chart.
'These values could also be pulled from a database.
' Retrieve Products Table
Dim arrValues(5) As Integer
arrValues(0) = 100
arrValues(1) = 135
arrValues(2) = 115
arrValues(3) = 125
arrValues(4) = 75
arrValues(5) = 300
Dim arrValueNames(5) As String
arrValueNames(0) = "Jan"
arrValueNames(1) = "Feb"
arrValueNames(2) = "Mar"
arrValueNames(3) = "Apr"
arrValueNames(4) = "May"
arrValueNames(5) = "June"
'Write out a title for your bar and pie chart.
objGraphics.DrawString("5 Month Projection Report", New
Font("Tahoma", 16), Brushes.Black, New PointF(5, 5))
'Create a legend to describe your bar and chart.
Dim symbolLeg As PointF = New PointF(335, 20)
Dim descLeg As PointF = New PointF(360, 16)
For i = 0 To arrValueNames.Length - 1
objGraphics.FillRectangle(New SolidBrush(GetColor(i)),
symbolLeg.X, symbolLeg.Y, 20, 10)
objGraphics.DrawRectangle(Pens.Black, symbolLeg.X, symbolLeg.Y,
20, 10)
objGraphics.DrawString(arrValueNames(i).ToString, New
Font("Tahoma", 10), Brushes.Black, descLeg)
symbolLeg.Y += 15
descLeg.Y += 15
Next i
'Loop through the values to create the Bar Chart.
For i = 0 To arrValues.Length - 1
objGraphics.FillRectangle(New SolidBrush(GetColor(i)), (i * 35)
+ 15, 200 - arrValues(i), 20, arrValues(i) + 5)
objGraphics.DrawRectangle(Pens.Black, (i * 35) + 15, 200 -
arrValues(i), 20, arrValues(i) + 5)
Next
'Loop through the values to create the Pie Chart.
Dim sglCurrentAngle As Single = 0
Dim sglTotalAngle As Single = 0
i = 0
For i = 0 To arrValues.Length - 1
'Current Value / (sum of all the Values) * 360 degree angle
sglCurrentAngle = arrValues(i) / 550 * 360
objGraphics.FillPie(New SolidBrush(GetColor(i)), 220, 95, 100,
100, sglTotalAngle, sglCurrentAngle)
objGraphics.DrawPie(Pens.Black, 220, 95, 100, 100,
sglTotalAngle, sglCurrentAngle)
sglTotalAngle += sglCurrentAngle
Next i
objBitMap.Save(Response.OutputStream, ImageFormat.Gif)
End Sub
'This function returns a color for the bar and pie charts.
Private Function GetColor(ByVal itemIndex As Integer) As Color
Dim objColor As Color
Select Case itemIndex
Case 0
objColor = Color.Blue
Case 1
objColor = Color.Red
Case 2
objColor = Color.Yellow
Case 3
objColor = Color.Purple
Case 4
objColor = Color.Orange
Case 5
objColor = Color.Brown
Case 6
objColor = Color.Gray
Case 7
objColor = Color.Maroon
Case Else
objColor = Color.Green
End Select
Return objColor
End Function
The values now are all static but I want to be able to pull the values from
a database.
Can you guys give me some ideas to do this?
Thanks
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Declare your object variables
Dim i As Integer
'Build a BitMap that will act as the pallet and container
'for the bar graph. Here 600 is the width and 300 is the height.
'These values could also be passed as parameters.
Dim objBitMap As New Bitmap(400, 200)
'Declare your Graphics objects for painting graphics on you newly
created bitmap.
Dim objGraphics As Graphics
objGraphics = Graphics.FromImage(objBitMap)
'Set the background color to silver
objGraphics.Clear(Color.Silver)
'Build an array of values for the bar and pie chart.
'These values could also be pulled from a database.
' Retrieve Products Table
Dim arrValues(5) As Integer
arrValues(0) = 100
arrValues(1) = 135
arrValues(2) = 115
arrValues(3) = 125
arrValues(4) = 75
arrValues(5) = 300
Dim arrValueNames(5) As String
arrValueNames(0) = "Jan"
arrValueNames(1) = "Feb"
arrValueNames(2) = "Mar"
arrValueNames(3) = "Apr"
arrValueNames(4) = "May"
arrValueNames(5) = "June"
'Write out a title for your bar and pie chart.
objGraphics.DrawString("5 Month Projection Report", New
Font("Tahoma", 16), Brushes.Black, New PointF(5, 5))
'Create a legend to describe your bar and chart.
Dim symbolLeg As PointF = New PointF(335, 20)
Dim descLeg As PointF = New PointF(360, 16)
For i = 0 To arrValueNames.Length - 1
objGraphics.FillRectangle(New SolidBrush(GetColor(i)),
symbolLeg.X, symbolLeg.Y, 20, 10)
objGraphics.DrawRectangle(Pens.Black, symbolLeg.X, symbolLeg.Y,
20, 10)
objGraphics.DrawString(arrValueNames(i).ToString, New
Font("Tahoma", 10), Brushes.Black, descLeg)
symbolLeg.Y += 15
descLeg.Y += 15
Next i
'Loop through the values to create the Bar Chart.
For i = 0 To arrValues.Length - 1
objGraphics.FillRectangle(New SolidBrush(GetColor(i)), (i * 35)
+ 15, 200 - arrValues(i), 20, arrValues(i) + 5)
objGraphics.DrawRectangle(Pens.Black, (i * 35) + 15, 200 -
arrValues(i), 20, arrValues(i) + 5)
Next
'Loop through the values to create the Pie Chart.
Dim sglCurrentAngle As Single = 0
Dim sglTotalAngle As Single = 0
i = 0
For i = 0 To arrValues.Length - 1
'Current Value / (sum of all the Values) * 360 degree angle
sglCurrentAngle = arrValues(i) / 550 * 360
objGraphics.FillPie(New SolidBrush(GetColor(i)), 220, 95, 100,
100, sglTotalAngle, sglCurrentAngle)
objGraphics.DrawPie(Pens.Black, 220, 95, 100, 100,
sglTotalAngle, sglCurrentAngle)
sglTotalAngle += sglCurrentAngle
Next i
objBitMap.Save(Response.OutputStream, ImageFormat.Gif)
End Sub
'This function returns a color for the bar and pie charts.
Private Function GetColor(ByVal itemIndex As Integer) As Color
Dim objColor As Color
Select Case itemIndex
Case 0
objColor = Color.Blue
Case 1
objColor = Color.Red
Case 2
objColor = Color.Yellow
Case 3
objColor = Color.Purple
Case 4
objColor = Color.Orange
Case 5
objColor = Color.Brown
Case 6
objColor = Color.Gray
Case 7
objColor = Color.Maroon
Case Else
objColor = Color.Green
End Select
Return objColor
End Function