本文介绍了你如何对System.Windows.Forms.DataVisualization.Charting X轴设置日期时间范围是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

presently我试图显示使用Windows窗体显示在X轴和Y轴的整数值月度数据的图表;但是,我不是设定的范围内适当地为X轴,其中MonthYear是一个日期时间:

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年1月1日?

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

以上code现在设置适当的范围内为X轴;然而,现在的图表本身是空的。

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

更新2:

是的,谢谢你DasKrumelmonster - 即固定它!我用code从<一个href="http://www.$c$cproject.com/Articles/168056/Windows-Charting-Application">http://www.$c$cproject.com/Articles/168056/Windows-Charting-Application,简单地应该看起来更密切关注笔者的受保护的内部结构图GenerateChart(数据表dtChartDataSource,诠释的宽度,高度INT,字符串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++;
}

感谢您!

推荐答案

无法重现。我想这code:

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.

它按预期工作:,所有的X轴与四个数据点一号线的标签是可见的,因此该图本身。如果仍有问题,请提供完整的测试code随着应该发生的事情与实际发生的情况的说明。

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轴设置日期时间范围是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:26