简体   繁体   English

删除d3 svg元素以进行重绘

[英]Deleting d3 svg elements for redraw

I hope you can help me, because this is driving me nuts! 我希望你能帮助我,因为这让我疯了!

So i was trying to redraw a svg using d3. 所以我试图用d3重绘一个svg。 In my code i add the svg using: 在我的代码中,我使用以下命令添加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);

  });

When my update function is called i then proceed to remove the element: 当我的更新函数被调用时,我继续删除元素:

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

This removes the container and the element. 这将删除容器和元素。 I then proceed with redrawing the svg again using the above code. 然后我继续使用上面的代码重新绘制svg。

My problem is that when it appends the svg again it does it twice and i do not understand why! 我的问题是,当它再次附加svg它会做两次,我不明白为什么! It seems that d3 somehow caches the svg, adding it again. 似乎d3以某种方式缓存svg,再次添加它。

Hope you can help to clear out what is wrong, any help would be appreciated! 希望你能帮助清除什么是错的,任何帮助将不胜感激!

I had a similar problem to this. 我遇到了类似的问题。 removing the SVG element did not allow me to fully update the data like I wanted. 删除SVG元素不允许我像我想要的那样完全更新数据。

Instead, I removed the g elements created inside the SVG with this line: 相反,我使用以下行删除了在SVG内创建的g元素:

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

in my updateGraph() function 在我的updateGraph()函数中

Full explanation and situation shown here: Repainting/Refreshing Graph in D3 此处显示的完整说明和情况:重新绘制/刷新D3中的图形

FIDDLE with a quick example of adding, removing and the adding again an SVG and a contained circle. FIDDLE快速添加,删除和添加SVG和包含的圆圈的示例。 Hope this helps. 希望这可以帮助。

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM