What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleDraw a continuous graph that starts and stops in VB .NET
DescriptionThis example shows how to draw a continuous graph that starts and stops in VB .NET.
KeywordsVB .NET, graph, graphing
CategoriesVB.NET, Graphics
When the program starts, it draws grid lines for the graph. When the user clicks the Graph button, the program calls subroutine DrawGraph. That routine loops for 5 seconds, using subroutine NewValue to generate a new value and the calling PlotValue to plot it.
' Start drawing the graph.
Private Sub btnGraph_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGraph.Click
    btnGraph.Text = "Stop"


    btnGraph.Text = "Start"
End Sub

' Draw a graph for 5 seconds.
Private Sub DrawGraph()
    ' Generate pseudo-random values.
    Dim y As Integer = m_Y
    Dim stop_time As Date = Now.AddSeconds(5)
    Do While Now < stop_time
        ' Generate the next value.

        ' Plot the new value.
        PlotValue(y, m_Y)
        y = m_Y
End Sub
Subroutine NewValue pauses for 20 milliseconds and then randomly generates a data value.
' Generate the next value.
Private Sub NewValue()
    ' Delay a bit before calculating the value.
    Dim stop_time As Date = Now.AddMilliseconds(20)
    Do While Now < stop_time

    ' Calculate the next value.
    Static rnd As New Random
    m_Y += rnd.Next(-4, 5)
    If m_Y < 0 Then m_Y = 0
    If m_Y >= picGraph.ClientSize.Height - 1 Then m_Y = _
        picGraph.ClientSize.Height - 1
End Sub
' Plot a new value. Private Sub PlotValue(ByVal old_y As Integer, ByVal new_y As Integer) ' Make the Bitmap and Graphics objects. Dim wid As Integer = picGraph.ClientSize.Width Dim hgt As Integer = picGraph.ClientSize.Height Dim bm As New Bitmap(wid, hgt) Dim gr As Graphics = Graphics.FromImage(bm)

' Move the old data one pixel to the left. gr.DrawImage(picGraph.Image, -1, 0)

' Erase the right edge and draw guide lines. gr.DrawLine(Pens.Blue, wid - 1, 0, wid - 1, hgt - 1) For i As Integer = m_Ymid To picGraph.ClientSize.Height Step GRID_STEP gr.DrawLine(Pens.LightBlue, wid - 2, i, wid - 1, i) Next i For i As Integer = m_Ymid To 0 Step -GRID_STEP gr.DrawLine(Pens.LightBlue, wid - 2, i, wid - 1, i) Next i

' Plot a new pixel. gr.DrawLine(Pens.White, wid - 2, old_y, wid - 1, new_y)

' Display the result. picGraph.Image = bm picGraph.Refresh()

gr.Dispose() End Sub

Subroutine PlotValue uses the Graphics object's DrawImage
    ' method to move the current graph one pixel to the
    ' left. It then draws the new value.
Notes: This example uses a Timer to update a label holding the current time. Note that this stops while the DrawGraph subroutine is running.

Compare this example to Draw a continuous graph in a separate thread in VB .NET. This example uses a separate PlotValue subroutine rather than plotting values as they are generated because this better fits the model used by the other example.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.