|
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.
Dim arrValues(4) As Integer
arrValues(0) = 100
arrValues(1) = 135
arrValues(2) = 115
arrValues(3) = 125
arrValues(4) = 75
Dim arrValueNames(4) As String
arrValueNames(0) = "Jan"
arrValueNames(1) = "Feb"
arrValueNames(2) = "Mar"
arrValueNames(3) = "Apr"
arrValueNames(4) = "May"
'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
|