MS 图表:X 轴标签重复
MS Chart: X-AXIS labels repeating
我正在尝试使用 MS Chart 创建多系列折线图。我创建成功了。但问题是 x 轴标签重复。这是创建的内容
谁能告诉我为什么月份会重复?我怎样才能避免呢?
更新:
这是代码:
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);
好的,我找到了解决方案。
我刚刚转换了
series1.XValueType = ChartValueType.Date;
series2.XValueType = ChartValueType.Date;
series3.XValueType = ChartValueType.Date;
收件人:
series1.XValueType = ChartValueType.String;
series2.XValueType = ChartValueType.String;
series3.XValueType = ChartValueType.String;
而不是在 xAxisValues
数组中使用日期,而是使用月份名称作为字符串。
DateTime[] xvals = {DateTime.Now.AddMonth(-1),DateTime.Now};
收件人:
string[] xvals = {DateTime.Now.AddMonth(-1).ToString("MMM"),DateTime.Now.ToString("MMM")};
希望这对其他人有帮助。
我正在尝试使用 MS Chart 创建多系列折线图。我创建成功了。但问题是 x 轴标签重复。这是创建的内容
谁能告诉我为什么月份会重复?我怎样才能避免呢? 更新: 这是代码:
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);
好的,我找到了解决方案。
我刚刚转换了
series1.XValueType = ChartValueType.Date;
series2.XValueType = ChartValueType.Date;
series3.XValueType = ChartValueType.Date;
收件人:
series1.XValueType = ChartValueType.String;
series2.XValueType = ChartValueType.String;
series3.XValueType = ChartValueType.String;
而不是在 xAxisValues
数组中使用日期,而是使用月份名称作为字符串。
DateTime[] xvals = {DateTime.Now.AddMonth(-1),DateTime.Now};
收件人:
string[] xvals = {DateTime.Now.AddMonth(-1).ToString("MMM"),DateTime.Now.ToString("MMM")};
希望这对其他人有帮助。