Add horizontal line to table in C #

I use System.Windows.Forms.DataVisualization.Chart to build some x, y scatter data, for example:

 chart1.Series["Series2"].Points.AddXY(stringX, doubleY); 
  • I would like to add to this diagram a horizontal line with an average value of the form y = constant. How can i do this? Note that the x axis is a string

  • Actually, the x axis is the time (hh: mm: ss). I convert it to a string to build it, because if I use the DateTime format for the x-axis (XValueType) of the chart, it does not display seconds. Can I fix this to show seconds so that I can directly portray x as DateTime and y as double?

+6
source share
2 answers

For point 2, you can still set XValueType to DateTime , but with the correct format in 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"; 

For point 1, add a StripLine to the y-axis of the chart.

 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); 
+11
source

I would add another series with the same X values, but with a constant Y value that represents the average value.

 double avgY = {average of Y points} 

and in your loop:

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

All Articles