Flot:只有当plotpan事件发生时,图例才会更新?

时间:2013-07-20 05:00:24

标签: javascript flot

只有plotpan事件发生时,我的图表图例才会出现。这是我在下面找到的updateLegend函数,我确信程序当然会使用跟踪消息

但是,自我添加plotpan功能以来,图例更新的唯一时间就在plotpan发生后。我不确定导致这种情况的原因,因此我无法解决问题。 Here is the JSFiddle that will be more helpful than the following isolated segment of code.

var updateLegendTimeout = null;
var latestPosition = null;
 function updateLegend(){
        var series = (plot.getData())[0];
        legends.eq(0).text(series.label ="x: " + (local_x)+" y: "+ (local_y));
 }  


placeholder.bind("plothover",  function (event, pos, item) {
if (item){
    local_x = item.datapoint[0].toFixed(2);
        local_y = item.datapoint[1].toFixed(2);
    console.log("x:" + local_x + ", " + "y:" + local_y);
}

if (!updateLegendTimeout){
    updateLegendTimeout = setTimeout(updateLegend, 50);
        updateLegendTimeout = null;
}
});

1 个答案:

答案 0 :(得分:3)

这行代码到底意味着什么?

legends.eq(0).text(series.label ="x: " + (x)+" y: "+ (y));

它似乎正在分配series.label但我不相信它实际上正在修改图例div的内容。但是,当你平移时它会更新,因为这会强制重绘网格(重绘图例)。

最简单的解决方法是在更改图例后手动调用setupGrid

function updateLegend(x,y){
  var series = (plot.getData())[0];
  var legends = $(placeholder_id+ ".legendLabel");
  series.label ="x: " + (x)+" y: "+ (y);
  plot.setupGrid();
  clearTimeout(updateLegendTimeout);
}

这是相对昂贵的(在每次鼠标移动时重新绘制网格)。另一种攻击方式是手动设置图例div的文本,但这可能会干扰flots内部图例绘制。如果你真的想要显示最近的点位置,也许只留下传说并用你自己的div进行。

最后,我不太确定你要去哪里setTimeout。对我来说似乎过于复杂,你可以简化这一点。

更新fiddle