J. Dough
J. Dough

Reputation: 77

VerticalLineAnnotation not displaying on RangeBar MSChart

General Info: C#, WinForms.

I am trying to create somewhat of a timeline, in which I visualize historical and/or planned data for a production process. I do so using a RangeBar styled MSChart. I succeeded in configuring the chart to my personal requirements and filling the chart with correct X and Y values.

When a currently ongoing process is being visualized, I would like to display a vertical line over the chart, corresponding with the current time. Searching through SO has led me to believe that a VerticalLineAnnotation object would be the best way to implement such a visualization. But whatever I try, I can't seem to make the VerticalLineAnnotation appear on screen, no matter which combination of properties I set. Here is the code from my test project, X and Y values and points are meaningless in this context:

ChartArea ca = chart.ChartAreas.Add("CA");
Series s1 = chart.Series.Add("s1");

s1.ChartArea = ca.Name;
s1.ChartType = SeriesChartType.RangeBar;
s1.XValueType = ChartValueType.String;
s1.YValueType = ChartValueType.DateTime;
ca.AxisY.LabelStyle.Format = "HH:mm tt";

ca.AxisY.Maximum = DateTime.ParseExact("20-9-2017 9:20:00", "dd-M-yyyy H:mm:ss", null).ToOADate();
ca.AxisY.Minimum = DateTime.ParseExact("20-9-2017 8:10:00", "dd-M-yyyy H:mm:ss", null).ToOADate();

int pointIndex = chart.Series["s1"].Points.AddXY(2, DateTime.ParseExact("20-9-2017 8:13:45", "dd-M-yyyy H:mm:ss", null).ToOADate(), DateTime.ParseExact("20-9-2017 9:17:05", "dd-M-yyyy H:mm:ss", null).ToOADate() );
chart.Series["s1"].Points.Add(new DataPoint() { AxisLabel = "SO", XValue = 1, YValues = new double[] { DateTime.ParseExact("20-9-2017 8:13:01", "dd-M-yyyy H:mm:ss", null).ToOADate(), DateTime.ParseExact("20-9-2017 8:15:57", "dd-M-yyyy H:mm:ss", null).ToOADate() } });
chart.Series["s1"].Points.Add(new DataPoint() { AxisLabel = "SO", XValue = 1, YValues = new double[] { DateTime.ParseExact("20-9-2017 9:13:01", "dd-M-yyyy H:mm:ss", null).ToOADate(), DateTime.ParseExact("20-9-2017 9:15:57", "dd-M-yyyy H:mm:ss", null).ToOADate() } });

s1.Points[pointIndex].AxisLabel = "Hello";
s1.Points[pointIndex].Color = Color.Blue;

// Please ignore the different Points adding/altering style, I was experimenting a bit in my test project

(chart is created in the Visual Studio Designer and declared as private System.Windows.Forms.DataVisualization.Charting.Chart chart;)

Up until here, my code works as intended and gives the following chart as a result: https://i.sstatic.net/As89y.jpg (Sorry, can't post image directly due to low reputation).

For test purposes, I try to create a VerticalLineAnnotation on 8:45:00 with the following code:

VerticalLineAnnotation LA = new VerticalLineAnnotation();
LA.AxisX = ca.AxisX;
LA.AxisY = ca.AxisY;
LA.Y = DateTime.ParseExact("20-9-2017 8:45:00", "dd-M-yyyy H:mm:ss", null).ToOADate();
LA.LineWidth = 2;
LA.Height = ca.AxisY.Maximum - ca.AxisY.Minimum // Should span entire chart
LA.LineColor = Color.Red;
LA.ClipToChartArea = ca.Name;
chart.Annotations.Add(LA);

The code compiles fine, but the result is the same as the previous image: A RangeBar chart with correct bars in it, but no sign of the VerticalLineAnnotation.

I feel like I'm not setting an important property (I tried setting LA.Visible to true, but it already defaults to true, so it didn't make a difference). I checked the documentation, but that does not seem to suggest anywhere that RangeBar charts are incompatible with VerticalLineAnnotations. I don't know what is left to check...

Upvotes: 2

Views: 1207

Answers (1)

TaW
TaW

Reputation: 54433

Two issues:

1 - This is just a typo (you wanted max - min, right?)..

LA.Height = ca.AxisY.Maximum - ca.AxisY.Maximum // Should span entire chart

But it is also not the recommended way... To span the whole chart area you can use the combination of

LA.IsInfinitive = true;
LA.ClipToChartArea = ca.Name;

2 - You have tried many things but the x value of the annotation, just like y, height and width are by no means irrelevant; You need to set the x position of the line, maybe like this to align it with the start of the 1st point..:

LA.X = chart.Series[0].Points[0].YValues[0];

Upvotes: 1

Related Questions