Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

graphics DrawRectangle Bar Graph format problem

Status
Not open for further replies.

cappmgr

Programmer
Jan 29, 2003
639
US
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
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
 
Just out of curiocity, are you redrawing the background? if not, changing the height from 350 to 170 would not appear to change. While changing from 170 to 350 would.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Rick,
No I am not redrawing the background. The code I posted is all the code I have written. I am in uncharted water. This is my first shot at any kind of graphics. If you can tell me or point me in the right direction that would be much appreciated.
Marty
 
I haven't tries you code, so I can't say for sure, but what I beleive may be you issue is that the old bar rectangle is not being cleared before the new bar rectangle is drawn.

try filling a rectangle the size of the old value (375) with the color of the form.backcolor over the bar prior to drawing the new bar (175)

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Rick,
It's my conversion formula. I will get it working and post.
Thank you,
Marty
 
I think its in your GetMaxValue function. You are comparing every element in your array and setting it, so it will always be the last element.

To get the highest number in your array, you should look at Array.Sort()

Also, since you don't seem to be drawing y-axis labels, I would approach this a little bit simpler.

If you decide your tallest bar is 100 pixels, then use a percentage of the others to designate their pixels. For example:
TheHeight = (TheValue/MaxValue) * 100
 
Rick,
Thank you very much, I converted all x and y values to single and the conversion factor to a single and all is fine.
Replaced
Code:
       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
with
Code:
recHeight = arrValues(i) * yConversion
yCord = Me.Height - (arrValues(i) * yConversion)
And it works fine.
I should not have used Int32 I believe it was rounding and throwing things off.
Thanks again,
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top