C# 如何在 System.Windows.Forms.DataVisualization.Charting 的 X 轴上设置 DateTime 范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15907556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:25:38  来源:igfitidea点击:

How do you set DateTime range on X axis for System.Windows.Forms.DataVisualization.Charting?

c#ado.net

提问by user8128167

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:

目前,我正在尝试使用 Windows 窗体显示图表,该图表在 X 轴上显示每月数据,在 Y 轴上显示一个整数值;但是,我没有为 X 轴正确设置范围,其中 MonthYear 是日期时间:

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);

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?

问题是,当显示图表时,X 轴的日期时间为“1900-01-01”,所以我的问题是,如何将日期范围设置为从 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;

Or,

或者,

chart.ChartAreas[0].AxisX.Crossing = DateTime.Now.Ticks;

Or,

或者,

chart.ChartAreas[0].AxisX.Minimum = DateTime.Now.Ticks;

TIA.

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();

The above code sets the proper range now for the X axis; however, now chart itself is blank.

上面的代码现在为 X 轴设置了适当的范围;然而,现在图表本身是空白的。

UPDATE 2:

更新 2:

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:

是的,谢谢 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);
}

To this:

对此:

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++;
}

Thank you!

谢谢!

采纳答案by DasKrümelmonster

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[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.

也许我在途中修正了你的错误。

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.

它按预期工作:一行包含四个数据点,所有 x 轴标签都是可见的,图形本身也是如此。如果仍然存在问题,请提供完整的测试代码以及​​应该发生的情况与实际发生的情况的描述。