Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I move an SVG element between SVG groups, in d3.js

Tags:

svg

d3.js

Can I move an SVG element between SVG groups - without stirring too much calculation behind the scenes nor crafting too much code in my own code? The d3 api documentation mentions you cannot re-append removed elements (?).

Recreating the element after removing it from its original group - may seem to be cumbersome in my code, and more expensive for d3 and the browser than it could be if that were possible.

This is also relevant for defining an element before associating it with a group, not just for really moving it between groups (i.e. for moving an element from a non-group to a group).

like image 611
matanster Avatar asked May 17 '14 16:05

matanster


People also ask

Can we group SVG elements in d3js?

The <g> SVG element is a container used to group other SVG elements. Transformations applied to the <g> element are performed on its child elements, and its attributes are inherited by its children. We can create a group element with D3. js by appending a g element using any selection.

How is SVG used with D3?

SVG stands for Scalable Vector Graphics. SVG is an XML-based vector graphics format. It provides options to draw different shapes such as Lines, Rectangles, Circles, Ellipses, etc. Hence, designing visualizations with SVG gives you more power and flexibility.

Does D3 use SVG or Canvas?

D3 charts are most often rendered using SVG, a retained mode graphics model, which is easy to use, but performance is limited. SVG charts can typically handle around 1,000 datapoints. Since D3 v4 you've also had the option to render charts using canvas, which is an immediate mode graphics model.

What is D3 and SVG?

Before we dive into D3, we'll take a quick look at SVG, which stands for Scalable Vector Graphics. SVG is a markup language for graphics. D3 is not limited to SVG. You can also use D3 to create visualizations using canvas, HTML elements, for example.


2 Answers

The docs you posted also mention that you can re-add the elements by passing a function to .append or .insert. Here's what it says:

Note that there is not currently a dedicated API to add removed elements back to the document; however, you can pass a function to selection.append or selection.insert to re-add elements.


Since using .remove on a selection returns that selection, you could store the removed element in a variable, and then .append it to the new group using selection.node() to grab the DOM element from the removed selection:

var removed = yourSelection.remove();
yourTargetGroup.append(function() {
  return removed.node();
});

Here's a simple Fiddle that uses this technique to move a circle element from one group to another in a click handler.

like image 193
jshanley Avatar answered Sep 20 '22 17:09

jshanley


To just move around existing nodes within the DOM tree it is not necessary to first remove them to later re-append those same nodes at a different position. d3.append() internally uses Node.appendChild() which works as follows:

If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position (there is no requirement to remove the node from its parent node before appending it to some other node).

This means that a node can't be in two points of the document simultaneously. So if the node already has a parent, the node is first removed, then appended at the new position.

As other answerers have already mentioned both selection.append() and selection.insert() when being passed a function will append or insert, respectively, the node returned by that function. Knowing that, all it takes to move around a DOM node from its current position to a new target is

target.append(() => existingNode);  // target is a D3 selection, existingNode is a native DOM node

Having this in your toolbox it is also fairly easy to move around multiple nodes at once:

selectionToBeMoved.each(function() { target.append(() => this); });

Have a look at the following demo to see it in action. The two circles colored in Aquamarine are move from group #g1 to group #g2.

const current = d3.select("#g1");
const target  = d3.select("#g2");

current.selectAll("circle[fill=Aquamarine]")
  .each(function() { target.append(() => this); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.15.0/d3.min.js"></script>

<svg>
  <g id="g1">
    <circle cx="100" cy="100" r="20" fill="Aquamarine" />
    <circle cx="150" cy="100" r="20" fill="HotPink" />
    <circle cx="200" cy="100" r="20" fill="Aquamarine" />
  </g>
  <g id="g2">
  </g>
</svg>
like image 35
altocumulus Avatar answered Sep 20 '22 17:09

altocumulus