Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NVD3, Clear svg before loading new chart

I have several different NVD3 charts that I call upon in the same svg. I use buttons to call functions, each containing a new chart that uses its own data.

Is there a way to clear my single svg without deleting it? I wish to press a button to call my chart, but clear the svg before the new chart is loaded.

It's not an issue when using the kind of chart... calling two multibarhorizontal charts, for example, just updates the shapes, which is fine. The problem is loading two different charts, like a line and a bar.

Thanks in advance

EDIT - The answers must be something like d3.select("svg").remove() but that just deletes the svg. I only want to clear it.

like image 574
JasTonAChair Avatar asked Mar 17 '14 10:03

JasTonAChair


People also ask

How do I clear SVG content?

In order to remove SVG content, you can use the remove() function provided by D3. js. The remove() function is used along with two methods which are also provided by D3. js.

How to clear SVG in D3 js?

It is common to remove the existing Scalable Vector Graphics (SVG) element by calling d3. select('#chart'). remove() , before rendering a new chart.

What is SVG selectAll?

svg. selectAll('rect') tells the browser to find the svg element and look inside it for any rectangles. If it finds rectangles, it returns them in a selection that is an array of elements. If it doesn't find any, it returns an empty selection, which is what will happen in this case.


2 Answers

You can select all the elements below the SVG with the "svg > *" selector, i.e. to remove all of those, do

d3.selectAll("svg > *").remove(); 
like image 138
Lars Kotthoff Avatar answered Sep 23 '22 00:09

Lars Kotthoff


This works for me:

var svg = d3.select("svg"); svg.selectAll("*").remove(); 
like image 40
kml Avatar answered Sep 26 '22 00:09

kml