Sunday, April 3, 2011

Using "Microsoft Chart Controls for .NET 3.5" for WinForms, how can I mark certain dates with a grid mark and label?

I'm using the Microsoft Chart Controls, and displays data with dates along the X axis and want to have a grid line with a different color on some dates.

Let's say I display data for one week with 7 values along the X-axis:

05.04.09 06.04.09 07.04.09 08.04.09 09.04.09 10.04.09 11.04.09

In addition I have a collection of DateTimes and names for some events, where one is on 07.04.09 and another is on 10.04.09. I then want to have a vertical grid line drawn on each of these dates and a label added with the event name.

I just can't figure out how the heck I can achieve this. Any help appreciated.

From stackoverflow
  • As far as I know you cannot change the color of specific lines in the chart grid. A tentative solution might be to add a new chart series of type Column; the series values are non-zero in the dates you want to highlight. It's a workaround, but it might be a starting point. As for the label, I have no idea.

  • So far I've managed to get it implemented by using CustomLabels on the secondary X axis, and have the tick marks on that axis drawn with the color I want. The main issue now is to get both X axes synchronized so that the CustomLabels ends up where I want them as they currently ends up a bit scattered around.

  • I solved this by subscribing to the PostPaint event. In the event handler I simply draw the lines myself and add the labels on top of these vertical lines.

  • chartName.Series["seriesName"].Points[index]["Color"] = System.Drawing.Color.Blue;

0 comments:

Post a Comment