2014-03-13 88 views
1

我希望你能幫助我,因爲這讓我瘋狂!刪除d3 svg元素進行重繪

所以我試圖用d3重繪一個svg。在我的代碼添加使用SVG:

d3.xml("Images/vertical_line.svg", "image/svg+xml", function(xml) { 

    var importedNode = document.importNode(xml.documentElement, true); 
    var svg = d3.select('#'+id+'_verticallinecontainer').node().appendChild(importedNode); 

    }); 

當我的更新函數被調用,然後我繼續要刪除的元素:

d3.select("#"+id+'_verticallinecontainer').remove(); 

這消除了容器和元素。然後我繼續使用上面的代碼重新繪製svg。

我的問題是,當它再次附加svg它做了兩次,我不明白爲什麼!似乎d3以某種方式緩存了svg,並再次添加了它。

希望你能幫助清理出錯的地方,任何幫助將不勝感激!

+0

有你試過'd3.select(svg).remove()'而不是? –

+0

應該svg是我想要刪除的元素的名稱? – Mkni1408

+0

不,變量'svg'。 –

回答

4

FIDDLE有一個快速添加,刪除和添加一個SVG和一個包含的圓的例子。希望這可以幫助。

function update() { 
    svg.remove(); 
    svg = d3.selectAll("body").append("svg"); 
    svg.append("circle") 
     .attr("cx",40) 
     .attr("cy",40) 
     .attr("r",20) 
     .style("fill","blue"); 
} 
3

我有類似的問題。刪除SVG元素並不能讓我完全更新我想要的數據。

相反,我刪除了SVG內創造了這一行摹元素:

充分說明和情況如下所示

d3.selectAll("g > *").remove() 

在我updateGraph()函數: Repainting/Refreshing Graph in D3