Stumped by ZedGraph

janneman

New member
Joined
Mar 27, 2009
Messages
4
Programming Experience
1-3
Hi,

I'm just starting to use ZedGraph, and it has me stumped. I have a sub CreateGraph to set up the graph and to ad the first curve. All OK.
Then another sub DrawGraphs to add more curves, and there I get a run-time error (or maybe a compile time error, not sure) as noted in the source below. It says that AddCurve isn't there, but it runs fine in CreateGraph. What am I missing??


VB.NET:
Public Sub CreateGraph(ByVal zgc As ZedGraphControl)
        Dim BiasGraph As GraphPane = zgc.GraphPane
        ' Set the titles and axis labels
        BiasGraph.Title.Text = "Linear Audio Bias Sleuth"
        BiasGraph.XAxis.Title.Text = "mSecs"
        BiasGraph.YAxis.Title.Text = "mAmperes"
        ' Make up some data points from the Sine function
        Dim list = New PointPairList()
        Dim x As Double, y As Double
        For x = 0 To 36
            y = Math.Sin(x * Math.PI / 15.0)
            list.Add(x, y)
        Next x
        ' Generate a blue curve with circle symbols
        Dim BiasCurve As LineItem = BiasGraph.AddCurve("Bias Current", list, Color.Blue, SymbolType.Circle)
        ' Make the symbols opaque by filling them with white
        BiasCurve.Symbol.Fill = New Fill(Color.White)

        ' Fill the axis background with a color gradient
        BiasGraph.Chart.Fill = New Fill(Color.White, Color.LightGoldenrodYellow, 45.0F)

        ' Fill the pane background with a color gradient
        BiasGraph.Fill = New Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0F)

        ' Calculate the Axis Scale Ranges
        'BiasGraph.AxisChange()
        zgBiasGraph.AxisChange()
        zgBiasGraph.Refresh()
    End Sub
    Public Sub DrawGraphs(ByVal BiasGraph)
        ' Make up some data points from the Sine function
        Dim list = New PointPairList()
        Dim x As Double, y As Double
        For x = 18 To 54
            y = Math.Sin(x * Math.PI / 15.0)
            list.Add(x, y)
        Next x
        'CreateGraph(BiasGraph)
        ' Generate a red curve with circle symbols
        'The next line gives a runtime error:
        'AddCurve is not a member of the ZedGraph calss or similar wording...
        Dim PowerCurve As LineItem = BiasGraph.AddCurve("Output Pc", list, Color.Red, SymbolType.Circle)
        ' Calculate the Axis Scale Ranges
        'BiasGraph.AxisChange()
        zgBiasGraph.AxisChange()
        zgBiasGraph.Refresh()
    End Sub

Thanks for any pointers,

Jan Didden
 
"DrawGraphs(ByVal BiasGraph)" this means BiasGraph parameter is implicitly declared as type Object, you have to declare it "As GraphPane".
 
ZedGraph unraveling

John, thank you, great help. I still haven't gotten my head around these various types of declarations I guess. But I'm getting there!

So, at the top level I have:

VB.NET:
Dim zgc As New ZedGraphControl
        Dim BiasGraph As GraphPane = zgc.GraphPane
        CreateGraph(zgBiasGraph)
        DrawGraphs(BiasGraph)
        SetSize()

Then I have the CreateGraph sub:

VB.NET:
Public Sub CreateGraph(ByVal zgc As ZedGraphControl)
        MessageBox.Show("Entering CreateGraph")
        Dim BiasGraph As GraphPane = zgc.GraphPane
     .....
        zgBiasGraph.AxisChange()
        zgBiasGraph.Refresh()

...and the 'add curve' sub:

VB.NET:
Public Sub DrawGraphs(ByVal BiasGraph As GraphPane)
.....
        zgBiasGraph.AxisChange()
        zgBiasGraph.Refresh()

Now if I call DrawGraphs(BiasGraph) from within CreateGraph, all is well, but I need to call DrawGraphs(BiasGraph) from somewhere else in my app and that doesn't work; I do enter the sub DrawGraphs, but the curve isn't visible. It IS visible when I call DrawGraphs from within CreateGraph. No error messages either way.

Jan Didden
 
From where I look I can't see no problems with it either, but then again I have no idea what you're trying to do, what you are referencing and how you are calling things from various places.
 
John,

I'm trying to go step by step.
First I create a pane for the curves to be drawn on, then call the sub that create the initial curve:

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim zgc As New ZedGraphControl
        Dim BiasGraph As GraphPane = zgc.GraphPane
        CreateGraph(BiasGraph) 'line a
        SetSize()
    End Sub

I believe that I created a pane to draw on called BiasGraph, that lives in memory space, and that I can pass to a sub by reference. So, I declare sub CreateGraph as follows:

VB.NET:
Public Sub CreateGraph(ByRef BiasGraph As GraphPane)

... but this does not create the initial graph (with the legends and 1st curve).

However, when I change 'line a above in:

VB.NET:
CreateGraph(zgBiasGraph) 'line a

and then declare the createsub as follows, and do a local dim of BiasGraph:

VB.NET:
Public Sub CreateGraph(ByVal zgc As ZedGraphControl)
        Dim BiasGraph As GraphPane = zgc.GraphPane

.. it does correctly everything I'm doing inside CreateGraph.

What I don't understand is why it doesn't work in the first case where I pass BiasGraph as a reference??

Jan Didden
 
janneman said:
ByRef BiasGraph As GraphPane
All reference types are passed by reference to parameters, use the default ByVal passing convention.
Dim zgc As New ZedGraphControl
Do you display this New ZedGraphControl any time?
zgBiasGraph
I'm guessing "zgBiasGraph" is a reference to the control you display?
What I don't understand is why it doesn't work in the first case where I pass BiasGraph as a reference??
Most likely because you are referring to a pane of a control you haven't displayed.
Look at these two examples that have same functionality and where both calls allow the same pane to be referenced:
VB.NET:
Sub SameCall()
   ' zgBiasGraph here refers to an existing control that is displayed
   CreateGraph(zgBiasGraph.GraphPane)
   CreateGraph(zgBiasGraph)
End Sub

Sub CreateGraph(ByVal graphPane As GraphPane)
   ' do something with the pane
End Sub

Sub CreateGraph(ByVal graphControl As ZedGraphControl)
   Dim graphPane As GraphPane = graphControl.GraphPane
   ' do something with the pane
End Sub
 
John,

I got it! Works like a charm now.

I think I was confused because of my bad choice of names - BiasGraph and zgBiasGraph have really nothing in common although the name suggests it. zgBiasGraph is the pane that is displayed to draw on, and BiasGraph in my subs was the instantiation so that I could reference the various methods to the displayed pane. Could as well have called it PuPuKaka. Am I correct?

Thanks a bunch,

Jan Didden
 
Back
Top