Search code examples
c#.netgraphzedgraph

ZedGraph vertical lines with LineObj issue


I have a ZedGraphControl with a few curves in it and I want to add vertical lines at some fixed x-positions. The lines should of course only be inside the actual graph area.

I tried following

LineObj line = new LineObj(Color.Black, xPos, myPane.YAxis.Scale.Min, xPos, myPane.YAxis.Scale.Max);  
line.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
line.Line.Width = 1f;
myPane.GraphObjList.Add(line);

and this works fine until the user zooms the graph, the vertical lines will then stretch out of the actual graph area (see pic link below, also notice that it is not dashed inside the graph, odd).

http://imageshack.us/photo/my-images/196/zedgraphzoom.png/

Is there a way to solve this (if only there was a a way to get myPane.Xaxis.Scale.Min and Max of the current zoom and then update the graph in the ZoomEvent?) or are there any better classes/methods to use other than LineObj for this purpose?


Solution

  • Instead of defining a LineObj, define a LineItem and add it to the GraphPane.CurveList:

    LineItem line = new LineItem(String.Empty, new[] { xPos, xPos },
                    new[] { myPane.YAxis.Scale.Min, myPane.YAxis.Scale.Max }, 
                    Color.Black, SymbolType.None);
    line.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
    line.Line.Width = 1f;
    
    myPane.CurveList.Add(line);
    

    This binds line to the coordinate system in the graph pane, so that when you zoom or pan the line position will still be confined in the graph. Of course, if you zoom out without updating the y values of line, the line ends will be inside the graph.

    I know from personal experience that dashing can be a problem in Zedgraph; however it seems like dashing is properly displayed when adding a LineItem, though.