On page load, my D3 graph visualization looks like this, as expected:
However, after clicking the root node to collapse all connections, then clicking it again to expand them, the links to the root appear on top of the root node.
How can I fix this?
This only occurs with the root node.
My code is fairly long, so you can find it here.
Create two SVG groups (
<g>
) to act as layers, and keep your nodes and links in different layers.Here's the gist of it:
This saves cycles compared to shuffling elements to the back on every new entry, is more robust, and is easier to understand when inspecting the DOM tree.
When your links are re-inserted, they appear after your root node in the DOM. This affects the visibility and the rendering order. This only happens when all links are removed then inserted. Not sure how to fix it exactly though, but a "hack" is to do it like this:
Then, in the
update
function: