如何在填满图形但不是无限的mschart上绘制垂直线? [英] How to draw vertical line on mschart that fills graph but isn't infinite?
问题描述
我正在尝试绘制锚定到一个点的垂直线.我尝试使用固定的Y轴高度绘制线条,但未正确居中.所以现在我有一条无限的线,但是我想要的是这样的线,就像这样填充图形
I am trying to draw a vertical line that is anchored to a point. I tried to use the height of my Y axis which is fixed to draw the line, but it wasn't centered correctly. So right now I have an infinite line, but that I want is the line to just fill the graph like so
VerticalLineAnnotation lineannot = new VerticalLineAnnotation();
lineannot.AnchorDataPoint = chart.Series[item].Points.Last();
lineannot.LineColor = Color.Red;
lineannot.Width = 3;
lineannot.Visible = true;
lineannot.IsInfinitive = true;
chart.Annotations.Add(lineannot);
推荐答案
IsInfinitive
由ClipToChartArea
补充;您可以将行设置为像这样裁剪到ChartArea:
IsInfinitive
is complemented by ClipToChartArea
; you can set the line to be clipped to a ChartArea like this:
lineannot.ClipToChartArea = chart.ChartAreas[item].Name;
假定item
是正确的区域名称或索引.
请注意,ClipToChartArea
使用图表区域的名称!
assuming item
is the right area name or index..
Note that ClipToChartArea
takes the name of the chart area!
这是最简单的方法.
还可以直接控制注释的位置和大小:
It is also possible to control an annotation's position and size directly:
// Note that directly after adding points this will return NaN:
double maxDataPoint = chart1.ChartAreas[0].AxisY.Maximum;
double minDataPoint = chart1.ChartAreas[0].AxisY.Minimum;
LineAnnotation annotation2 = new LineAnnotation();
annotation2.IsSizeAlwaysRelative = false;
annotation2.AxisX = chart1.ChartAreas[0].AxisX;
annotation2.AxisY = chart1.ChartAreas[0].AxisY;
annotation2.AnchorY = minDataPoint;
annotation2.Height = maxDataPoint - minDataPoint;;
annotation2.Width = 0;
annotation2.LineWidth = 2;
annotation2.StartCap = LineAnchorCapStyle.None;
annotation2.EndCap = LineAnchorCapStyle.None;
annotation2.AnchorX = 21; // <- your point
annotation2.LineColor = Color.Pink; // <- your color
chart1.Annotations.Add(annotation2);
这篇关于如何在填满图形但不是无限的mschart上绘制垂直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!