How can I draw a vertical line on mschart that fills the chart but is not infinite?

I am trying to draw a vertical line anchored to a point. I tried using the height of my Y axis, which is fixed to draw the line, but it was not centered correctly. So right now I have an infinite line, but I want the line to just fill the graph like so

http://i.imgur.com/OKdjcdU.png?1

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);

      

+1


source to share


1 answer


IsInfinitive

supplemented ClipToChartArea

; you can set the line to snap to the ChartArea like this:

lineannot.ClipToChartArea = chart.ChartAreas[item].Name; 

      

Assuming item

is the correct area name or index. Note that it ClipToChartArea

takes the name of the chart area!



This is the easiest way to do it.

You can also directly control the position and size of the annotation:

// 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);

      

+3


source







All Articles