我正在使用System.Windows.Forms.DataVisualization.Chart
绘制一些x,y分散数据,如下所示:
chart1.Series["Series2"].Points.AddXY(stringX, doubleY);
我想在该图表中添加一条水平线,其平均值为y =常数。 我该怎么做?请注意,x轴是一个字符串
实际上,x轴是时间(hh:mm:ss)。我正在将它转换为字符串以绘制它,因为如果我使用DateTime格式的图表的x轴(XValueType)它不显示秒。 我可以更正显示秒数,这样我可以直接将x绘制为DateTime,将y绘制为double吗?
答案 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);