问题
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