ZedGraph vertical lines with LineObj issue
Asked Answered
R

2

6

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?

Ringleader answered 3/8, 2012 at 11:48 Comment(0)
B
4

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.

Bubonocele answered 3/8, 2012 at 12:12 Comment(2)
Works well as long as you dont call zedGraphControl.AxisChange() afterwords, in which case it will add margin at the bottom, but I guess you can do everything that requires that call before adding the lines. Thank you!Ringleader
One solution is to create a secondary Y axis, then put its scale.max and min to both 0 and 1, then attach the LineItem to it with two points (x, 0) and (x, 1). This way, it remains independant from the other axis.Hibben
H
3

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);
Hibben answered 11/11, 2013 at 19:49 Comment(2)
This seems like a much better way to do it than adding a LineItem, because the vertical lines are conceptually different than data curves.Jerboa
My quest is finally over.Carmella

© 2022 - 2024 — McMap. All rights reserved.