score:150
Your answer will work, but for posterity, these methods are more generic.
Remove all children from HTML:
d3.select("div.parent").html("");
Remove all children from SVG/HTML:
d3.select("g.parent").selectAll("*").remove();
The .html("")
call works with my SVG, but it might be a side effect of using innerSVG.
score:3
To remove all element from a node:
var siblings = element.parentNode.childNodes;
for (var i = 0; i < siblings.length; i++) {
for (var j = 0; j < siblings.length; j++) {
siblings[i].parentElement.removeChild(siblings[j]);
}
}`
score:7
in this way, I have resolved it very easily,
visualRoot.selectAll(".circle").remove();
visualRoot.selectAll(".image").remove();
and then I just re-added visual elements which were rendered differently because the code for calculating radius and color had changed properties. Thank you.
score:7
If you want to remove the element itself, just use element.remove()
, as you did. In case you just want to remove the content of the element, but keep the element as-is, you can use f.ex.
visualRoot.selectAll(".circle").html(null);
visualRoot.selectAll(".image").html(null);
instead of .html("")
(I wasn't sure which element's children you want deleted). This keeps the element itself, but cleans all included content. It the official way to do this, so should work cross-browser.
PS: you wanted to change the circle sizes. Have you tried
d3.selectAll(".circle").attr("r", newValue);
score:9
My first advice is that you should read the d3.js
API about selections: https://github.com/mbostock/d3/wiki/Selections
You have to understand how the enter()
command works (API). The fact that you have to use it to handle new nodes has a meaning which will help you.
Here is the basic process when you deal with selection.data()
:
first you want to "attach" some data to the selection. So you have:
var nodes = visualRoot.selectAll(".node") .data(graphData.nodes)
Then you can modify all nodes each times data is changed (this will do exactly what you want). If for example you change the radius of old nodes which are in the new dataset you loaded
nodes.attr("r", function(d){return d.radius})
Then, you have to handle new nodes, for this you have to select the new nodes, this is what
selection.enter()
is made for:var nodesEnter = nodes.enter() .attr("fill", "red") .attr("r", function(d){return d.radius})
Finally you certainly want to remove the nodes you don't want anymore, to do this, you have to select them, this is what
selection.exit()
is made for.var nodesRemove = nodes.exit().remove()
A good example of the whole process can also be found on the API wiki: https://github.com/mbostock/d3/wiki/Selections#wiki-exit
Source: stackoverflow.com
Related Query
- How do I remove all children elements from a node and then apply them again with different color and size?
- Create unique DOM elements with D3.js and update them but not append them again
- how to append a circle with a label to indicate information to rectangle node and remove the circle before download
- How to select elements from array field in dc.js / crossfilter / d3 and use each of them as separate data point for chart?
- 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 pull data from mysql database and visualize with D3.JS?
- How to check d3 js force graph for nodes with no links and remove them?
- Removing node and its children from d3js tree layout
- How to summarize an array with group and rollup from d3-array?
- How do you translate HTML elements along with SVG elements when zooming and panning with D3
- Adding and removing elements from D3 Visualization with animation
- JS- how to remove duplicate JSON nodes and add one link for all nodes that get merged
- How to select first two elements in d3 and apply transition to them?
- How to add links from CSV file to SVG elements generated with D3?
- D3 nodes and links from JSON with nested arrays of children
- How do i select all nodes and apply a css style to all of them, D3
- How do I replace a node with an image using D3 and CoffeeScript for a network visualization?
- How to retrieve Vector Tiles from Mapbox with d3.js and convert to geojson?
- How to implement D3 scales to have children inherit colour from parent with graduations?
- How to customize nvd3's tooltips and remove labels from x axis
- D3 Object Constancy not working with key function. Enter and Update contain all elements every time
- how to remove zero if hours less then 10 from d3.time.format("%I %p")
- How do I convert strings from csv in d3.js and be able to use them as a dataset?
- Remove all .fixed classes from force layout nodes with jQuery
- How to select all elements with a class that contains a str?
- How to highlight a source node and all its target nodes and corresponding links in d3 hierarchical edge bundling
- How to create a d3.js legend with both solid and dashed elements
- How can I apply a clipPath in SVG with multiple paths and NOT clip out the area between the paths?
- Svg how to position elements lo left and right like with floats?
- how to find all leaves of group with javascript and json
More Query from same tag
- SVG disappears in firefox on transform
- D3 divgrid.js with json data
- custom no data label in nvd3 scatter chart
- D3.js - Text not appearing in circles
- Best way to arrange tick text using d3
- Difference between d3.event vs d3.Event
- Create dynamic vertical rule in d3
- nvd3 displaying large black dots, and not fully rendering
- Using d3 selection.join to create a new line each time
- Javascript Library to dynamically create graphs?
- Why d3.scaleLinear domain impacts bars offset in V4?
- Infographic conditional fill on custom SVG shapes
- Update multi-line graph with D3
- How to draw a simple bar chart where each bar is of different color?
- Rescale an SVG in CSS
- Why am I getting the drop-down value as `undefined' on console.log?
- d3 stacked bar chart in angular4
- Handling optional data in d3.js
- Data points and ticks in the scaleBand axis are not aligned
- How can I pass a very long string from one page to another?
- python: how to save dynamically rendered html web page code
- d3 js: creating a nested array (matrix) from a flat array
- D3 Scaling issue of Graph with appropriate scaling of Scales(Y-axis,X-axis)
- Why do my svg nodes leak memory in IE
- d3 - help required in node
- Handling Memory Bloats in Javascript
- d3.js foreach loop on all the paths contained in a svg
- javascript polar scatter plot using d3.js
- How to select JSON arrays with D3.js with . operator
- Read Array of Arrays Like CSV