Skip to content
Advertisement

Move g element for centering them in a tree layout

I would like move the g element include multiple tspan text to enter.

JavaScript
JavaScript

The attribute dx and dy doesn’t work in this example. what’s the proper way to move the g element to make it move to center?

Advertisement

Answer

For repositioning them dynamically, an easy approach is getting the size of the element and translating it up/left by half its height/width:

JavaScript

Here is your code with that change:

JavaScript
JavaScript
Advertisement