首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Flot识别悬浮点?

使用Flot识别悬浮点?
EN

Stack Overflow用户
提问于 2012-02-06 00:41:49
回答 1查看 4.7K关注 0票数 2

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

我正在使用flot将我收集到的一些数据绘制成散点图。我使用以下函数来完成此操作...

代码语言:javascript
复制
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}
        });
  });

}

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

代码语言:javascript
复制
$("#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的位置是...

代码语言:javascript
复制
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);
}

基本上,当鼠标悬停在某个点上时,我希望将添加了该点的url的值呈现给d1,但我无法做到这一点,因为item对象在item.datapoint中不返回url,只返回这些点的x,y值。url包含在item中,但在item.data下,与图中的所有其他点在一个数组中。

我的问题是,无论是从item.data中列出的数组中唯一地确定点,还是强制flotitem.datapoint中包含url,有没有办法让url到达相应的点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-06 01:03:55

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

代码语言:javascript
复制
item.series.data[item.dataIndex][2].url
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9150964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档