ZedGraph vertical lines with LineObj issue

荒凉一梦 提交于 2019-12-05 09:10:49

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.

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!