-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] dynamic manipulation of trees #139
Comments
If I understand your question right: yes, it's possible to update hierarchy dynamically.
|
Is there any other ways to do it? It shouldn't be a difficult job to add a function like d3.hierarchy.addChildren(index, children) |
Yeah, d3-hierarchy doesn’t really support mutation of the hierarchy right now, but you can do it “in data” and then reconstruct the tree using d3.hierarchy(data). It’s fairly easy to detach and re-attach parts of the tree in the same place by manipulating the children array as shown above. But this can easily result in node.height that is inaccurate when the children have been removed, for example. So, we could have methods like node.add(child), node.remove(child), node.addAll(children) and node.removeAll(), which update the entire hierarchy as appropriate. That said, I find immutability tends to be cleaner than mutation, so we might think about designing the API to return new trees rather than modifying them in-place. For example, if node.sum returned a copy of the tree, then you could easily pass the same node to multiple hierarchical layouts with different methods of summation and they would co-exist happily. But immutability has a cost, too: by nature of the bidirectional parent-child links you’d then have to copy the entire tree right, and that could be prohibitive for large trees. At any rate, currently your options are:
|
I don't know if this is waterproof, but it looks like you can update the hierarchy with a little trick: // merge hierarchy B in hierarchy A as child of nodeA
B.parent = nodeOfA;
nodeOfA.children.push(B);
A = d3.hierarchy(A);
A.each((d) => (d.data = { ...d.data.data, children: d.children })); // fixes the glitch |
I am a bit lost when using tree / hierarchy in interactive dynamic context.
Similar to this project I am trying to migrate in v5:
http://bl.ocks.org/robschmuecker/7880033
In a dynamic context, since it should be data driven, I suppose I have to dynamically update my initial data and each time recompute the whole graph ?
It is a bit confusing , because I was thinking that I could modify the tree and the data will be updated automatically, but that does not seem being.
Please can you highlights which way I should use it in a dynamic context.
Thanks.
The text was updated successfully, but these errors were encountered: