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 in VB .NET
DescriptionThis example shows how to draw a simple histogram in VB .NET.
Keywordshistogram, graph, bar chart, bar graph
CategoriesGraphics, VB.NET
 
When the program loads, it generates some random data.
 
Private Const MIN_VALUE As Single = 0
Private Const MAX_VALUE As Single = 100

Private m_DataValues(9) As Single

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim rnd As New Random

    ' Create data.
    For i As Integer = 0 To m_DataValues.GetUpperBound(0)
        m_DataValues(i) = rnd.Next(MIN_VALUE + 5, MAX_VALUE _
            - 5)
    Next i
End Sub
 
The picHisto PictureBox's Paint event handler draws the histogram. It applies a scaling transformation to the Graphics object so the graph will fit the PictureBox vertically and horizontally. It then translates the Graphics object to position the upper left data point in the upper left corner of the control.

The program then loops over the data points, drawing their histogram bars. Nots that it uses a pen of thickness 0, which gives the thinnest possible line. If you use a pen with thickness 1, the Graphics object scales the pen (try it).

 
Private Sub picHisto_Paint(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.PaintEventArgs) Handles _
    picHisto.Paint
    e.Graphics.Clear(picHisto.BackColor)

    ' Flip vertically and scale to fit.
    Dim scalex As Single = picHisto.Width / _
        (m_DataValues.GetUpperBound(0) + 1)
    Dim scaley As Single = -picHisto.Height / MAX_VALUE
    e.Graphics.ScaleTransform( _
        scalex, scaley, MatrixOrder.Append)

    ' Translate so (0, MAX_VALUE) maps to the origin.
    e.Graphics.TranslateTransform(0, picHisto.Height, _
        MatrixOrder.Append)

    ' Draw the histogram.
    For i As Integer = 0 To m_DataValues.GetUpperBound(0)
        Dim rect As New Rectangle(i, 0, 1, m_DataValues(i))
        Dim clr As Color = Color.FromArgb(&HFF000000 Or _
            QBColor(i Mod 16))
        Dim the_brush As New SolidBrush(clr)
        Dim the_pen As New Pen(Color.Black, 0)
        e.Graphics.FillRectangle(the_brush, rect)
        e.Graphics.DrawRectangle(the_pen, rect)
        the_brush.Dispose()
        the_pen.Dispose()
    Next i

    e.Graphics.ResetTransform()
    e.Graphics.DrawRectangle(Pens.Black, 0, 0, _
        picHisto.Width - 1, picHisto.Height - 1)
End Sub
 
When the user clicks on the graph, the program determines which histogram bar is under the mouse and displays the corresponding data value.
 
Private Sub picHisto_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picHisto.MouseDown
    ' Determine which data value was clicked.
    Dim bar_wid As Single = picHisto.Width / _
        (m_DataValues.GetUpperBound(0) + 1)
    Dim i As Integer = Int(e.X / bar_wid)
    MessageBox.Show(i & ": " & m_DataValues(i), "Value", _
        MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated