2016-04-24 105 views
-1

我有一個帶有頻繁變化數據點的d3圖,我想在圖中顯示。目前,我每天都在數據對象被更新時重繪的全部劇情:如何更新d3圖形的數據?

container.remove() 
container = svg.append("g"); 
drawContainer(); 

與drawContainer繪圖功能:

container.selectAll(".dot") 
    .data(datajson) 
    .enter().append("circle") 
    .attr("class", "dot") 
    .attr("r", 3.5) 
    .attr("cx", xMap) 
    .attr("cy", yMap) 
    .style("fill", function(d) { return color(cValue(d));}) 

我想簡單地調用:

container.selectAll(".dot") 
     .data(newdata) 

添加新的數據點,但這並沒有使新的數據點可見。我怎樣才能更新只有新的數據點的情節?

+1

這裏一個很好的閱讀關於如何創建更新刪除寶在d3中使用enter in exit輸入d3 https://medium.com/@c_behrens/enter-update-exit-6cafc6014c36 – Cyril

回答

1

我想你可以做同樣的事情來調用drawContainer();

而不是

container.selectAll(".dot") 
    .data(newdata); 

你可以這樣做:

var dots = container.selectAll(".dot") 
    .data(newdata); 
dots.enter().append("circle") 
    .attr("class", "dot") 
    .attr("r", 3.5) 
    .attr("cx", xMap) 
    .attr("cy", yMap) 
    .style("fill", function(d) { return color(cValue(d));}); 
dots.exit().remove(); 

對於一個完整的例子: https://bl.ocks.org/mbostock/3808218