2013-06-02 27 views
0

我用一些數據通過時間來繪製圓,更新過程是好的,但出口不行,這裏是我的代碼部分:爲什麼exit()不能在這裏工作?

function update(data) { 

     var tmp1 = group.selectAll('.circle').data(data) 
     tmp1.enter().append('circle') 
       .attr('cx', function(d) {coords = projection([d.long,d.lat]); return coords[0]}) 
       .attr('cy', function(d) {coords = projection([d.long,d.lat]); return coords[1]}) 
       .attr('r', function(d) { return countScale(d.count)}) 
       .attr("stroke", function(d, i) { return color(d.name, i);}) 
       .attr("stroke-width", 2.3) 
       .style('fill', function(d) { 
         if (d.count == 0){ return 'white';} 
         if (d.count !== 0){ return color(d.name);} 
        }); 
       tmp1.exit().remove(); 
} 

後,我用setInterval來更新我的數據,但出口不工作,之前的圈子仍然退出。

setInterval(function() { update(nextDay(data)) }, 10); 

回答

1

selectAll(".circle")由類名「圈」中選擇,但隨後要附加circle元素,而不是設置類屬性的匹配選擇。所以你的退出選擇總是空的,因爲沒有匹配的元素。

您是不是想要selectAll("circle")(不是前導.)按照元素名來選擇呢?

(另外,你可能需要一個key function和閱讀有關general update pattern

+0

我解決我的問題,非常感謝,很高興你回答我的問題,你一般更新模式教程幫助了我很多! – DoReMi

+0

很高興我能幫到你。如果我回答了您的問題,請接受帶有複選標記的答案。 – mbostock