问题描述
目前我正在尝试使用 Windows 窗体显示图表,该图表在 X 轴上显示月度数据,在 Y 轴上显示整数值;但是,我没有正确设置 X 轴的范围,其中 MonthYear 是 DateTime:
Presently I am attempting to display a chart using windows forms that shows monthly data on the X axis and an integer value on the Y axis; however, I am not setting the range properly for the X Axis, where MonthYear is a 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 开始?
The problem is, when the chart is displayed, the X axis has the datetime "1900-01-01" so my question is, how do I set the date range to start at 2013-01-01?
请注意,我已经搜索了互联网并尝试了以下设置,但它们没有给我正确的范围:
Please note that I have searched the internet and tried the following settings, but they do not give me the correct range:
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.
更新:请注意,我找到了如何正确设置范围的方法:
UPDATE: Please note that I found how to set the range properly using this:
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 轴设置了合适的范围;但是,现在图表本身是空白的.
The above code sets the proper range now for the X axis; however, now chart itself is blank.
更新 2:
是的,谢谢 DasKrumelmonster - 解决了它!我使用的代码来自 http://www.codeproject.com/Articles/168056/Windows-Charting-Application,应该更仔细地查看作者受保护的内部图表 GenerateChart(DataTable dtChartDataSource, int width, int height, string bgColor, int intType) 函数.为了纠正这个问题,我更改了以下几行:
Yes, thank you DasKrumelmonster--that fixed it! I was using code from http://www.codeproject.com/Articles/168056/Windows-Charting-Application, and simply should have looked more closely at the author's protected internal Chart GenerateChart(DataTable dtChartDataSource, int width, int height, string bgColor, int intType) function. To correct the issue, I changed these lines:
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++;
}
谢谢!
推荐答案
无法重现.我试过这段代码:
Cannot reproduce. I tried this code:
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.Add(new ChartArea()); // In some cases the winforms designer adds this already
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();
}
也许我在途中修正了你的错误.
Maybe I fixed your error on the way.
它按预期工作:一行有四个数据点,所有 x 轴标签都是可见的,图形本身也是如此.如果仍然存在问题,请提供完整的测试代码以及应该发生的情况与实际发生的情况的描述.
It works as expected: One line with four data points, all x-axes labels are visible and so is the graph itself. If there is still an issue, please provide full testing code along with a description of what should happen vs. what actually happens.
这篇关于如何为 System.Windows.Forms.DataVisualization.Charting 在 X 轴上设置 DateTime 范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!