The exit method:
Returns the exit selection: existing DOM elements in the selection for which no new datum was found. (The exit selection is empty for selections not returned by selection.data.) (API documentation).
While you have specified data for the selections
background, you have not specified any data for the selection
svg, so the exit selection will be empty. Consequently, .remove() can't remove anything.
To use an exit selection, note that the exit selection doesn't remove elements in a selection. It is a selection of a subset of elements in a selection that no longer have corresponding bound data.
If a selection holds 10 DOM elements, and the selection's data array is set to an array with 9 elements, the exit selection will contain one element: the excess element.
We can remove this subset with .exit().remove(). The exit is which elements we no longer need, and remove() gets rid of them. Exit doesn't remove elements because we may want to still do something with them, such as transition them, or hide them, etc.
As noted in the quote above, to populate an exit selection you must use selection.data():
var selection = svg.selectAll("path") .data(data);
Now we can access the enter selection, the exit selection, and the update selection.
Unless specifying a key, only one of the enter or exit selections can contain elements: if we need more elements we don't need to exit any, if we have excess elements, we don't need any new ones.
To remove excess elements, we can now use:
var selection = svg.selectAll("path") .data(data); selection.exit().remove();
Given you are specifying data for other selections such as
g, these are the selections you should be using .exit() on, as part of a complete enter/update/exit cycle. Keep in mind, in d3v4 you need to merge the update and enter selections in order to perform operations on pre-existing and new elements in the selection.
However, if you simply want to get rid of the svg and start fresh (which is what it looks like you want to do - as you are trying to remove the svg) you can simply remove the svg:
d3.select("#test") .select("svg") .remove();
However, this won't allow you transition nicely between charts or utilize the enter/update/exit cycle.
Why note use svg.remove()? Because the selection
svg holds a
var svg = d3.select("#test") // returns a selection with #test .append("svg:svg") // returns a selection with the new svg .attr(...) // returns the selection with the newly created svg again .append("svg:g") // returns a selection with the new g .attr(...); // returns the selection with the newly created g again
svg is consequently a selection of a
g, and removing it won't remove the svg element. In fact running the above code block after
svg.remove() will add a new svg element to the DOM while not removing the old one.
- d3.js:refresh the chart with new dataset
- D3 How to update the chart after selection from drop down menu with new data
- Why doesn't my pie chart update with the new size?
- d3 bar chart labels not getting updated on updating the chart with the new data
- d3.js - updating stacked bar chart with new dataset
- updating d3 chart with new data, the old data points not removed
- d3js open a new tab with the country id in map chart
- D3: How to refresh a chart with new data?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How to update d3.js bar chart with new data
- Make simple bar chart using C3 with separate columns on the x axis
- D3 updating graph with new elements create edges with the wrong nodes
- D3 update dataset on click and redraw the bar chart
- New layout after opening a group not base on the last layout with cola.js
- update d3 pie chart with new data.json
- d3.js chart not updated with new data
- Sorting the bars in a bar chart with dc.js
- D3 v4 - Pie Chart with connectors outside the pie and with dots.
- D3.js - Highlight chart elements when interacting with the legend & vice versa
- D3: Sunburst chart with root node on the outside ring
- D3.js: plot dots on the existing line in a multiseries line chart with long formatted data
- d3js gantt chart with date/time scale at the top and current day blue line
- Placing Image in the Center of a Sunburst Chart with D3.js
- Is it possible to insert an icon in the arc of a donut chart with D3?
- Transitioning a bar chart with negative values for the width
- d3.js mapping a dataset into a new one with different keys
- D3.js bar chart 'enter' appears to draw the new bars in wrong position
- Create a bar chart with local storage data (drawing the bars doesn't work)
- update d3 chart with new data
- Edit the data in a pie chart rather than create a new one
More Query from same tag
- d3js: Nodes don't collapse correctly in my tree diagram
- d3.v4: How to use linkRadial instead of linkVertical/Horizontal
- D3Node getComputedTextLength() is not a function
- D3: how to create a bar chart by row with a csv file
- How to assign colors according to a index in d3.js?
- Issue with responsive d3 graph
- Pie chart on tooltip hover on multi bar chart nvd3
- use d3 brush to select element in a linechart
- D3 - rotating rect text elements individually instead of as group
- d3 choropleth map is extremely small
- Why the graph lines are off axis Y and X?
- d3 svg to fill screen
- d3.js inline-block elements are failing in Firefox and IE
- d3.js combining Hierarchical Edge Bundling and Radial Reingold–Tilford Tree + data
- D3 padding dates and values
- D3.js redraw graph on button click
- Four color theorem in D3js for neighbors polygons coloring?
- D3js - How do I make my visualization responsive?
- How data in a d3 scatterplot could be referenced/called outside d3 selection
- unresolved variable d3.event.translate and d3.event.scale
- Extra legend present
- D3 Bar Chart Zoom Not Centered
- Retrieve value from password field
- C3js change axis time format on load
- Recursive circle packing?
- dc.js - Listening for chart group render
- resetting fill color in d3
- Default values in json object based on contents of other objects in the same array
- Unable to properly remove a D3 element