Question

I'm using the Microsoft Chart Controls for .NET 3.5 and am struggling with getting the chart control to support window and control resizing.

I have graphs where the X value is dates, and want the chart to display the maximum available of intervals/labels on the chart axis when I resize the window.

The closest I've come is to call this from the PrePaint event:

double interval = chart.Series[0].Points.Count / ((double)chart.Width / 90);
foreach (var area in chart.ChartAreas.Where(ca => ca.Visible))
{
    area.AxisX.Interval = interval;
}

This makes the intervals and labels fit perfectly along the X axis, but the dates are not shown correctly. This first label seems to be right (some date in 2008), but the rest of the labels along the axis are displayed as some date in 1900 instead.

What is the preferred way of doing this?

Was it helpful?

Solution

For dates in Microsoft Chart Controls you have to explicitly specify the minimum dates to start with. Otherwise it takes 29th december 1899 as origin. You can set minimum and max dates on chart like

chart.ChartAreas[0].AxisY.Minimum = (new DateTime(2010, 5, 1)).ToOADate();
chart.ChartAreas[0].AxisY.Maximum = (new DateTime(2011, 4, 1)).ToOADate();

The post Creating a Gantt Chart with the MS Chart Controls has some valuable information on Microsoft Chart Controls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top