简体   繁体   English

Oxyplot从“ X轴值” C#查找“ Y轴值”

[英]Oxyplot Find 'Y Axis Value' From 'X Axis Value' C#

I draw my plot using Oxyplot and i don't have any problem about drawing the plot. 我使用Oxyplot绘制绘图,而绘制绘图没有任何问题。 My Y axis is a 'LinearAxis' and X axis is a 'TimeSpanAxis'. 我的Y轴是“ LinearAxis”,X轴是“ TimeSpanAxis”。

What i want to do is get Y value from given X value. 我想做的就是从给定的X值中获取Y值。

For example i want to get Y value from TimeSpan(0,0,0,1). 例如我想从TimeSpan(0,0,0,1).获得Y值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. X值将由用户指定为时间跨度。

This should be easy but I couldn't find anything. 这应该很容易,但是我什么也找不到。

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. 我不太确定这是执行此操作的最有效方法,但这是有关如何从X查找Y值的建议。请注意,这不是经过100%测试的代码,但是您会从中获得要点。

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. 给定一个轴为LinearAxis,另一个轴为TimeSpanAxis,并给出需要查找Y的TimeSpanAxis。

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;
}            

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM