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.
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.
It is common to remove the existing Scalable Vector Graphics (SVG) element by calling d3. select('#chart'). remove() , before rendering a new chart.
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.
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();
This works for me:
var svg = d3.select("svg"); svg.selectAll("*").remove();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With