我希望你能帮助我,因为这让我抓狂!
所以我尝试使用 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,然后再次添加它。
希望您能帮助解决问题,我们将不胜感激!
最佳答案
我遇到了类似的问题。删除 SVG 元素不允许我像我想要的那样完全更新数据。
相反,我使用以下行删除了在 SVG 中创建的 g 元素:
d3.selectAll("g > *").remove()
在我的 updateGraph() 函数中
此处显示了完整的解释和情况: Repainting/Refreshing Graph in D3
关于javascript - 删除重绘的 d3 svg 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22382984/