observablehq / observablehq/plot

Compute the tree height automatically

Open
#1,703 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.