I am building a proof of concept to make a bar graph. I built in a yConversion factor to bring the bars closer to the top of the garph so it looks nicer. The following value of
arrValues(0) = 375
makes the graph look as I would expect, if I chnage it to
arrValues(0) = 175
the graph does not display correctly and I cannot find a correlation.
It is a just windows form for with OnPaint Overridden.
Any help or input appreciated.
Marty
arrValues(0) = 375
makes the graph look as I would expect, if I chnage it to
arrValues(0) = 175
the graph does not display correctly and I cannot find a correlation.
It is a just windows form for with OnPaint Overridden.
Any help or input appreciated.
Marty
Code:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim arrValues(10) As Int32
arrValues(0) = 375
arrValues(1) = 135
arrValues(2) = 115
arrValues(3) = 125
arrValues(4) = 175
arrValues(5) = 100
arrValues(6) = 135
arrValues(7) = 115
arrValues(8) = 125
arrValues(9) = 75
arrValues(10) = 100
Dim arrValueNames(10) As String
arrValueNames(0) = "Jan"
arrValueNames(1) = "Feb"
arrValueNames(2) = "Mar"
arrValueNames(3) = "Apr"
arrValueNames(4) = "May"
arrValueNames(5) = "Jun"
arrValueNames(6) = "Jul"
arrValueNames(7) = "Aug"
arrValueNames(8) = "Sep"
arrValueNames(9) = "Oct"
arrValueNames(10) = "Nov"
Dim yConversion As Int32
Dim maxValue As Int32 = GetMaxValue(arrValues)
yConversion = (Me.Height - 30) / maxValue
Dim recWidth As Int32 = Convert.ToInt32(Me.Width / (arrValues.Length * 2))
Dim recHeight As Int32
Dim xCord As Int32
Dim yCord As Int32
Dim i As Int32
For i = 0 To arrValues.Length - 1
xCord = recWidth * i * 2
'if it is the max value do not need conversion factor
If maxValue = arrValues(i) Then
recHeight = arrValues(i)
yCord = Me.Height - arrValues(i)
Else
recHeight = Me.Height - arrValues(i)
yCord = Me.Height - (arrValues(i) * yConversion)
End If
g.DrawRectangle(Pens.Black, xCord, yCord, recWidth, recHeight)
g.FillRectangle(New SolidBrush(GetColor(i)), xCord, yCord, recWidth, recHeight)
g.DrawString(arrValueNames(i) & " " & arrValues(i), New Font("Tahoma", 10), New SolidBrush(Color.Black), recWidth * i * 2, Me.Height - (arrValues(i) * yConversion) + 10)
Next i
End Sub
Private Function GetColor(ByVal itemIndex As Integer) As Color
Dim gColor As Color
itemIndex = itemIndex Mod 9
Select Case itemIndex
Case 0
gColor = Color.Blue
Case 1
gColor = Color.Red
Case 2
gColor = Color.Yellow
Case 3
gColor = Color.Purple
Case 4
gColor = Color.Orange
Case 5
gColor = Color.Brown
Case 6
gColor = Color.Gray
Case 7
gColor = Color.Maroon
Case Else
gColor = Color.Green
End Select
Return gColor
End Function
Private Function GetMaxValue(ByVal arrValues As Int32()) As Int32
Dim maxValue As Single = Single.MinValue
Dim i As Int32
For i = 0 To arrValues.Length - 1
If arrValues(i) > maxValue Then
maxValue = arrValues(i)
End If
Next i
Return maxValue
End Function