Search code examples
c#chartsmschart

C# MS Charts get Y value from a generic X


I have a chart with 3 lines, all of them dynamics (the points series varies any time). For two lines I have few points, up to 20, and for the other one, that is a Spline, up to 500. (always in the same x range)

I have to verify if the Spline is between the two other lines. In other words, for each point of the Spline get the Y value of all the lines and verify if it's in the range.

How I could achieve that?

I've already tried the follow code, but when I call an X point that is not defined in the other two Line I'll have only the Y value for the Spline:

   var a = chart1.Series.Select(series => series.Points.Where(point => point.XValue == 7).ToList()).ToList();

Follow an image of the graph-like:

The blue and the yellow line are the ones "less defined", and I have to verify if the red one stays between them


Solution

  • It's rather a math problem than a chart problem. To determine the mid line is in between the upper and lower bounds, it comes down to the linear interpolation of the bounds.

    Suppose your upper bound (ub) is defined on five points: 1,2,5,6,9. The easy way to do the linear interpolation is to find the two closest neighbors and do the weighted average. e.g. ub(5.5) = ub(5)/2 + ub(6)/2

    There are many ways to do the 1D linear interpolation and if you search it on stackoverflow you should be able to find existing solutions.