observablehq / observablehq/plot
Compute the tree height automatically
Nobody has claimed this yet.
- Dominant language
- HTML
- Stars
- 5.4k
- Forks
- 244
- PR merge metrics
- No merged PRs in 30d
Description
It’s often desirable to compute the height of a tree diagram automatically, like so:
function computeTreeWidth(paths) {
const root = d3.tree().nodeSize([1, 1])(d3.stratify().path((d) => d.path)(paths));
const [x1, x2] = d3.extent(root, (d) => d.x);
return x2 - x1;
}
Plot.plot({
axis: null,
height: computeTreeWidth(paths) * 12,
marginTop: 4,
marginBottom: 4,
marginRight: 120,
marks: [
Plot.tree(paths, {path: "path", textStroke: "var(--vp-c-bg)", channels: {href: {value: "link", filter: null}}, treeSort: null})
]
})
It would be nice if Plot.tree did this automatically, somehow. The above approach is brittle because computeTreeWidth needs to match the behavior of Plot.tree exactly. (For example, it could be wrong if the tree is sorted prior to layout.) And hard-coding the height is also undesirable because the height should be changed if the data changes.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the Plot.tree implementation and its tree layout and sizing behavior, then compare it with the d3.tree and d3.stratify usage shown in the issue. The work is done when Plot.tree derives an appropriate height from the current data and layout, including when sorting or data changes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- d3js, javascript
- Domain
- data-visualization
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100