【问题标题】:Specific way to show a serie in MSChart using c#使用 c# 在 MSChart 中显示系列的特定方法
【发布时间】:2011-08-04 20:11:59
【问题描述】:

不知道有没有办法把这个serie显示如下,chartype是column。

我知道我需要一个 (X,Y) 对来显示图表中的数据点,但问题是我需要在 x 范围内显示一个 Y 值,为了澄清这一点,让我们放一个示例:

X 轴显示小时数和 Y 轴生产值,在这种情况下,生产从 7:30 开始,所以从 7:30 到 8 点的生产假设为 20 个单位; 8-9,60个单位; 9-10, 45 个单位,10-10:35 18 个单位。

我可以使用 MSChart 或者你们建议的其他任何一种来实现吗?这是一个 winform 应用程序。

谢谢

【问题讨论】:

标签: c# .net winforms data-visualization mschart


【解决方案1】:

你应该看看MS Chart site。下载选项卡包含 WinForms 示例,您可以将其作为 WinForms 项目下载,在 Visual Studio 中运行,并查看所需图表的代码。图表控件包含在 .Net 4 中,但可以下载并添加为 .Net 3.5 的参考。

您想要的是柱形图。您将值设置为图表上的系列,然后您可以选择列的宽度以显示所需的 x 值范围。

基本思想是创建ChartChartAreaSeries,以及可选的TitleLegend

您将一个或多个Series 对象添加到ChartArea,然后将ChartAreaTitleLegend 添加到Chart

您将DataPoints 单独添加到Series,如下例所示,或者您将Chart.DataSource 设置为DataTable 之类的东西并使用DataBinding

这是来自柱形图示例的 sn-p:

System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint1 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36890, 32);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint2 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36891, 56);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint3 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36892, 35);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint4 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36893, 12);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint5 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36894, 35);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint6 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36895, 6);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint7 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(36896, 23);
System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Title title1 = new System.Windows.Forms.DataVisualization.Charting.Title();

chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
chart1.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
chart1.BorderlineWidth = 2;
chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;

chartArea1.Area3DStyle.Inclination = 15;
chartArea1.Area3DStyle.IsClustered = true;
chartArea1.Area3DStyle.IsRightAngleAxes = false;
chartArea1.Area3DStyle.Perspective = 10;
chartArea1.Area3DStyle.Rotation = 10;
chartArea1.Area3DStyle.WallWidth = 0;
chartArea1.AxisX.LabelAutoFitMaxFontSize = 8;
chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
chartArea1.AxisX.LabelStyle.Format = "MM-dd";
chartArea1.AxisX.LabelStyle.IsEndLabelVisible = false;
chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisY.LabelAutoFitMaxFontSize = 8;
chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
chartArea1.AxisY.LabelStyle.Format = "C0";
chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.BackColor = System.Drawing.Color.OldLace;
chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
chartArea1.BackSecondaryColor = System.Drawing.Color.White;
chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
chartArea1.Name = "Default";
chartArea1.ShadowColor = System.Drawing.Color.Transparent;

chart1.ChartAreas.Add(chartArea1);

legend1.BackColor = System.Drawing.Color.Transparent;
legend1.Enabled = false;
legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
legend1.IsTextAutoFit = false;
legend1.Name = "Default";

chart1.Legends.Add(legend1);
chart1.Location = new System.Drawing.Point(16, 64);
chart1.Name = "chart1";

series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
series1.ChartArea = "Default";
series1.Legend = "Default";
series1.Name = "Series1";
series1.Points.Add(dataPoint1);
series1.Points.Add(dataPoint2);
series1.Points.Add(dataPoint3);
series1.Points.Add(dataPoint4);
series1.Points.Add(dataPoint5);
series1.Points.Add(dataPoint6);
series1.Points.Add(dataPoint7);
series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;

chart1.Series.Add(series1);
chart1.Size = new System.Drawing.Size(412, 296);
chart1.TabIndex = 0;

title1.Font = new System.Drawing.Font("Trebuchet MS", 14.25F, System.Drawing.FontStyle.Bold);
title1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
title1.Name = "Title1";
title1.ShadowColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
title1.ShadowOffset = 3;
title1.Text = "Column Chart";

chart1.Titles.Add(title1);

【讨论】:

  • 感谢您的回复@nekno,但我认为这对我不起作用。正如您在示例中注意到的那样, series1.Points.Add(dataPoint1) 其中 dataPoint1 是一对 X,Y;将在该点绘制一列(因为 CharType 是列),如果您这样做是一样的:series1.Points.AddXY(8,20)。我需要的是: series1.Points.AddXY({7.30,8},20) 但不支持这样的事情。无论如何,我会给你一票,因为它是如何使用 MSchart 的一个很好的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-20
相关资源
最近更新 更多