简体   繁体   English

AxisX上带有DateTime的柱形图显示错误的日期

[英]Column Chart with DateTime on AxisX Displays wrong Dates

I created a column chart and I simply don't get the labels on the X-axis right. 我创建了一个柱形图,但我根本没有在X轴上找到正确的标签。

Here is a screenshot of my chart. 这是我的图表的屏幕截图。 I added a line at the bottom of the screenshot to show how I expect it to be. 我在屏幕截图的底部添加了一行,以显示预期的效果。

柱形图

Here is my code: 这是我的代码:

private void Form1_Load(object sender, EventArgs e)
{
    string series = "TestData";

    // Clear Series
    chart.Series.Clear();

    // Add new Sereis and set XValueType
    chart.Series.Add(series);
    chart.Series[series].XValueType = ChartValueType.DateTime;

    // Add data points to Series
    chart.Series[series].Points.AddXY(new DateTime(2008, 12, 31).ToOADate(), 49.91);
    chart.Series[series].Points.AddXY(new DateTime(2009, 12, 31).ToOADate(), 102.05);
    chart.Series[series].Points.AddXY(new DateTime(2010, 12, 31).ToOADate(), 15.84);
    chart.Series[series].Points.AddXY(new DateTime(2011, 12, 31).ToOADate(), 29.12);
    chart.Series[series].Points.AddXY(new DateTime(2012, 12, 31).ToOADate(), 3.3);
    chart.Series[series].Points.AddXY(new DateTime(2013, 12, 31).ToOADate(), 31.09);
    chart.Series[series].Points.AddXY(new DateTime(2014, 12, 31).ToOADate(), 5.44);

    // Set color and dash style of Major Grid
    chart.ChartAreas[0].AxisY.MajorGrid.LineColor       = Color.Gray;
    chart.ChartAreas[0].AxisY.MajorGrid.LineDashStyle   = ChartDashStyle.Dash;
    chart.ChartAreas[0].AxisX.MajorGrid.LineColor       = Color.Gray;
    chart.ChartAreas[0].AxisX.MajorGrid.LineDashStyle   = ChartDashStyle.Dash;

    // Set Interval for X-Axis
    chart.ChartAreas[0].AxisX.LabelStyle.Format         = "yyyy";
    chart.ChartAreas[0].AxisX.Interval                  = 1;
    chart.ChartAreas[0].AxisX.IntervalType              = DateTimeIntervalType.Years;
    chart.ChartAreas[0].AxisX.IntervalOffsetType        = DateTimeIntervalType.Years;
}

I already tried to use the IntervalOffset , but it did not do the job. 我已经尝试使用IntervalOffset ,但是它没有完成任务。

Does somebody know how to fix this issue? 有人知道如何解决此问题吗?

Many thanks Konstantin 非常感谢康斯坦丁

First of all you have to change the Date to be the first day of the year and not the last, and you have to set the property IsXValueIndexed to true, like: 首先,必须将日期更改为一年的第一天而不是最后一天,并且必须将属性IsXValueIndexed设置为true,例如:

    private void Form1_Load(object sender, EventArgs e)
    {
        string series = "TestData";

        // Clear Series
        chart.Series.Clear();

        // Add new Sereis and set XValueType
        chart.Series.Add(series);
        chart.Series[series].XValueType = ChartValueType.Date;
        chart.Series[series].IsXValueIndexed = true;

        // Add data points to Series
        chart.Series[series].Points.AddXY(new DateTime(2008, 1, 1).ToOADate(), 49.91);
        chart.Series[series].Points.AddXY(new DateTime(2009, 1, 1).ToOADate(), 102.05);
        chart.Series[series].Points.AddXY(new DateTime(2010, 1, 1).ToOADate(), 15.84);
        chart.Series[series].Points.AddXY(new DateTime(2011, 1, 1).ToOADate(), 29.12);
        chart.Series[series].Points.AddXY(new DateTime(2012, 1, 1).ToOADate(), 3.3);
        chart.Series[series].Points.AddXY(new DateTime(2013, 1, 1).ToOADate(), 31.09);
        chart.Series[series].Points.AddXY(new DateTime(2014, 1, 1).ToOADate(), 5.44);

        // Set color and dash style of Major Grid
        chart.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Gray;
        chart.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
        chart.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Gray;
        chart.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;

        // Set Interval for X-Axis
        chart.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy";
        chart.ChartAreas[0].AxisX.Interval = 1;
        chart.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Years;
        chart.ChartAreas[0].AxisX.IntervalOffsetType = DateTimeIntervalType.Years;            

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM