' 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.Refresh()
DrawGraph()
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.
NewValue()
' Plot the new value.
PlotValue(y, m_Y)
y = m_Y
Loop
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
|