Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

c# - get chart value in point

For example I have chart with 2 points - 0,0 and 10,10 and chart type is FastLine. I want to know what Y value will be in chart in choosen X value.

For example when X is 5 I want to know that Y is 5.

My charts more complicated and have tons of points, I need to get Y value through X.

How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...