Comments (4)
Stratify doesn’t do any parsing; it just assembles the tree using unique string identifiers for each node and its parent. So I am not sure I understand the proposed feature.
There is no requirement that these identifiers be named id
and parentId
; that is just the default implementation. And the node.data property represents the input to the stratify operator (each node wraps the corresponding input object), so that is more of a design consequence of the nodes than the stratify operator.
from d3-hierarchy.
@mbostock Thanks for the quick follow up.
While I'm aware they don't have to be named that, I've hit a few use cases where I don't want the id's to be located on the data object and would prefer the separation of concerns.
My thought was to allow for the user to define what the the data
property contained and keep the node related properties on the node itself without having to iterate over the tree to manipulate it afterwards. Sort of similar to the d3-dsv .csvParse()
method.
I've made a pull request that partially solves this. More detail in that PR. It's a minimal change, but if you feel that it's outside the scope of stratify
then feel free to close it.
from d3-hierarchy.
I think I see what you’re trying to do now. Superficially, I’d probably have named your proposed method stratify.data to make it clearer that it’s the accessor that defines the node.data property in the returned hierarchy (and I’d also have passed it the standard arguments including i
and data
).
But more importantly I don’t think this is the right approach, or at least, I think there’s already a reasonable way to do this, and one that doesn’t require adding a feature to d3.stratify. And that is to iterate over the hierarchy and reassign the data as desired:
var root = d3.stratify()
.id(function(d) { return d.id; })
.parentId(function(d) { return d.parentId; })
(data)
.eachBefore(function(d) { d.data = {foo: d.data.foo}; });
https://tonicdev.com/571eb333aded861100033e76/57b08e304e962a120052f44f
Introducing stratify.props (or stratify.data) adds complexity for something that can already be done easily, particularly in that there’s now a third object to understand (that becomes node.data), rather than just the input data and the output node that wraps it.
from d3-hierarchy.
Missing i
and dada
was an oversight, thanks for catching that. As for the design, I agree, stratify.props isn't a good name; I decided against calling it 'data' to avoid confusion with the pattern of d3-select. An optional callback similar to d3-dsv parsing methods would probably be preferable.
Introducing stratify.props (or stratify.data) adds complexity for something that can already be done easily, particularly in that there’s now a third object to understand (that becomes node.data), rather than just the input data and the output node that wraps it.
That's a good point. My reasoning for suggesting it was to have a way to do that without the need to iterate over the nodes after stratifying and/or perform operations on the data before stratifying. Operating on the data could be done within stratify.id, but that feels hacky.
Just food for thought I suppose; thanks for taking the time to check out the PR and follow up.
from d3-hierarchy.
Related Issues (20)
- Treemap leaf nodes with rect height = 0 HOT 1
- 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
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.