Results 1 to 12 of 12

Thread: Zedgraph Real Time

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    104

    Zedgraph Real Time

    Hi,

    I'm wanting to do a real time graph, as a test i'm going to set up a timer to increment x and y values and then plot them every second on the graph.

    I've got hold of Zedgraph, which is quite a nice add-in that i came across, does anyone have experience of doing this?

    Thanks in advance,

    Aaron.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Zedgraph Real Time

    I have some experience with zedgraph.... Just post your questions and I believe many of us here can help you find the answers you're looking for.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    104

    Re: Zedgraph Real Time

    Ok, here goes,

    I'm using the sample that comes with the download, and i see that to put points on the graph you use the list.add(x,y) command. so i want to be able to add in some points, and then some time after add some more and the previous data still be there, advancing the x axis. how do i do this?

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Zedgraph Real Time

    You create a sub and pass in the points to be graphed. In this sub, you add the point to your graph pointpairlist. The next time when you wnat to add more points to the graph, you call the sub again and pass in the new points. The existing points remain there unless you clear out the pointpairlist.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    104

    Re: Zedgraph Real Time

    I've got a form with a Zedgraph and one button. The form loads up and populates the first 20 values (0 thru 19). The intention is the button will then add to the list the next 20 values (20 thru 39). This is where i'm heading but what actually happens is each click of the button re-creates another curve on the graph as another data series, i don't want that. i can't figure out how to declare the 'mypane' object outside of the creategraph sub.

    here's my code:

    Code:
     Imports ZedGraph
    Public Class Form1
        Private x As Double, y As Double
        Private alist = New PointPairList()
        Private zgc = New ZedGraphControl
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For x = 0 To 19
                y = Math.Abs(Math.Sin(x * x + 100))
                alist.Add(x, y)
            Next x
            CreateGraph(zg1)
        End Sub
    
        Private Sub CreateGraph(ByVal zgc As ZedGraphControl)
            Dim myPane As GraphPane = zgc.GraphPane
            ' Set the titles and axis labels
            myPane.Title.Text = "Flow Meter A"
            myPane.XAxis.Title.Text = "Time"
            myPane.YAxis.Title.Text = "Kg/Hr"
            ' Generate a blue curve 
            Dim myCurve As LineItem = myPane.AddCurve("Flow Meter A", alist, Color.Blue, SymbolType.None)
            ' Calculate the Axis Scale Ranges
            zg1.Refresh()
            zgc.AxisChange()
            SetSize()
        End Sub
    
        Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
            SetSize()
        End Sub
    
        Private Sub SetSize()
            zg1.Location = New Point(10, 10)
            ' Leave a small margin around the outside of the control
            zg1.Size = New Size(ClientRectangle.Width - 20, ClientRectangle.Height - 20)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For x = 20 To 39
                y = Math.Abs(Math.Sin(x * x + 100))
                alist.Add(x, y)
            Next x
            CreateGraph(zg1)
        End Sub
    End Class

    thanks,

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Zedgraph Real Time

    You need to clear the curve list before adding a new curve to the pane.
    In the CreateGraph sub, add this line before the line that adding the curve
    Code:
    '......
    myPane.CurveList.Clear()
    ' Generate a blue curve 
    Dim myCurve As LineItem = myPane.AddCurve("Flow Meter A", alist, Color.Blue, SymbolType.None)
    '.....
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    104

    Re: Zedgraph Real Time

    Adding that line in has stopped adding extra series's to the top of the chart. but, it still functions as before.

    when you load up the form, the first (0-19) points go in with the x axis ends at 25.

    first click of the button then adds in points (20-25) but the x axis still ends at 25. unless you minimise the graph, at which point it ends at 50 with the extra data.

    have i got the refresh in the wrong place?

    thanks,

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    104

    Re: Zedgraph Real Time

    ignore last post, i've moved on from there.

    ok, i've got the following code, which is useful.

    vb Code:
    1. Imports ZedGraph
    2. Public Class Form1
    3.     Private x As Double, y As Double
    4.     Private alist = New PointPairList()
    5.     Private zgc = New ZedGraphControl
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         For x = 0 To 9
    8.             y = Math.Abs(Math.Sin(x * x + 100))
    9.             alist.Add(x, y)
    10.         Next x
    11.         CreateGraph(zg1)
    12.     End Sub
    13.     Private Sub CreateGraph(ByVal zgc As ZedGraphControl)
    14.         Dim myPane As GraphPane = zgc.GraphPane
    15.         ' Set the titles and axis labels
    16.         myPane.Title.Text = "Flow Meter A"
    17.         myPane.XAxis.Title.Text = "Time"
    18.         myPane.YAxis.Title.Text = "Kg/Hr"
    19.         '
    20.         myPane.CurveList.Clear()
    21.         ' Generate a blue curve
    22.         Dim myCurve As LineItem = myPane.AddCurve("Flow Meter A", alist, Color.Blue, SymbolType.None)
    23.         ' Calculate the Axis Scale Ranges
    24.         zgc.AxisChange()
    25.         SetSize()
    26.         zg1.Refresh()
    27.     End Sub
    28.     Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    29.         SetSize()
    30.     End Sub
    31.     Private Sub SetSize()
    32.         zg1.Location = New Point(10, 10)
    33.         ' Leave a small margin around the outside of the control
    34.         zg1.Size = New Size(ClientRectangle.Width - 20, ClientRectangle.Height - 20)
    35.     End Sub
    36.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    37.         alist = New PointPairList()
    38.         For x = 10 To 19
    39.             y = Math.Abs(Math.Sin(x * x + 100))
    40.             alist.Add(x, y)
    41.         Next x
    42.         zgc = New ZedGraphControl
    43.         CreateGraph(zg1)
    44.         SetSize()
    45.     End Sub
    46. End Class

    what this does is when the form loads up, the values 0-9 get put on the graph, with the x axis starting at 0. which is good.

    the button is then clicked to load in the next values as a new list so as to get rid of the last set. which it does, but it starts the x axis at 8 but the first value is 10, so there's a gap. how can i set the starting point of the graph?

    thanks,

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Zedgraph Real Time

    1. Get rid of the SetSize sub all together. You can achieve the same effect by setting the zedgraph control's Anchor property.
    2. Use the same pointpairlist you use the last time if you want the data (points) to remain... Don't create a new one.
    3. In the button click event handler, after you add more points to the pointpairlist, you need to call AxisChanges() to force the graph to recalculate its drawing and then call the Refresh method to update the UI to the new graph.
    4. Don't cal createGraph again unless you want a brand new graph. If you just need to modify the data points on an existing graph, you manipulate the pointpairlist by adding/removing point from it and then tell the zedgraph control to recalculate the drawing as I mentioned in #3.
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For x = 10 To 19
                y = Math.Abs(Math.Sin(x * x + 100))
                alist.Add(x, y)
            Next x
            zg1.AxisChange()
            zg1.Refresh()
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    104

    Re: Zedgraph Real Time

    right, i've got a good little real time graph which is just being updated by a timer event putting a sine wave onto the graph. i've set up a boolean to control when I start to scroll back in time, which stops updating the graph, but still updates the list, so when i double click the graph, it starts back up again. the only niggle i have with this is that when i scroll back in time, i can't scroll forwards because the scroll bar grows and doesn't let me.

    here's my code, but thanks for the tips, as you can see, i can't seem to get the graph going without using the 'creategraph' sub. i tried doing it on form load, and then only using the refresh and axischange, but to no avail.?

    even if this as far as i can go i still really like Zedgraph!

    vb Code:
    1. Imports ZedGraph
    2. Public Class Form1
    3.     Private inhibit_scroll As Boolean = False
    4.     Private x As Double = 0.0, y As Double = 0.0
    5.     Private alist = New PointPairList()
    6.     Private zgc = New ZedGraphControl
    7.     Dim maxX As Double = 9.0, maxY As Double = 8000.0, minX As Double = 0.0, minY As Double = 0.0
    8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9.         ' CreateGraph(zgc)
    10.     End Sub
    11.     Private Sub CreateGraph(ByVal zgc As ZedGraphControl)
    12.         Dim myPane As GraphPane = zgc.GraphPane
    13.         myPane.XAxis.Scale.Max = maxX
    14.         myPane.XAxis.Scale.Min = minX
    15.         myPane.YAxis.Scale.Max = maxY
    16.         myPane.YAxis.Scale.Min = minY
    17.         ' Set the titles and axis labels
    18.         myPane.Title.Text = "Flow Meter A"
    19.         myPane.XAxis.Title.Text = "Time"
    20.         myPane.YAxis.Title.Text = "Kg/Hr"
    21.         myPane.CurveList.Clear()
    22.         ' Generate a blue curve
    23.         Dim myCurve As LineItem = myPane.AddCurve("Flow Meter A", alist, Color.Red, SymbolType.None)
    24.         myPane.Chart.Fill = New Fill(Color.Black, Color.Gray)
    25.         ' Calculate the Axis Scale Ranges
    26.         zgc.AxisChange()
    27.         'zgc.Invalidate()
    28.         zg1.Refresh()
    29.     End Sub
    30.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    31.         Label1.Text = x
    32.         Label2.Text = maxX
    33.         x = x + 0.1
    34.         y = (3800 * Math.Sin(0.5 * x)) + 4000
    35.         alist.add(x, y)
    36.         ' zgc = New ZedGraphControl
    37.         If Not inhibit_scroll Then
    38.             CreateGraph(zg1)
    39.         End If
    40.         ' if the graph has reached the end of the trace, then start to advance the axis.
    41.         If x > maxX Then
    42.             minX = minX + 0.1
    43.             maxX = maxX + 0.1
    44.         End If
    45.     End Sub
    46.     Private Sub zg1_ScrollProgressEvent(ByVal sender As ZedGraph.ZedGraphControl, ByVal scrollBar As System.Windows.Forms.ScrollBar, ByVal oldState As ZedGraph.ZoomState, ByVal newState As ZedGraph.ZoomState) Handles zg1.ScrollProgressEvent
    47.         inhibit_scroll = True
    48.     End Sub
    49.     Private Sub zg1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zg1.DoubleClick
    50.         inhibit_scroll = False
    51.     End Sub
    52. End Class

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    104

    Re: Zedgraph Real Time

    i've sorted the scroll, using :

    scrollMaxX = (my max x axis)
    scrollMinX = 0

    any thoughts on my code, as above?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Guernsey, UK
    Posts
    104

    Re: Zedgraph Real Time

    i've got a good real time graph now, thanks. only thing is, i'd like to put a time stamp on the x axis as it goes along, do you have any hints on this? thanks,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width