2016-07-14 90 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()中可能會更好,因爲看起來奇怪的是圖例值在更新後消失了。