ZedGraph vertical lines with LineObj issue

Viewed 5510

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?

2 Answers

You were on the good way using a LineObj rather than a CurveItem,.

Have a look on the Location struct and the CoordinateFrame property. It allows to use a different coordinate system for X and/or Y.

Setting the CoordinateFrame to XScaleYChartFraction allows to use 0d and 1d as Y, which means "the bottom" and "the top" of the graph pane (instead of YAxis.Scale.Min and YAxis.Scale.Max), as X continues to use the X Axis scale coordinate system.

That means you can use .AxisChange(), zoom, pan, and the LineObj will not interfer with the scale changes of the Y axis !

var line = new LineObj(Color.Black, xPos, 0, xPos, 1);

line.Location.CoordinateFrame = XScaleYChartFraction; // This do the trick !
line.IsClippedToChartRect = true;

line.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
line.Line.Width = 1f;

myPane.GraphObjList.Add(line);
Related