简体   繁体   English

用Flot确定悬停点?

[英]identifying hovered point with Flot?

Hopefully this questions isn't too confusing or long, I'm working with the Flot examples, specifically this one. 希望这个问题不会太混乱或太长,我正在研究Flot示例,尤其是这个示例

I'm using flot to graph some data I've been gathering into a Scatter chart. 我正在使用flot绘制一些我一直在收集到散点图中的数据的图形。 I'm using the following function to do so... 我正在使用以下功能来做到这一点...

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}
        });
  });

}

My code generates a Scatter chart with various points. 我的代码生成具有多个点的散点图。 When I hover over a point the following listener gets activated... 当我将鼠标悬停在某个点上时,以下监听器将被激活...

$("#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;            
    }

});

where showTooltip is defined is... 定义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);
}

Basically, when hovering over a point I'd like to render the value for url added with the point to d1 but I haven't been able to because the item object doesn't return url inside item.datapoint only the x,y value of the points are returned. 基本上,当我将鼠标悬停在某个点上时,我想渲染添加到d1的点的url的值,但是我无法这样做,因为item对象不在item.datapoint返回url ,仅返回x,y值的点数返回。 url is included inside item but under item.data in an array with all of the other points in the graph. url包含在item但在item.data中的item.data下, item.data中的所有其他点。

My question is, either identifying the point uniquely from the array listed in item.data or forcing flot to include url inside item.datapoint is there a way to get the url to a corresponding point? 我的问题是,要么从item.data列出的数组中唯一标识该点,要么迫使floturl包含在item.datapoint是否有办法将url指向相应的点?

如果这样定义你的数据结构,那么你应该能够得到的URL与(例如你的plothover回调位置 ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM