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 three-dimensional histogram with VB .NET
DescriptionThis example shows how to draw a three-dimensional histogram with VB .NET.
Keywordshistogram, bar chart, graph, VB.NET
CategoriesGraphics, VB.NET
 
The picHisto control's Paint event handler draws the histogram. First it uses scaling transformations to make drawing the values more convenient. It flips the Y coordinate so increasing Y values go upwards. It also sets the horizontal scale so the data's values fit in 1 units increments across the width of the control.

Then for each data value, the program draws a bar. First it makes a semi-transparent brush of a color chosen for that data value. It makes a pen that is a darker version of the same color. It draws the lines that lie behind the bar and then draws the face, top, and front side. The result is what appears to be a translucent solid bar. It's a simple but effective illusion.

 
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) + 2)
    Dim scaley As Single = -picHisto.Height / (MAX_VALUE + _
        10)
    e.Graphics.ScaleTransform( _
        scalex, scaley, MatrixOrder.Append)

    ' Translate so (0, MAX_VALUE + 10) 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 brush_color As Color = _
            Color.FromArgb(&HC0FFFFFF And _
            m_Colors(i).ToArgb())
        Dim the_brush As New SolidBrush(brush_color)
        Dim pen_color As Color = Color.FromArgb(&HC0, _
            m_Colors(i).R / 2, m_Colors(i).G / 2, _
                m_Colors(i).B / 2)
        'Dim the_pen As New Pen(pen_color, 0)
        Dim the_pen As New Pen(Color.Black, 0)

        ' Draw hidden lines.
        e.Graphics.DrawLine(the_pen, CSng(i + 0.5), 0 + 10, _
            CSng(i + 0.5), m_DataValues(i) + 10)
        e.Graphics.DrawLine(the_pen, CSng(i + 0.5), 0 + 10, _
            CSng(i + 1.5), 0 + 10)

        ' Face.
        Dim pts() As PointF
        pts = New PointF() { _
            New PointF(i, 0), _
            New PointF(i + 1, 0), _
            New PointF(i + 1, m_DataValues(i)), _
            New PointF(i, m_DataValues(i)) _
        }
        e.Graphics.FillPolygon(the_brush, pts)
        e.Graphics.DrawPolygon(the_pen, pts)

        ' Top.
        pts = New PointF() { _
            New PointF(i, m_DataValues(i)), _
            New PointF(i + 0.5, m_DataValues(i) + 10), _
            New PointF(i + 1.5, m_DataValues(i) + 10), _
            New PointF(i + 1, m_DataValues(i)) _
        }
        e.Graphics.FillPolygon(the_brush, pts)
        e.Graphics.DrawPolygon(the_pen, pts)

        ' Side.
        pts = New PointF() { _
            New PointF(i + 1, 0), _
            New PointF(i + 1, m_DataValues(i)), _
            New PointF(i + 1.5, m_DataValues(i) + 10), _
            New PointF(i + 1.5, 10) _
        }
        e.Graphics.FillPolygon(the_brush, pts)
        e.Graphics.DrawPolygon(the_pen, pts)

        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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated