简体   繁体   English

清除D3.js画布

[英]Clearing D3.js canvas

I am using D3 to produce a family tree graph, (based on the code presented here: http://www.d3noob.org/2014/01/tree-diagrams-in-d3js_11.html ) and the graph data comes from a server and the user can select which family he wants to view, then clicks the submit button which returns the data and then D3 draws the graph. 我正在使用D3生成家谱图(基于此处提供的代码: http : //www.d3noob.org/2014/01/tree-diagrams-in-d3js_11.html ),该图数据来自服务器和用户可以选择他要查看的家庭,然后单击“提交”按钮以返回数据,然后D3绘制图形。 However, if the user chooses another family (or the same one for that matter) and presses submit without refreshing the page, the new graph is drawn below the old one. 但是,如果用户选择另一个家庭(或与此相同的家庭)并按提交而不刷新页面,则新图形将绘制在旧家庭下方。 How can I clear the D3.js canvas so that the new graph is the only thing seen? 如何清除D3.js画布,以便仅看到新图形?

Apply .remove() to the exiting selection. .remove()应用于现有选择。

// Set new data on your chart:
var items = d3.select('svg').selectAll('.item').data(newData);

// Remove old elements:
items.exit().remove();

Use the following code before drawing the graph. 在绘制图形之前,请使用以下代码。 Hope this will help. 希望这会有所帮助。

d3.select('svg').selectAll('*').remove();

在使用容器的情况下,使用JQuery空方法清除容器解决了我的问题

$("#svgCanvasDiv").empty();

上面的方法在某些情况下有效,但是我发现清除画布的最简单方法是将其绘制在容器中,然后再绘制任何东西,使用以下方法清除容器:d3.select(“#container”)。html(null );

You can clear the canvas using 您可以使用清除画布

  d3.select('svg').remove();

The new graph will be drawn in this cleared out space. 新图形将在此清除空间中绘制。

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

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