gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:16 25 4
gpt4 key购买 nike

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

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com