Oxyplot Find 'Y Axis Value' From 'X Axis Value' C#

*爱你&永不变心* 提交于 2021-01-29 16:16:09

问题


I draw my plot using Oxyplot and i don't have any problem about drawing the plot. My Y axis is a 'LinearAxis' and X axis is a 'TimeSpanAxis'.

What i want to do is get Y value from given X value.

For example i want to get Y value from TimeSpan(0,0,0,1).

I can't use mouse position or any other events. X value will be given by user as a timespan.

This should be easy but I couldn't find anything.


回答1:


I am not quite sure this is the most efficient way to do this, but here is one suggestion on how to find Y value from X. Please note this is not 100% tested code, but you would get the Gist of the idea from it.

The idea is to calculate the Slope based on the "defined point" before and after the point to search. Then, you can use the slope to find the missing cordinate of point in question.

Slope = (y2-y2)/(x2-x1)

Given you have one axis as LinearAxis and other as TimeSpanAxis, and you are give the TimeSpanAxis for which you need to find Y.

var timeSpanToSearch = new TimeSpan(8, 30, 0);
var timeSpanToSearchAsDouble = TimeSpanAxis.ToDouble(timeSpanToSearch);
var series = MyModel.Series.First() as LineSeries;
double result = 0;

// If the point is a defined point, you can find Y easily
if (series.Points.Any(x => x.X.Equals(timeSpanToSearchAsDouble)))
{
    result = series.Points.First(x => x.X.Equals(timeSpanToSearchAsDouble)).Y;
}
else
{
    var floorValue = series.Points.Where(x => x.X < timeSpanToSearchAsDouble).Last();       
    var ceilingValue = series.Points.Where(x => x.X > timeSpanToSearchAsDouble).First();

    var slope = (ceilingValue.Y - floorValue.Y) / (ceilingValue.X - floorValue.X);
    result = slope * (timeSpanToSearchAsDouble - floorValue.X) + floorValue.Y;
}            


来源:https://stackoverflow.com/questions/54063884/oxyplot-find-y-axis-value-from-x-axis-value-c-sharp

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