在C#中向水平线添加水平线

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

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

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

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

我会添加另一个具有相同X值但仍然代表平均值的Y值的系列。

 double avgY = {average of Y points} 

在你的循环中:

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