在C#中添加水平线到图表

时间:2014-02-24 14:03:56

标签: c# .net mschart

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

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

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

2 个答案:

答案 0 :(得分:15)

对于第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);

答案 1 :(得分:0)

我会添加另一个具有相同X值的系列,但是代表平均值的常数Y值。

double avgY = {average of Y points}

并在你的循环中:

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