【问题标题】:Add horizontal line to chart in C#在 C# 中向图表添加水平线
【发布时间】:2014-02-24 14:03:56
【问题描述】:

我正在使用System.Windows.Forms.DataVisualization.Chart 绘制一些 x,y 散点数据,如下所示:

chart1.Series["Series2"].Points.AddXY(stringX, doubleY);
  1. 我想在该图表中添加一条水平线,其平均值为 y = 常数。 我该怎么做?请注意,x 轴是一个字符串

  2. 其实x轴就是时间(hh:mm:ss)。我将它转换为字符串来绘制它,因为如果我对图表的 x 轴 (XValueType) 使用 DateTime 格式,它不会显示秒数。 我可以更正它以显示秒数,以便我可以直接将 x 绘制为 DateTime 并将 y 绘制为 double 吗?

【问题讨论】:

标签: c# .net mschart


【解决方案1】:

对于第 2 点,您仍然可以将 XValueType 设置为 DateTime,但在 LabelStyle 中使用正确的格式。

chart1.Series[SeriesName].XValueType = ChartValueType.DateTime;
// Use the required format. I used the below format as example.
chart1.ChartAreas[ChartName].AxisX.LabelStyle.Format = "dd/mm/yy hh:mm:ss";

对于第 1 点,将StripLine 添加到图表区域的 Y 轴。

StripLine stripline = new StripLine();
stripline.Interval = 0;
stripline.IntervalOffset = average value of the y axis; eg:35
stripline.StripWidth = 1;
stripline.BackColor = Color.Blue;
chart1.ChartAreas[ChartAreaName].AxisY.StripLines.Add(stripline);

【讨论】:

  • chart1.ChartAreas[ChartName].AxisY.StripLines.Add(stripline) 中的 ChartName 是什么;
  • @Silver,实际上是 ChartAreaName 而不是 ChartName...我已经更正了代码...感谢您指出。
  • 默认 ChartAreaName 名为“ChartArea1”
【解决方案2】:

我将添加另一个具有相同 X 值但代表平均值的恒定 Y 值的系列。

double avgY = {average of Y points}

在你的循环中:

chart1.Series["Series3"].Points.AddXY(stringX, avgY);

【讨论】:

  • 不添加水平线,而是将峰值添加到指定位置。
猜你喜欢
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
  • 2016-03-25
  • 2019-09-07
  • 2021-12-15
  • 1970-01-01
相关资源
最近更新 更多