Bar Graph Plotter

 

Today I encountered another question about VB in Yahoo!Answer section that goes like this:

"How do I make bar graph in VB 6.0?

I want to make a bar graph output in VB 6.0. the values will be given from text field. Please help me."

 

I came out with a program that can solve the above problem.
 

 

Private Sub Command1_Click()
Dim y1, y2, y3, y4 As Single

y1 = Val(Txt_y1.Text)
y2 = Val(Txt_y2.Text)
y3 = Val(Txt_y3.Text)
y4 = Val(Txt_y4.Text)

Picture1.Line (100, 6000)-(1100, 6000 - y1 * 1000), vbRed, BF
Picture1.Line (1200, 6000)-(2200, 6000 - y2 * 1000), vbRed, BF
Picture1.Line (2300, 6000)-(3300, 6000 - y3 * 1000), vbRed, BF
Picture1.Line (3400, 6000)-(4400, 6000 - y4 * 1000), vbRed, BF

End Sub

Private Sub Command2_Click()
Txt_y1.Text = ""
Txt_y2.Text = ""
Txt_y3.Text = ""
Txt_y4.Text = ""

Picture1.Cls

End Sub
 

 

The formula to draw rectangle is

Line(x1,y1)-(x2,y20),color,BF

where color can be defined using name constant VbRed, VbGreen and etc.

The coordinates of the four corners of the rectangle is
(x1,y1),(x2,y1),(x1,y2) and (x2,y2)

The point (0,0) is the top left corner of the form or screen.
So, in order to draw the bar graph from bottom to top, we have to start with a point near the bottom edge of the form. Let say the height of the form is 7000 twips, so we can start somewhere like 6000 twips. Then we accept input from the users, let say y1,y2,y3 and y4, which take values from 0 to 6 (example only), we then multiply them 1000, and use 6000-y to get the bar height, it is like working the reverse way from normal coordinate system(where the origin starts from the bottom left corner) .

 

 

 

 

 

 

 [Back to VBToday]