在假想的垂直线上浮动工具提示

发布于 2024-11-17 19:54:09 字数 846 浏览 3 评论 0原文

如何添加工具提示的功能,以便在 鼠标“悬停”在该点上或该点上方/下方的假想垂直线上的任何像素?

我的图表上只有 1 条线。我 希望这样当鼠标移动时可以显示数据点的工具提示 越过穿过该点的“看不见的”垂直线 - 不强制 用户明确将鼠标悬停在该点上。

我找到了这个解决方案 - 但它看起来像是一个黑客(并且不起作用): Flot mousehover算法纯粹基于x位置进行检测?< /a>

有没有更好的方法来做到这一点而不是破解源代码?

这是我正在谈论的一个例子(它的大图表,但是这个 是我想要的功能)。您可以将鼠标悬停在该线上或将鼠标悬停在 X 轴上的任意点以显示该 X 轴上的点 像素: http://finance.yahoo.com/echarts?s=^DJI+Interactive#chart1:symbol=^dji;range=1d;indicator=volume;charttype=line ;crosshair=on;ohlcvalues=0;logscale =on;来源=未定义

How can I add the ability for a tooltip for a point to show when the
mouse "hovers" over that point OR any pixel on an imaginary vertical line above/below that point?

My graph only has 1 line on it. I
would like it so a tooltip can show for the datapoint when the mouse
goes over an "invisible" vertical line through the point - not forcing
the user to explicitly hover over the point.

I found this solution - but it seems like a hack (and doesn't work):
Flot mousehover algorithm to detect purely based on x location?

Is there any better way to do this short of hacking the source?

Here is an example of what I'm talking about (its bigcharts, but this
is the function I want). You can hover over the line or hover over
any point on the X axis to show the point that is on that X axis
pixel:
http://finance.yahoo.com/echarts?s=^DJI+Interactive#chart1:symbol=^dji;range=1d;indicator=volume;charttype=line ;crosshair=on;ohlcvalues=0;logscale=on;source=undefined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2024-11-24 19:54:09

我能够在这个 fiddle 中执行类似于雅虎图表的操作。我只是在鼠标悬停时更新 x 位置:

$("#placeholder").bind("plothover", function (event, pos, item) {
    latestPosition = pos;
    if (!updateTooltipTimeout) {
        updateTooltipTimeout = setTimeout(update, 30);
    }
});

然后运行更新方法。 update 方法(取自 flot crosshairs 示例)对 y 数据点进行插值最接近 x,然后在线上显示工具提示和点:

function update() {
    updateTooltipTimeout = null;
    var pos = latestPosition;

    var i, j, dataset = plot.getData();
    for (i = 0; i < dataset.length; ++i) {
        var series = dataset[i];

        // Find the nearest points, x-wise
        for (j = 0; j < series.data.length; ++j) {
            if (series.data[j][0] > pos.x) {
                break;
            }
        }

        // Now Interpolate
        var y,
        p1 = series.data[j - 1],
            p2 = series.data[j];

        if (p1 == null) {
            y = p2[1];
        } else if (p2 == null) {
            y = p1[1];
        } else {
            y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);
        }

        var o = plot.pointOffset({
            x: pos.x,
            y: y
        });

        updateTooltip(o.left, o.top,
            "x: " + pos.x.toFixed(2) + " y: " + y.toFixed(2));

        plotPoint(o.left, o.top, 5, "rgb(153,180,125)", "circle");
    }
}

我认为为了实际与数据点交互,您必须更改源。

I was able to do something similar to the Yahoo chart in this fiddle. I just update the x position whenever the mouse is hovering:

$("#placeholder").bind("plothover", function (event, pos, item) {
    latestPosition = pos;
    if (!updateTooltipTimeout) {
        updateTooltipTimeout = setTimeout(update, 30);
    }
});

then run the update method. The update method (taken from the flot crosshairs example) interpolates the y datapoint closest to x, then displays a tooltip and point on the line:

function update() {
    updateTooltipTimeout = null;
    var pos = latestPosition;

    var i, j, dataset = plot.getData();
    for (i = 0; i < dataset.length; ++i) {
        var series = dataset[i];

        // Find the nearest points, x-wise
        for (j = 0; j < series.data.length; ++j) {
            if (series.data[j][0] > pos.x) {
                break;
            }
        }

        // Now Interpolate
        var y,
        p1 = series.data[j - 1],
            p2 = series.data[j];

        if (p1 == null) {
            y = p2[1];
        } else if (p2 == null) {
            y = p1[1];
        } else {
            y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);
        }

        var o = plot.pointOffset({
            x: pos.x,
            y: y
        });

        updateTooltip(o.left, o.top,
            "x: " + pos.x.toFixed(2) + " y: " + y.toFixed(2));

        plotPoint(o.left, o.top, 5, "rgb(153,180,125)", "circle");
    }
}

I'm thinking that in order to actually interact with the datapoints, you will have to alter the source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文