简体   繁体   English

如何使用.net图表控件将X轴日期时间值jan-01更改为Oct-01

[英]How to change X-axis datetime value jan-01 to Oct-01 using .net chart control

I am trying to show x axis value in the form of date(MMM-yy) but it is always begin with jan-01. 我试图以date(MMM-yy)的形式显示x轴值,但它始终以jan-01开头。 So please provide solution to display other then jan-01, I mean instead of Jan-01 show oct-01. 因此,请提供解决方案以显示除jan-01之外的其他内容,我的意思是代替Jan-01 show oct-01。

Please find the simulated function : 请找到模拟功能:

    private static void drawGraph()
    {
        List<GraphPoints> listGP = new List<GraphPoints>();
        listGP.Add(new GraphPoints()
        {
            RecordDate = "01/10/1984",
            benchmark = "10000.00"
        });

        listGP.Add(new GraphPoints()
        {
            RecordDate = "29/06/1987",
            benchmark = "30396.00"
        });

        listGP.Add(new GraphPoints()
        {
            RecordDate = "31/05/1989",
            benchmark = "10000.00"
        });

        listGP.Add(new GraphPoints()
        {
            RecordDate = "30/09/1993",
            benchmark = "310137.88"
        });

        listGP.Add(new GraphPoints()
        {
            RecordDate = "31/12/2015",
            benchmark = "440037.28"
        });


        Graph.Chart chart;

        chart = new Graph.Chart();
        chart.Location = new System.Drawing.Point(10, 10);
        chart.Size = new System.Drawing.Size(800, 300);

        chart.ChartAreas.Add("draw");

        chart.ChartAreas["draw"].AxisX.IntervalType = Graph.DateTimeIntervalType.Years;
        chart.ChartAreas["draw"].AxisX.LabelStyle.Format = "MMM-yyyy";
        chart.ChartAreas["draw"].AxisX.MajorGrid.LineColor = Color.Black;
        chart.ChartAreas["draw"].AxisX.MajorGrid.LineDashStyle = Graph.ChartDashStyle.NotSet;

        chart.ChartAreas["draw"].AxisY.IntervalAutoMode = Graph.IntervalAutoMode.VariableCount;
        chart.ChartAreas["draw"].AxisY.MajorGrid.LineColor = Color.Black;
        chart.ChartAreas["draw"].AxisY.MajorGrid.LineDashStyle = Graph.ChartDashStyle.NotSet;

        chart.ChartAreas["draw"].BackColor = Color.White;



        chart.Series.Add("Bench-Mark");         
        chart.Series["Bench-Mark"].XValueType = Graph.ChartValueType.Date;
        chart.Series["Bench-Mark"].ChartType = Graph.SeriesChartType.Line;            
        chart.Series["Bench-Mark"].Color = Color.Red;
        chart.Series["Bench-Mark"].BorderWidth = 1;

        foreach (var item in listGP)
        {
            chart.Series["Bench-Mark"].Points.AddXY(Convert.ToDateTime(item.RecordDate).ToOADate(), item.benchmark);
        }

        chart.SaveImage("MyImage.jpg", Graph.ChartImageFormat.Jpeg);
    }

在此处输入图片说明

See Graph axis points are showing from jan 1988. 请参见Graph轴点显示自1988年1月。

There is a nice page on MSDN on various types of labels. MSDN上有一个漂亮的页面,上面有各种类型的标签。

It explains three types of labelling: 它解释了三种标签类型:

  • Using the Label of the AxisX.LabelStyle.Format and Axis.Interval property to create a regular grid of labels not really connected to the data points 使用AxisX.LabelStyle.FormatAxis.Interval属性的Label创建未真正连接到数据点的常规标签网格
  • Using the DataPoint.AxisLabel property to label each DataPoint individually 使用DataPoint.AxisLabel属性分别标记每个DataPoint
  • Using CustomLabels to set labels at arbitrary points on an axis. 使用CustomLabels在轴上的任意点设置标签。

You are using the first option but this will put a Label at the beginning of one unit of the AxisX.IntervalType which in your case will be years even if you switch to Months because there simply are too few points. 您使用的是第一个选项,但是这会将Label放置在AxisX.IntervalType的一个单元的开头,即使您切换到Months由于您的点AxisX.IntervalType ,在您的情况下,这也是几年。

This really should be simple to correct; 这确实应该很容易纠正。 since you do not want the IntervalType units labelled but the individual DataPoints , you should add AxisLabels to each DataPoint and all ought to be well: 由于不希望将IntervalType单位标记为单个DataPoints ,因此应将AxisLabels添加到每个DataPoint并且所有标记都应该很好:

Series S1 = chart.Series["Bench-Mark"];
foreach (var item in listGP)
{
    DateTime dt = Convert.ToDateTime(item);
    int p = S1.Points.AddXY(dt, listGP[item]);
    string l = dt.ToString("MMM-yyyy");
    S1.Points[p].AxisLabel = l;
}

Unfortunately with your data, the Chart control's built-in 'intelligence' makes this a bit harder; 不幸的是,对于您的数据, Chart控件的内置“智能”使此操作变得更加困难。 the issue is that no matter which combination of Interval and IntervalType you choose it just never will show each AxisLabel . 问题是,无论您选择IntervalIntervalType哪种组合,它都永远不会显示每个 AxisLabel In fact I only managed to display those that actually hit the Interval , ie DataPoints the fall the 1st of a month. 实际上,我只能显示那些实际达到Interval ,例如, DataPoints在每月的1号秋天。 But your data are randomly spread out over several years. 但是您的数据会随机散布到几年中。

But you can use CustomLabels as a workaround. 但是您可以使用CustomLabels作为解决方法。 After adding the AxisLabels in the code above call this little helper function to add CustomLabels ; 在上面的代码中添加AxisLabels之后,调用这个小帮助函数来添加CustomLabels ; we need to tell the Chart for which range each is to be displayed, and with a large enough range they all show up where they should (according to my data): 我们需要告诉Chart分别显示哪个范围,并在足够大的范围内将它们全部显示在应显示的位置(根据我的数据):

void addCustomLabels(Chart chart)
{
    Series S1 = chart.Series[0];
    ChartArea CA = chart.ChartAreas[0];
    CA.AxisX.CustomLabels.Clear();
    for (int i = 0; i < S1.Points.Count; i++)
    {
        CustomLabel cl = new CustomLabel();
        cl.FromPosition = S1.Points[i].XValue - 10;  // 10 day back and ahead..
        cl.ToPosition = S1.Points[i].XValue + 10;   //..will be enough space for one label
        cl.Text = S1.Points[i].AxisLabel;
        CA.AxisX.CustomLabels.Add(cl);
    }
}

在此处输入图片说明

Note that such a solution will only work well if your data points are as sparse as in the example; 请注意,仅当您的数据点与示例中一样稀疏时 ,这种解决方案才有效。 for many points the labels would clutter the axis. 对于许多点,标签会使轴混乱

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

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