简体   繁体   English

MS图表:X-AXIS标签重复

[英]MS Chart: X-AXIS labels repeating

I am trying to create multi series line chart using MS Chart. 我正在尝试使用MS Chart创建多系列折线图。 I have created it successfully. 我已经成功创建了它。 But the problem is the x-axis labels repeation. 但是问题是x轴标签重复。 Here is what is created 这是创建的

在此处输入图片说明

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

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. 而不是在xAxisValues数组中使用日期, xAxisValues使用月份名称作为字符串。

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. 希望这对其他人有帮助。

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

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