10

I am using a System.Windows.Forms.DataVisualization.Chart to plot some x,y scatter data, like this:

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

  2. Actually, the x axis is the time (hh:mm:ss). I am converting it to string to plot it because if I use the DateTime format for the x axis (XValueType) of the chart it doesn't show the seconds. Can I correct that to show the seconds so I can plot directly x as DateTime and y as double?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
betelgeuse
  • 430
  • 2
  • 10
  • 24
  • You can achieve the same results with a `StripLine`, refer to: http://stackoverflow.com/questions/6524501/custom-x-y-grid-line-in-mschart-control – Dayan Feb 24 '14 at 14:10

2 Answers2

20

For point 2, you can still set the XValueType to DateTime but with proper 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 chartarea.

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);
Junaith
  • 3,298
  • 24
  • 34
0

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

double avgY = {average of Y points}

and in your loop:

chart1.Series["Series3"].Points.AddXY(stringX, avgY);
D Stanley
  • 149,601
  • 11
  • 178
  • 240