2016-07-14 86 views
1

我正在使用dygraph来监视CSV文件并使用动态更新功能。当我将鼠标悬停在图表上以显示图例中的曲线值时,只要图形更新就会消失,这有点烦人。Dygraph动态更新图例值消失

<html> 
<head> 
<script type="text/javascript" src="/static/dygraph-combined.js"></script></head> 
<body> 
<div id="psu"></div> 
<script type="text/javascript"> 
    g = new Dygraph(document.getElementById("psu"), "/data/psu", 
    { 
     legend: 'always', 
     hideOverlayOnMouseOut: false, 
     ylabel: 'current (A)', 
     height: 480, 
     width: 640, 
     sigFigs: 2, 
     title: 'power interface monitor', 
     xValueFormatter: Dygraph.dateString_, 
     xAxisLabelFormatter: Dygraph.dateString_, 
     xTicker: Dygraph.dateTicker 
    }); 
    window.intervalId = setInterval(function(){g.updateOptions({ 'file': "/data/psu" }); }, 1000); 
</script> 
</html> 

所以图是所有正确显示和数据更新,只有传说值消失后刷新图与g.updateOptions()。我在想也许我可以在g.updateOptions()之后重新触发某种"mouseover"事件,所以这些值会回来,但可能会有更清晰的方式。

谢谢。

回答

0

我找到了解决我的问题的方法,但我不确定它是如何实施的。我在这里分享,让其他人可能会发现:

$(document).ready(function() { 
 
    var data = []; 
 
    var t = new Date(); 
 
    for (var i = 10; i >= 0; i--) { 
 
    var x = new Date(t.getTime() - i * 1000); 
 
    data.push([x, Math.random()]); 
 
    } 
 

 
    var last_mousemove_evt = null; 
 
    var on_graph = false; 
 

 
    var g = new Dygraph(document.getElementById("div_g"), data, { 
 
    legend: 'always', 
 
    drawPoints: true, 
 
    showRoller: true, 
 
    valueRange: [0.0, 1.2], 
 
    labels: ['Time', 'Random'], 
 
    highlightCallback: function(e, x, pts, row) { 
 
     last_mousemove_evt = e; 
 
     on_graph = true 
 
    }, 
 
    unhighlightCallback: function(e) { 
 
     on_graph = false; 
 
    } 
 
    }); 
 
    // It sucks that these things aren't objects, and we need to store state in window. 
 
    window.intervalId = setInterval(function() { 
 
    var x = new Date(); // current time 
 
    var y = Math.random(); 
 
    data.push([x, y]); 
 
    g.updateOptions({ 
 
     'file': data 
 
    }); 
 
    if (on_graph) { 
 
     g.mouseMove_(last_mousemove_evt); 
 
    } 
 
    }, 1000); 
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
 
<div id="div_g" style="width:600px; height:300px;"></div>

所以我最终使用的highlightCallbackunhighlightCallback选项,这样我可以计算出鼠标的位置和动态更新调用然后dygraph.mouseMove_()后函数来重绘图例值。似乎工作。

请让我知道是否有更好的解决方案。在默认情况下,将此功能包含在dygraph.updateOptions()中可能会更好,因为看起来奇怪的是图例值在更新后消失了。