获取图表值 [英] get chart value in point
问题描述
例如,我有2点的图表-0,0和10,10,图表类型为FastLine. 我想知道所选X值中图表中的Y值.
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.
例如,当X为5时,我想知道Y为5.
For example when X is 5 I want to know that Y is 5.
我的图表更加复杂并且有很多点,我需要通过X获得Y值.
My charts more complicated and have tons of points, I need to get Y value through X.
我该怎么做?
推荐答案
问题归结为两个任务:
- 找到x值的相邻点
- 将它们的y值内插给定的x值.
如果x值确实在稳步增加,则应同时解决这两个问题:
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);
}
它使用Linq查找上一个和下一个数据点,然后使用简单的数学查找插值.
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:
要在图表像素和值之间转换,请使用Axis
函数ValueToPixelPosition
和PixelPositionToValue
,顺便说一句.
To convert between chart pixels and values there are Axis
functions ValueToPixelPosition
and PixelPositionToValue
, btw.
这篇关于获取图表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!