目前我正在尝试使用窗体显示图表,该窗体显示X轴上的月度数据和Y轴上的整数值;但是,我没有为X轴正确设置范围,其中MonthYear是DateTime:
var pnChart = new System.Windows.Forms.Panel();
pnChart.Controls.Clear();
DataTable dtChartData = myDatabaseLayer.BuildDataTable("SELECT Added, Modified FROM tblStatistics WHERE ApplicationID = " + intApplicationID + " ORDER BY MonthYear");
Chart chart = GenerateChart(dtChartData, pnChart.Width, pnChart.Height, "ActiveBorder", 6);
chart.Series[0].XValueType = ChartValueType.DateTime;
chart.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd";
chart.ChartAreas[0].AxisX.Interval = 1;
chart.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Months;
chart.ChartAreas[0].AxisX.IntervalOffset = 1;
pnChart.Controls.Add(chart);
问题是,当显示图表时,X轴的日期时间为“1900-01-01”,所以我的问题是,如何设置日期范围从2013-01-01开始?
请注意我已在互联网上搜索并尝试了以下设置,但他们没有给我正确的范围:
chart.ChartAreas[0].AxisX.Maximum = DateTime.Now.Ticks;
或者,
chart.ChartAreas[0].AxisX.Crossing = DateTime.Now.Ticks;
或者,
chart.ChartAreas[0].AxisX.Minimum = DateTime.Now.Ticks;
TIA。
更新 请注意,我找到了如何正确设置范围:
chart.Series[0].XValueType = ChartValueType.DateTime;
DateTime minDate = new DateTime(2013, 01, 01);
DateTime maxDate = DateTime.Now;
chart.ChartAreas[0].AxisX.Minimum = minDate.ToOADate();
chart.ChartAreas[0].AxisX.Maximum = maxDate.ToOADate();
上面的代码为X轴设置了适当的范围;但是,现在图表本身是空白的。
更新2:
是的,谢谢你DasKrumelmonster - 修好了!我正在使用来自http://www.codeproject.com/Articles/168056/Windows-Charting-Application的代码,并且应该更仔细地查看作者的受保护内部Chart GenerateChart(DataTable dtChartDataSource,int width,int height,string bgColor,int intType)函数。为了解决这个问题,我更改了以下几行:
foreach (DataRow dr in dtChartDataSource.Rows)
{
double dataPoint = 0;
double.TryParse(dr[dc.ColumnName].ToString(), out dataPoint);
DataPoint objDataPoint = new DataPoint() { AxisLabel = "series", YValues = new double[] { dataPoint } };
chart.Series[series].Points.Add(dataPoint);
}
对此:
DateTime dtStart = new DateTime(2013, 01, 01);
int intMonthCounter = 0;
//Add data points to the series
foreach (DataRow dr in dtChartDataSource.Rows)
{
double dataPoint = 0;
double.TryParse(dr[dc.ColumnName].ToString(), out dataPoint);
DataPoint objDataPoint = new DataPoint() { AxisLabel = "series", YValues = new double[] { dataPoint } };
chart.Series[series].Points.AddXY(dtStart.AddMonths(intMonthCounter), dataPoint);
intMonthCounter++;
}
谢谢!
答案 0 :(得分:35)
无法重现。我试过这段代码:
private void button1_Click(object sender, EventArgs e)
{
var s = new Series();
s.ChartType = SeriesChartType.Line;
var d = new DateTime(2013, 04, 01);
s.Points.AddXY(d, 3);
s.Points.AddXY(d.AddMonths(-1), 2);
s.Points.AddXY(d.AddMonths(-2), 1);
s.Points.AddXY(d.AddMonths(-3), 4);
chart1.Series.Clear();
chart1.Series.Add(s);
chart1.Series[0].XValueType = ChartValueType.DateTime;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd";
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Months;
chart1.ChartAreas[0].AxisX.IntervalOffset = 1;
chart1.Series[0].XValueType = ChartValueType.DateTime;
DateTime minDate = new DateTime(2013, 01, 01).AddSeconds(-1);
DateTime maxDate = new DateTime(2013, 05, 01); // or DateTime.Now;
chart1.ChartAreas[0].AxisX.Minimum = minDate.ToOADate();
chart1.ChartAreas[0].AxisX.Maximum = maxDate.ToOADate();
}
也许我在途中修复了你的错误。
它按预期工作:一行有四个数据点,所有x轴标签都可见,图表本身也是如此。如果仍有问题,请提供完整的测试代码以及应该发生的事情与实际情况的描述。