javascript - 用 Flot 识别悬停点?

标签 javascript jquery charts flot

希望这个问题不会太困惑或太长,我正在使用 Flot示例,特别是 this one.

我正在使用 flot 将我收集到的一些数据绘制成散点图。我正在使用以下函数来这样做...

function genScatter(){
    var no = getSelectedRepeat();
    $.get("getPages.json",{rid: no},function(data){
        var d1 = [];
        $.each(data,function(i,obj){
            d1.push([obj.queries,obj.count,{url: obj.url}]);
        })
        $.plot($("#scatter"), [ { label: "Pages",
            data: d1, 
            lines:{show: false},
            points:{show: true}}],{
            xaxis:{min: 1},
            grid:{ hoverable: true}
        });
  });

}

我的代码生成了一个包含多个点的散点图。当我将鼠标悬停在某个点上时,将激活以下监听器...

$("#scatter").bind("plothover", function (event, pos, item) {
$("#x").text(pos.x.toFixed(2));
$("#y").text(pos.y.toFixed(2));
    if (item) {
        if (previousPoint != item.dataIndex) {
            previousPoint = item.dataIndex;

            $("#tooltip").remove();
            var x = item.datapoint[0].toFixed(2),
                y = item.datapoint[1].toFixed(2);

            /*this would be the line where I extract
             the url and forward it to showToolTip.*/
            showTooltip(item.pageX, item.pageY,
                        item.series.label  + ": " + y);
        }
    }
    else {
        $("#tooltip").remove();
        previousPoint = null;            
    }

});

定义 showTooltip 的地方是...

function showTooltip(x, y, contents) {
   $('<div id="tooltip">' + contents + '</div>').css( {
        position: 'absolute',
        display: 'none',
        top: y + 5,
        left: x + 5,
        border: '1px solid #fdd',
        padding: '2px',
        'background-color': '#fee',
        opacity: 0.80
    }).appendTo("body").fadeIn(200);
}

基本上,当鼠标悬停在一个点上时,我想将添加到 d1url 的值呈现出来,但我无法做到,因为item 对象不会在 item.datapoint 中返回 url,仅返回点的 x、y 值。 url 包含在 item 中,但在 item.data 下,与图中所有其他点组成一个数组。

我的问题是,要么从 item.data 中列出的数组中唯一地识别点,要么强制 flot 中包含 url >item.datapoint 有没有办法把 url 到对应的点?

最佳答案

如果您这样定义数据结构,那么您应该能够在 plothover 回调中获取 url(例如 here):

item.series.data[item.dataIndex][2].url

关于javascript - 用 Flot 识别悬停点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9150964/

相关文章:

javascript - 模拟 useSelector 具有不同的值

VBA:为散点图中的误差线创建不同的格式

python - 在同一张图表上将 Pandas DataFrame 绘制为条形图和折线图

javascript - 将 JavaScript 代码更改为我自己脚本的 jQuery

javascript - .val() 函数正在 trim 特殊字符

javascript - 如何将计数器变量添加到变量名?

javascript - 如何在单击按钮时运行功能?

javascript - jQuery ajax 不适用于最新版本的 chrome

javascript - 滚动时触摸的 CSS 或 Javascript 事件

ios - 使用图表框架加载第二个图表时,我收到 exc_bad_access