核心代码:
Chart1.ChartAreas[0].AxisX.Interval = (Front_Max - Front_Min).Days / 2; Chart1.ChartAreas[0].AxisX.Minimum = Front_Min.ToOADate(); Chart1.ChartAreas[0].AxisX.Maximum = Front_Max.ToOADate();
实现的效果:数据库最小的值:2015-01-12,最大值是2015-05-13,中间显示一个2015-03-13,只显示3个Label,如果是4个Lable就是除以3. 一个4个,间隔3个。
之前因为下面一行代码,Label日期显示的起始日期,而是前后扩展一段时间,研究了很久不知道怎么显示起始日期。
Chart1.ChartAreas[0].AxisX.IntervalType = System.Web.UI.DataVisualization.Charting.DateTimeIntervalType.Days
把这行代码删掉,或者改为下面的就可以了,默认就是这样:
Chart1.ChartAreas[0].AxisX.IntervalType = System.Web.UI.DataVisualization.Charting.DateTimeIntervalType.Auto;
因为在之前已经设置过ChartType 为日期格式。
chart.Series[SeriesName].ChartType = ChartValueType.Date
添加日期范围、日期间隔有效值检查的改良版代码:
try
{
Chart1.ChartAreas[0].AxisX.Minimum = Front_Min.AddDays(-2).ToOADate();
Chart1.ChartAreas[0].AxisX.Maximum = Front_Max.AddDays(2).ToOADate();
double days = (double)((TimeSpan)(Front_Max.AddDays(2) - Front_Min.AddDays(-2))).Days;
double labels = 2.0;
// check if the number of days is bigger than labels
if (days > labels)
{
// calculate the interval
double interval = days / labels;
Chart1.ChartAreas[0].AxisX.Interval = interval;
}
else
{
// set the interval of 1 day
Chart1.ChartAreas[0].AxisX.Interval = 1;
}
}
catch
{
//prevent null data
}
MS Chart-按照数据库的最大最小时间设置X轴label.
原文:http://www.cnblogs.com/sen068/p/4766505.html