Comments (2)
This is an open-ended question as there are lots of ways to solve this problem. Two options immediately come to mind.
One option is that wherever your code currently has data nodes, change that code so that it has hierarchy nodes. If you always have hierarchy nodes, you can use hierarchy.parent and hierarchy.children to search, and you never need to map from a data node to a hierarchy node. Ideally, you do this when you load your data, so your code only sees hierarchy nodes and never the “raw” data (be it from CSV, JSON, whatever).
Another option would be to add bidirectional links from the data nodes to the containing hierarchy nodes. There’s already hierarchy.data to link from hierarchy to data, but you can iterate over the hierarchy and add a back-link:
root.each(function(node) {
node.data.node = node;
});
This approach assumes there is a 1-to-1 relationship between the data and the hierarchy, which is probably the case, but isn’t required. (As in, this approach precludes you from using the same data to generate multiple hierarchical visualizations that share the same data object.)
A third approach would be to create a Map that explicitly maps from data to hierarchy. That way if you want multiple _hierarchy_s you can have multiple _map_s.
If you want more assistance, please use Stack Overflow tag d3.js to ask for help. Although I make an effort to assist everyone that asks, I am not always available to provide help promptly or directly. Stack Overflow provides a better collaborative forum for self-help: tens of thousands of D3-related questions have already been asked there, and some answered questions may be relevant to you.
When asking for help, please include a link to a live example that demonstrates the issue, preferably on bl.ocks.org. It is often impossible to debug from code snippets alone. Isolate the issue and reduce your code as much as possible before asking for help. The less code you post, the easier it is for someone to debug, and the more likely you are to get a helpful response.
If you have a question about D3’s behavior and want to discuss it with other users, also consider the d3-js Google Group or joining the d3-js Slack.
Thank you! 🤗
from d3-hierarchy.
I would like to avoid creating a new issue for this, so I post my question here.
I wonder if there is a possibility to create the back-links/mapping on-the-fly when calling d3.hierarchy
, d3.stratify()
so I don't need to traverse the tree 2.nd time
root.each(function(node) {
...
});
Background: I would like to create a id -> hierarchy node look-up table os I can access nodes instantly.
from d3-hierarchy.
Related Issues (20)
- How can I handle the ts error "Type 'number | undefined' is not assignable to type 'number'." ? HOT 1
- IE11 issue - Const must be initialized in each.js HOT 1
- the json result convert to d3-hierarchy can not generate the correct format without children HOT 1
- tree layout: Support transparent intermediary nodes to keep the tree tidy HOT 3
- Undesirable padding behaviour with disparately sized children HOT 3
- node.sum/count for non-numeric data
- New nodesAtDepth(depth) function HOT 3
- d3 partition: ability to choose different "value" size variable
- document d3.Node
- Seemingly non-weird ball collection throws an error in d3.packEnclose() HOT 1
- Suggestion: pack() layouts with deterministic outputs HOT 5
- Inner and outer padding for partition layout
- How can you add new attributes to d3.heirarchy() object? HOT 1
- why non-negative for node.sum
- Using @ant-design/plots and this error is popping up
- d3.stratify().path() doesn't bind data HOT 1
- Stratify nodes that are both a leaf (with value) and a parent
- d3__WEBPACK_IMPORTED_MODULE_0__.hierarchy is not a function HOT 3
- d3__WEBPACK_IMPORTED_MODULE_0__.hierarchy is not a function HOT 4
- Circle packing performance when there are a lot of nodes at the same level HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from d3-hierarchy.