-
Notifications
You must be signed in to change notification settings - Fork 313
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
Hierarchy layouts should allow non-root nodes as input? #91
Comments
Would you be able to share a running code example that demonstrates this behavior? Thank you. |
sure! i've pieced together a fiddle https://jsfiddle.net/devacholi/k3m83ofn/3/ const data = [
{"label": 'World',},
{"label": 'Europe', "parent": 'World'},
{"label": 'Portugal', "parent": 'Europe', "value": 30},
{"label": 'Spain', "parent": 'Europe', "value": 120},
{"label": 'South America', "parent": 'World',"value": 320},
{"label": 'Africa',"parent": 'World'},
{"label": 'Uganda', "parent": 'Africa',},
{"parent": 'Uganda',"label": 'Jinja',"value": 20,},
{ "parent": 'Uganda', "label": 'Kampala', "value": 40,},
{ "label": 'Kenya', "parent": 'Africa', "value": 70,},
]
const stratify = d3.stratify()
.id(d => d.label)
.parentId(d => d.parent);
const layout = d3.partition()
const root = stratify(data)
.sum(d => d.value)
.sort((a,b) => a.value - b.value);
// node.depth = 0
const okRectangles = layout(root)
.descendants()
console.log('expecting 0, got ' + okRectangles.filter(r => r.y1 > 1).length)
// expecting 0, got 0
// root.children[0].depth = 1
const badRectangles = layout(root.children[0])
.descendants()
console.log('expecting 0, got ' + badRectangles.filter(r => r.y1 > 1).length)
// expecting 0, got 2 |
Partition function that fixes d3/d3-hierarchy#91
Per #80 (comment), this is a documented limitation; the hierarchy layouts take a root node as input, not any node. To quote the README:
And:
You can use node.copy to create a new root from an existing node’s subtree. My longer-term goal for this module is that the hierarchy layouts no longer mutate the input data (#62). That would make it easier to avoid the call to node.copy before computing the layout. But I don’t think it’s worth fixing this issue in just one of the layouts, when the other layouts have the same problem. It would be reasonable to add some text to the README to clarify this limitation and the recommended workaround in the interim. |
Retitled to generalize this issue. |
Thanks for the explanation Didn't know about |
When a descendant node (i.e depth != 0) is 'partitioned', it's children will be be positioned inaccurately.
Here the 'world' node is 'partitioned'
Here the 'africa' node is partitioned, because the 'africa' node depth > 0, the layout gets broken
The text was updated successfully, but these errors were encountered: