observablehq / observablehq/plot

tree and gradient

Open
#1,978 0 comments 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

Using Plot.tree with a gradient stroke fails on links that are purely horizontal:

Capture d’écran 2024-01-30 à 13 35 38

The bug (reported by @pixflowave) is due to the infamous browser issue that an horizontal path has height 0, and thus the gradient is optimized out. (This issue is also often hitting d3-sankey users, https://github.com/d3/d3-sankey/issues/28.)

A way to fix this is to always add some points in any path, to guarantee that the bounding-box’s area is not 0.

For example, one could use: curve: bbCurve(d3.curveBumpX)

where:

function bbCurve (curve) {
  return (context) => {
    const c = curve(context);
    const { point, lineEnd } = c;
    let x0, y0;
    return Object.assign(c, {
      point(x, y) {
        point.call(this, (x0 = x), (y0 = y));
      },
      lineEnd() {
        this._context.moveTo(x0 - 0.1, y0);
        this._context.moveTo(x0, y0 + 0.1);
        lineEnd.call(this);
      }
    });
  };
}

Capture d’écran 2024-01-30 à 13 35 52

Not sure if this should be done in Plot, as an option of d3 curves, or somewhere else?

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 at the Plot.tree implementation and its gradient-stroke path handling, then reproduce the failure with a purely horizontal link. Compare the suggested curve wrapper with the existing d3 curve integration; done means horizontal tree links render their gradients reliably without affecting other link shapes.

Written by the indexing model from the issue text.

Assessment

Tech stack
d3, javascript
Domain
data-visualization
Issue type
Bug
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.