Skip to content
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

Add DOM-style hierarchy traversal methods? #57

Closed
albell opened this issue Sep 11, 2016 · 1 comment
Closed

Add DOM-style hierarchy traversal methods? #57

albell opened this issue Sep 11, 2016 · 1 comment

Comments

@albell
Copy link

albell commented Sep 11, 2016

I am working with some large-ish hierarchies, with over 10,000 nodes, and a bit of custom data. Too large to be in the DOM all at once, so they need to be queried. I am finding that I often need to be able to traverse a hierarchy, starting from a given node, looking for a node that satisfies some condition. This is straightforward to do manually, but in results in a lot of for loops, which aren't really in the D3 idiom. The existing each-* methods have the slight performance disadvantage that there is no way early-exit the loop once the target node has been found. It might be nice to have that option as an API refinement.

Another option could be jQuery-esque .closest and .find methods that looked up and down the hierarchy, respectively, and returned the first "matching" node.

Thanks.

@mbostock
Copy link
Member

mbostock commented Sep 12, 2016

You can extend d3.hierarchy.prototype to define new methods on hierarchy nodes, as noted in the README. You can see the existing method implementations in src/hierarchy. The name node.find(filter) sounds fine for finding the first descendant in breadth-first order (I like the similarity to array.find), but I’d probably use the name node.findAncestor(filter) rather than node.closest since that’s more self-explanatory.

I don’t have any immediate plans to implement new methods, but if you implement something useful, feel free to send a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants