javascript - Flot:如何抓取距离光标所在位置最近的标绘点?

标签 javascript flot

我正在尝试获取光标所在的最近标绘点。我

我在 jquery.flot.js 源代码中找到了 findNearbyItem 函数,它似乎能够执行此操作,但是当我尝试手动调用它时,我收到了ReferenceError: 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;
}

如果有其他方法可以解决这个问题,请告诉我。

最佳答案

您的用例是什么?如果将 mouseActiveRadius 选项增加到较大的值,flot 将为您找到离光标最近的点。

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

示例 here .

编辑

是的,您可以使用 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);
    }
});

更新 fiddle here .

关于javascript - Flot:如何抓取距离光标所在位置最近的标绘点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17554782/

相关文章:

javascript - 如何创建文本区域字数计数器?

javascript - 主干 JS 模板仅打印集合中的最后一项

javascript - Jquery Flot 重复数字时出现错误的图表

javascript - 使用 Flot JQuery 库更改现有图上的轴最小值/最大值

javascript - 绘制两个自定义日期范围之间的历史流程图

javascript - 当表或 div 中的复选框数组时,.each() 内的 .index() 始终返回 0

javascript - Html 5 模式正则表达式和最小值

javascript - QuickBase:每次在表中搜索时加载 javascript 文件

javascript - Django Flot 和 JavaScript

javascript - 如何在 Flot 中的多个堆叠线图之间填充颜色?