Search code examples
c#.netmschart

MS Chart: X-AXIS labels repeating


I am trying to create multi series line chart using MS Chart. I have created it successfully. But the problem is the x-axis labels repeation. Here is what is created

enter image description here

Can anyone tell me why the months are repeated? how can i avoid it? UPDATE: Here is the code:

DateTime[] xvals = {DateTime.Now.AddMonth(-1),DateTime.Now};
decimal[] gvals = {4.3,0};
decimal[] ypvals = {0,0};
decimal[] yvals = {3.5,0};

                        // create the chart
var chart = new Chart();
chart.Size = new Size(600, 250);
chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
chart.BorderlineColor = System.Drawing.Color.FromArgb(26, 59, 105);
chart.BorderlineWidth = 3;

var chartArea = new ChartArea();
chartArea.AxisX.MajorGrid.LineWidth = 0;
//Remove Y-axis grid lines
chartArea.AxisY.MajorGrid.LineWidth = 0;
chartArea.AxisX.LabelStyle.Format = "MMM";
chartArea.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chartArea.AxisY.LabelStyle.Font = new Font("Consolas", 8);
chart.ChartAreas.Add(chartArea);

var series = new Series();
series.Name = "Y";
series.Legend = "Y";
series.ChartType = SeriesChartType.Line;
series.XValueType = ChartValueType.DateTime;
series.IsVisibleInLegend = true;
series.Color = Color.Red;
series.IsValueShownAsLabel = true;
series.BorderWidth = 2;
chart.Series.Add(series);
// bind the datapoints
chart.Series[0].Points.DataBindXY(xvals, yvals);


                        var series2 = new Series();
                        series2.Name = "YP";
                        series2.Legend = "YP";
                        series2.ChartType = SeriesChartType.Line;
                        series2.XValueType = ChartValueType.DateTime;
                        series2.IsVisibleInLegend = true;
                        series2.Color = Color.Yellow;
                        series2.IsValueShownAsLabel = true;
                        series2.BorderWidth = 2;
                        chart.Series.Add(series2);

                        // bind the datapoints
                        chart.Series[1].Points.DataBindXY(xvals, ypvals);


                        var series3 = new Series();
                        series3.Name = "G";
                        series3.Legend = "GG";
                        series3.ChartType = SeriesChartType.Line;
                        series3.XValueType = ChartValueType.DateTime;
                        series3.IsVisibleInLegend = true;
                        series3.Color = Color.Blue;
                        series3.IsValueShownAsLabel = true;
                        series3.BorderWidth = 2;
                        chart.Series.Add(series3);

                        // bind the datapoints
                        chart.Series[2].Points.DataBindXY(xvals, gvals);



                        // draw!
                        chart.Invalidate();

                        // write out a file
                        chart.SaveImage("D:\\cha.png", ChartImageFormat.Png);

Solution

  • Ok, I have got the solution.

    I have just converted the

    series1.XValueType = ChartValueType.Date;
    series2.XValueType = ChartValueType.Date;
    series3.XValueType = ChartValueType.Date;
    

    TO:

    series1.XValueType = ChartValueType.String;
    series2.XValueType = ChartValueType.String;
    series3.XValueType = ChartValueType.String;
    

    and instead of using dates in the xAxisValues array used months name as string.

    DateTime[] xvals = {DateTime.Now.AddMonth(-1),DateTime.Now};
    

    TO:

    string[] xvals = {DateTime.Now.AddMonth(-1).ToString("MMM"),DateTime.Now.ToString("MMM")};
    

    Hope this helps someone else.