3

我试图抓住光标所在的最近的绘图点。我

我在源代码中找到了似乎能够做到这一点的findNearbyItem函数,但是当我尝试手动调用它时,我收到了错误。jquery.flot.jsReferenceError: findNearbyItem is not defined

这是我指的功能:

function findNearbyItem(mouseX, mouseY, seriesFilter) {
    var maxDistance = options.grid.mouseActiveRadius,
        smallestDistance = maxDistance * maxDistance + 1,
        item = null, foundPoint = false, i, j, ps;

    for (i = series.length - 1; i >= 0; --i) {
        if (!seriesFilter(series[i]))
            continue;

        var s = series[i],
            axisx = s.xaxis,
            axisy = s.yaxis,
            points = s.datapoints.points,
            mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster
            my = axisy.c2p(mouseY),
            maxx = maxDistance / axisx.scale,
            maxy = maxDistance / axisy.scale;

        ps = s.datapoints.pointsize;
        // with inverse transforms, we can't use the maxx/maxy
        // optimization, sadly
        if (axisx.options.inverseTransform)
            maxx = Number.MAX_VALUE;
        if (axisy.options.inverseTransform)
            maxy = Number.MAX_VALUE;

        if (s.lines.show || s.points.show) {
            for (j = 0; j < points.length; j += ps) {
                var x = points[j], y = points[j + 1];
                if (x == null)
                    continue;

                // For points and lines, the cursor must be within a
                // certain distance to the data point
                if (x - mx > maxx || x - mx < -maxx ||
                    y - my > maxy || y - my < -maxy)
                    continue;

                // We have to calculate distances in pixels, not in
                // data units, because the scales of the axes may be different
                var dx = Math.abs(axisx.p2c(x) - mouseX),
                    dy = Math.abs(axisy.p2c(y) - mouseY),
                    dist = dx * dx + dy * dy; // we save the sqrt

                // use <= to ensure last point takes precedence
                // (last generally means on top of)
                if (dist < smallestDistance) {
                    smallestDistance = dist;
                    item = [i, j / ps];
                }
            }
        }

        if (s.bars.show && !item) { // no other point can be nearby
            var barLeft = s.bars.align == "left" ? 0 : -s.bars.barWidth/2,
                barRight = barLeft + s.bars.barWidth;

            for (j = 0; j < points.length; j += ps) {
                var x = points[j], y = points[j + 1], b = points[j + 2];
                if (x == null)
                    continue;

                // for a bar graph, the cursor must be inside the bar
                if (series[i].bars.horizontal ?
                    (mx <= Math.max(b, x) && mx >= Math.min(b, x) &&
                     my >= y + barLeft && my <= y + barRight) :
                    (mx >= x + barLeft && mx <= x + barRight &&
                     my >= Math.min(b, y) && my <= Math.max(b, y)))
                        item = [i, j / ps];
            }
        }
    }

    if (item) {
        i = item[0];
        j = item[1];
        ps = series[i].datapoints.pointsize;

        return { datapoint: series[i].datapoints.points.slice(j * ps, (j + 1) * ps),
                 dataIndex: j,
                 series: series[i],
                 seriesIndex: i };
    }

    return null;
}

如果有解决此问题的替代方法,请告诉我。

4

3 回答 3

7

你的用例是什么?如果您将mouseActiveRadius选项增加到大型浮点数,则会为您找到离光标最近的点。

var options = {
    grid: { 
        hoverable: true, 
        mouseActiveRadius: 1000
    }
}

这里的例子。

编辑

是的,您可以使用该plothover事件来检索突出显示的点。

$("#placeholder").bind("plothover", function (event, pos, item) {
    if (item){
        var x = item.datapoint[0].toFixed(2),
            y = item.datapoint[1].toFixed(2);
        console.log("x:" + x + ", " + "y:" + y);
    }
});

在这里更新了小提琴。

于 2013-07-13T20:02:55.243 回答
2

这是 flot 的内部功能,但您可以轻松地自己重新创建相同的功能。您需要做的就是遍历数据系列中的点,并将它们的位置与鼠标指针进行比较(毕达哥拉斯应该能够在这里为您提供帮助)。

于 2013-07-09T18:16:33.680 回答
0

至于你看到的原因:ReferenceError: findNearbyItem is not defined看到这个。手动应用这个小补丁将使功能公开。

于 2013-08-13T21:27:54.527 回答