How to get dynamic spacing in Dundas / Microsoft Chart Controls for Windows Forms?
I am using Microsoft Chart Controls for .NET 3.5 and am struggling with getting a chart control to support window resizing and control.
I have charts where the X value is dates and want the chart to display the maximum available spacing / labels on the chart axis when the window is resized.
The closest I came 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 spacing and labels fit perfectly along the X axis, but the dates are not displayed correctly. This first label appears to be correct (some date in 2008), but the rest of the labels along the axis are displayed as some date in 1900.
What's the preferred way to do this?
a source to share
For dates in Microsoft Chart Controls, you must explicitly specify the minimum start dates. Otherwise it would take December 29, 1899 as the source. You can set minimum and maximum dates on a 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 Create a Gantt Chart Using MS Chart Controls contains valuable information about Microsoft Chart Controls.
a source to share