2013-03-11 61 views
36

我想創建一個實時圖,使用nvd3.js將定期更新,並有數據實時處理的印象。與nvd3.js實時線圖

現在我已經能夠創建一個函數來定期更新圖形,但是我無法像在向左轉換的線那樣在「狀態」之間進行平滑過渡。

Here是我使用的是什麼nvd3.js,這裏的有趣的代碼是:

d3.select('#chart svg') 
    .datum(data) 
    .transition().duration(duration) 
    .call(chart); 

現在,我已經能夠生產我想用d3.js什麼,但我更希望能夠使用nvd3.js提供的所有工具。 Here是我想生產什麼用nvd3

使用d3.js過渡有趣的代碼是:

function tick() { 

    // update the domains 
    now = new Date(); 
    x.domain([now - (n - 2) * duration, now - duration]); 
    y.domain([0, d3.max(data)]); 

    // push the accumulated count onto the back, and reset the count 
    data.push(Math.random()*10); 
    count = 0; 

    // redraw the line 
    svg.select(".line") 
     .attr("d", line) 
     .attr("transform", null); 

    // slide the x-axis left 
    axis.transition() 
     .duration(duration) 
     .ease("linear") 
     .call(x.axis); 

    // slide the line left 
    path.transition() 
     .duration(duration) 
     .ease("linear") 
     .attr("transform", "translate(" + x(now - (n - 1) * duration) + ")") 
     .each("end", tick); 

    // pop the old data point off the front 
    data.shift(); 

} 

回答

12

你可能想看看: D3 Real-Time streamgraph (Graph Data Visualization)

特別是鏈接的回答: http://bost.ocks.org/mike/path/

一般來說,我看到兩種方法來處理垂直轉換問題:

  • 過採樣在實際點之間有一些線性插值,點之間的間隔越小,垂直轉換看起來就越「水平」(但缺點是會得到很多「假」點,這可能是不可接受的,取決於圖表的用途)
  • 通過在最後添加來擴展圖表,並在左側翻譯圖表,確保仍然可用的左側部分不顯示,直到您將其刪除(剪切它或做任何其他技巧),這是最好的,並且上面提到的解決方案確實如此
+0

我同意你所說的。不過我的問題是使用nvd3而不是d3。我的問題中的第一個鏈接顯示了我現在在nvd3中的內容,第二個鏈接顯示了我使用d3.js完全運行的版本。您的第二種方法是我用於d3解決方案的方法。 – 2013-05-16 23:40:31

+0

好吧,我的不好...你的解決方案已經在用d3來完成了 – 2013-05-17 04:17:34