Visual Basic: Make a simple graph using ZedGraphControl

Platform:Visual Basic
Task:Make a simple graph using ZedGraphControl
Discussion:
This is a silly form I made to test out the ZedGraphControl
Example:
Imports ZedGraph
Public Class Form1
    Dim MyPointPairList As New PointPairList 'the list of points to be graphed
    Dim MyGraphPane As GraphPane 'the graphpane

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'create the graph
        CreateGraph(Me.WeightZedGraphControl)
    End Sub

    ''' <summary>
    ''' Creates a simple ZedGraph and populates it with some points
    ''' </summary>
    ''' <param name="ZedGraphControl">The ZedGraphControl to populate</param>
    ''' <remarks>Creates a simple ZedGraph and populates it with some points</remarks>
    Private Sub CreateGraph(ByVal ZedGraphControl As ZedGraphControl)
        'get a reference to the GraphPane
        MyGraphPane = ZedGraphControl.GraphPane

        'clear the curvelist so that if this is a refresh it won't put another graph over the top of the first
        MyGraphPane.CurveList.Clear()

        'titles
        MyGraphPane.Title.Text = "Skeeter's Test Graph"
        MyGraphPane.XAxis.Title.Text = "My X Axis"
        MyGraphPane.YAxis.Title.Text = "My Y Axis"

        'generate some points to graph and add them to the point pair list
        For i As Integer = 1 To 222
            MyPointPairList.Add(i, i + GetRandomNumber(1, 100))
        Next

        'build a curve and add it to the graph pane
        Dim MyCurve As LineItem = MyGraphPane.AddCurve("Skeeter", MyPointPairList, Color.Red, SymbolType.Diamond)
        'don't know what this does but the map won't draw without it
        ZedGraphControl.AxisChange()

        'refresh the map
        ZedGraphControl.Refresh()
    End Sub

    Public Function GetRandomNumber(ByVal Min As Integer, ByVal Max As Integer) As Integer
        Static Generator As System.Random = New System.Random()
        Return Generator.Next(Min, Max)
    End Function

    Private Sub AddAPointToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddAPointToolStripButton.Click
        AddAPoint()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        AddAPoint()
    End Sub

    Private Sub AddAPoint()
        MyPointPairList.Add(MyPointPairList.Count + 1, Now.Second)
        WeightZedGraphControl.Refresh()
        WeightZedGraphControl.RestoreScale(MyGraphPane)
    End Sub

    Private Sub StartTimeToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartTimeToolStripButton.Click
        Timer1.Interval = 500
        If Timer1.Enabled = True Then
            Timer1.Enabled = False
        Else
            Timer1.Enabled = True
        End If
    End Sub
End Class
Back to Index