1

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

plotpan但是,自从我包含该功能以来,图例的唯一一次更新是在 aplotpan发生之后。我不确定是什么原因造成的,因此我无法解决这个问题。这是 JSFiddle,它比以下孤立的代码段更有帮助。

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

1 回答 1

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. 对我来说似乎过于复杂了,你可以简化一下。

更新小提琴

于 2013-07-20T15:56:47.490 回答