MS Chart:X-AXIS标签重复

时间:2015-03-13 19:30:32

标签: c# .net mschart

我正在尝试使用MS Chart创建多系列折线图。我已成功创建它。但问题是x轴标签重复。这是创建的内容

enter image description here

谁能告诉我为什么这几个月会重演?我怎么能避免呢? 更新: 这是代码:

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

1 个答案:

答案 0 :(得分:0)

好的,我有解决方案。

我刚刚转换了

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;

而不是使用xAxisValues数组中的日期,而是使用月份名称作为字符串。

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

TO:

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

希望这有助于其他人。