Flot:如何在给定x坐标的行上找到y坐标

时间:2014-08-07 20:47:39

标签: graph flot point intersection

我正在使用Flot图表,我正在设置各种互动元素。 其中一个元素是用户输入任何x值的元素(根据情况,它实际上可以是x或y值,但为了简单起见,我们假设它始终是x轴值)并且我需要在我绘制的线上输出相应的y坐标。我觉得这应该很简单,所以如果答案显而易见,我会道歉。请注意,输入值可能不会是"点"在flot用于创建行的数组中(虽然它可以)。

您还可以想象在某个点处x = [用户输入,不一定是整数]的垂直线与另一个线系列相交。我需要找到交叉点。我尝试上传照片,但我没有足够的声望点。

1 个答案:

答案 0 :(得分:3)

你的代数怎么样?

这实际上是一个例子,埋在了flot的例子中here。如果你查看该页面的来源,你会看到这一点(我已经添加了解释说明):

// Find the nearest points, x-wise
// loop the series data until you find the point
// immediately after your x value of interest (pos.x in this code)
for (j = 0; j < series.data.length; ++j) {
    if (series.data[j][0] > pos.x) {
        break;
    }
}

// Now Interpolate
// Here's the algebra fun!
var y,
    p1 = series.data[j - 1], // point before your x
    p2 = series.data[j]; // point after your x

if (p1 == null) {
    y = p2[1]; // if no point before just get y-value
} else if (p2 == null) {
    y = p1[1]; // if no point after just get y-value
} else {
    y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);
    // here's the algebra bit, see below    
}

在最后的其他方面,使用的等式是this interpolation between two points。 Ain的数学很棒?

相关问题