问题
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?
回答1:
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.
回答2:
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);
来源:https://stackoverflow.com/questions/11795141/zedgraph-vertical-lines-with-lineobj-issue