Search code examples
c#winformsmschart

Line Chart Graphical Alert


I currently have a line graph in my C# program, and I have a min and max variable. If any the graph ever exceeds the max, or goes below the min, is there any built in way of displaying on the graph (such as a dot at the point) that the limit was passed, and display the x/y values for that point?

int max = 2000;
int min = 2000;

for (int i = 0; i < dgvLoadedValues.RowCount - 1; i++)
{
   DateTime x = Convert.ToDateTime(dgvLoadedValues.Rows[i].Cells[0].Value.ToString());

   try
   {
      float y = float.Parse(dgvLoadedValues.Rows[i].Cells[e.ColumnIndex].Value.ToString());
      chart1.Series["Series1"].Points.AddXY(x, y);
   }
   catch
   {
      Console.WriteLine("Unable to plot point");
   }
}

Code above simply shows values taken from a datagridview and displaying it into a line graph

Thank you


Solution

  • Unfortunately there seems to be no way to define such an automatic alert.

    But as you know just when the DataPoints are added or bound you can set a Marker where necessary.

    Here is a loop that does it after the fact in one go, but of course you can just as well set the markers as you add the points..:

    foreach (DataPoint dp in chart1.Series[0].Points)
    {
        if (dp.YValues[0] < max && dp.YValues[0] > min ) continue;
        dp.MarkerStyle = MarkerStyle.Circle;
        dp.MarkerColor = Color.Red;
    }
    

    Or in your case:

    try
    {
       float y = float.Parse(dgvLoadedValues.Rows[i].Cells[e.ColumnIndex].Value.ToString());
       int i = chart1.Series["Series1"].Points.AddXY(x, y);
       if (y < min || y > max)
       {
          chart1.Series["Series1"].Points[i].MarkerStyle = MarkerStyle.Circle;
          chart1.Series["Series1"].Points[i].MarkerColor = Color.Red;
       }       
    }
    

    To clear a marker you can set its MarkerStyle = MarkerStyle.None.

    Of course you could easily give the min and max points different colors..

    Here is an example with the simple circle style, but there are others including images..:

    enter image description here

    To add the values in a label use a format like this:

    dp.Label = "(#VALX{0.0} / #VAL{0.0})" ;