Charts stay testable and smooth when the math is plain functions and the DOM work is kept to one of everything.

The problem

The classic way to add a chart is to let a charting library own a chunk of the DOM. That works until React re-renders the parent and the two argue over who owns which <path>. Worse, the trickiest logic (where a gap breaks the line, what the domain is for a flat series) ends up inside an effect you can only test by mounting a browser.

The rest of the plumbing has the same shape. Every chart measures itself, so resizing a dashboard tile re-renders on every pointer move and stutters. Every data point gets its own invisible hit circle, so the tooltip flickers off between points on a sparse series.

What I concluded

Split the chart into math and markup, then give each shared job exactly one owner.

Math in functions, SVG in components

Every chart has a geometry module: data and a size in, scales and path strings out. A math library helps compute them but never touches the DOM. The component just renders what came back.

// geometry.ts: no React, no DOM
type Vals = (number | null)[];
export function lineGeometry(vals: Vals, w: number, h: number) {
  const x = scaleLinear().domain([0, vals.length - 1]).range([0, w]);
  const y = scaleLinear().domain(extent(vals)).range([h, 0]);
  const path = line<number | null>().defined((v) => v !== null)
    .x((_, i) => x(i)).y((v) => y(v!))(vals);
  return { x, y, path };
}

The payoff is the test file. “Does a lone point between gaps still show,” “does a flat series sit in the middle” become one-line assertions on plain arrays that run in milliseconds.

One measurement, once per frame

One container measures itself with a ResizeObserver and shares the size through context. It records every notification but commits at most one size per animation frame. The cost is one frame of lag, which I can’t see. Charts that are a fixed aspect ratio, like a donut, skip measuring and let a viewBox scale them.

One hover surface

A single transparent rect over the plot owns hover, click and drag. On move, it finds the nearest point by x and shows that tooltip, which only closes when the pointer leaves the plot. A short gesture is a click. A longer one draws a band and selects a range snapped to real points, feeding the same window the date brush controls.

Rules

  • The math library never touches the DOM. It computes, React renders.
  • Test geometry, not pixels. A handful of array assertions beats one SVG snapshot.
  • Measure in one place, commit once a frame. Debouncing freezes the chart mid-drag, which feels worse.
  • Skip measuring when CSS can scale it. Only measure when geometry depends on pixels.
  • One element owns the pointer. Nearest by x, with a small threshold between click and drag.
  • Give every chart a text twin. An accessible name and a table of the values, outside anything size-dependent.

What you get

Charts whose hard cases are unit tested, that track a resize smoothly, and whose tooltip never drops between points. The trade is writing your own ticks and transitions, and for thousands of marks per frame, canvas is the better tool.

Related: Tooltip, Date brush, Sparklines