get chart value in point

跟風遠走 提交于 2019-12-07 16:08:27

The problem boils down to two tasks:

  • Finding the neighbouring points for an x-value
  • Interpolating their y-values for the given x-value.

If the x-values are indeed steadily increasing this should solve both:

double interpolatedY(Series s, double xval)
{
    DataPoint pPrev = s.Points.Last(x => x.XValue <= xval);
    DataPoint pNext = s.Points.First(x => x.XValue >= xval);

    if (pPrev == pNext) return pPrev.YValues[0];

    return pPrev.YValues[0] + (pNext.YValues[0] - pPrev.YValues[0])
        * (xval  - pPrev.XValue)/ (pNext.XValue - pPrev.XValue); 
}

It uses Linq to find the previous and next datapoint and then uses simple math to find the interpolated value.

Note that most checks are omitted!

Here I have added an identical point series and a third one to add the interpolated values:

To convert between chart pixels and values there are Axis functions ValueToPixelPosition and PixelPositionToValue, btw.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!