|
-
Dec 1st, 2011, 03:57 PM
#1
Thread Starter
Lively Member
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.
-
Dec 1st, 2011, 04:05 PM
#2
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 -
-
Dec 1st, 2011, 04:11 PM
#3
Thread Starter
Lively Member
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?
-
Dec 1st, 2011, 04:19 PM
#4
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 -
-
Dec 1st, 2011, 05:00 PM
#5
Thread Starter
Lively Member
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,
-
Dec 1st, 2011, 05:12 PM
#6
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 -
-
Dec 2nd, 2011, 03:02 AM
#7
Thread Starter
Lively Member
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,
-
Dec 2nd, 2011, 04:45 AM
#8
Thread Starter
Lively Member
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:
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 9
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"
'
myPane.CurveList.Clear()
' Generate a blue curve
Dim myCurve As LineItem = myPane.AddCurve("Flow Meter A", alist, Color.Blue, SymbolType.None)
' Calculate the Axis Scale Ranges
zgc.AxisChange()
SetSize()
zg1.Refresh()
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
alist = New PointPairList()
For x = 10 To 19
y = Math.Abs(Math.Sin(x * x + 100))
alist.Add(x, y)
Next x
zgc = New ZedGraphControl
CreateGraph(zg1)
SetSize()
End Sub
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,
-
Dec 2nd, 2011, 10:07 AM
#9
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 -
-
Dec 2nd, 2011, 04:55 PM
#10
Thread Starter
Lively Member
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:
Imports ZedGraph
Public Class Form1
Private inhibit_scroll As Boolean = False
Private x As Double = 0.0, y As Double = 0.0
Private alist = New PointPairList()
Private zgc = New ZedGraphControl
Dim maxX As Double = 9.0, maxY As Double = 8000.0, minX As Double = 0.0, minY As Double = 0.0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' CreateGraph(zgc)
End Sub
Private Sub CreateGraph(ByVal zgc As ZedGraphControl)
Dim myPane As GraphPane = zgc.GraphPane
myPane.XAxis.Scale.Max = maxX
myPane.XAxis.Scale.Min = minX
myPane.YAxis.Scale.Max = maxY
myPane.YAxis.Scale.Min = minY
' Set the titles and axis labels
myPane.Title.Text = "Flow Meter A"
myPane.XAxis.Title.Text = "Time"
myPane.YAxis.Title.Text = "Kg/Hr"
myPane.CurveList.Clear()
' Generate a blue curve
Dim myCurve As LineItem = myPane.AddCurve("Flow Meter A", alist, Color.Red, SymbolType.None)
myPane.Chart.Fill = New Fill(Color.Black, Color.Gray)
' Calculate the Axis Scale Ranges
zgc.AxisChange()
'zgc.Invalidate()
zg1.Refresh()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = x
Label2.Text = maxX
x = x + 0.1
y = (3800 * Math.Sin(0.5 * x)) + 4000
alist.add(x, y)
' zgc = New ZedGraphControl
If Not inhibit_scroll Then
CreateGraph(zg1)
End If
' if the graph has reached the end of the trace, then start to advance the axis.
If x > maxX Then
minX = minX + 0.1
maxX = maxX + 0.1
End If
End Sub
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
inhibit_scroll = True
End Sub
Private Sub zg1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zg1.DoubleClick
inhibit_scroll = False
End Sub
End Class
-
Dec 3rd, 2011, 07:55 AM
#11
Thread Starter
Lively Member
Re: Zedgraph Real Time
i've sorted the scroll, using :
scrollMaxX = (my max x axis)
scrollMinX = 0
any thoughts on my code, as above?
-
Dec 3rd, 2011, 04:58 PM
#12
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|