Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw a simple histogram
Keywordshistogram, graph
CategoriesGraphics
 
Set a PictureBox's Scale properties so the values range from 0 to the number of histogram values horizontally and from 0 to the maximum data value vertically. Then for the ith bar, draw a box from (i - 1, 0) to (i, data_value(i)).
 
Private Sub DrawHistogram(ByVal pic As PictureBox, _
    data_values() As Single, ByVal min_value As Single, _
    ByVal max_value As Single)
Dim i As Integer

    ' Set convenient data bounds.
    pic.ScaleLeft = LBound(data_values) - 1
    pic.ScaleWidth = UBound(data_values) - _
        LBound(data_values) + 1
    pic.ScaleTop = max_value
    pic.ScaleHeight = min_value - max_value

    ' Draw the data.
    For i = LBound(data_values) To UBound(data_values)
        pic.Line (i - 1, 0)-(i, data_values(i)), QBColor(i _
            Mod 16), BF
    Next i
End Sub
 
When the user clicks on the histogram, use the X coordinate to see which value is selected and display the value.
 
' Display this value.
Private Sub picHistogram_MouseDown(Button As Integer, Shift _
    As Integer, X As Single, Y As Single)
Dim i As Integer

    i = Int(X) + 1
    MsgBox "Value " & i & " = " & m_DataValues(i)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated