Any two windows of the same length should be comparable, in how they draw and in what their change means.

The problem

A shop tracks revenue daily for a year, and someone wants last month. The naive version filters to points inside the window. The line starts at the first sample after the left edge and stops at the last one before the right edge, so it floats with dead space on both sides. Nudge the window and the line changes width even though the data didn’t.

The change readout breaks the same way. “Last 90 days vs the 90 before” sounds fair, until the earlier window reaches back before the shop opened. Now it holds 50 days of data against 90, and a raw total makes the current window look like a triumph.

What I concluded

A window is a lens on one series, and every lens of the same size has to behave the same. I load the whole history once and window it on the client, so scrubbing never waits on the network.

The line reaches both edges

Where a segment crosses an edge, I add a point at the edge by interpolating between the two real samples on either side. The line runs edge to edge in every window. If either neighbor is a gap, the edge point is a gap too, and past the newest sample the line simply stops.

The step is the data’s own cadence

The window snaps to the tightest gap between samples, so hourly data scrubs by the hour and daily data by the day.

Compare like with like

The change compares this window with the equal stretch right before it. When that earlier stretch is missing days, totals would flatter the current one, so the readout says the previous window is partial and compares daily averages instead.

const partial = prev.days < windowDays;
const change = partial
  ? cur.total / cur.days / (prev.total / prev.days) - 1
  : cur.total / prev.total - 1;
  previous 90d             │ last 90d
  ░░ no data yet ░░▂▃▃▄▃▅▄ │ ▅▆▅▇▆▇▆▇▆▇▆▇▆▇
                           │
  50 of 90 days            → compare per day, and say so

Rules

  • Same length, same width. Edge points make every window draw across the full plot.
  • Interpolate only between real samples. A gap at the edge stays a gap.
  • Snap to the data’s cadence. Not to a fixed unit the data doesn’t have.
  • A partial baseline changes the math, out loud. Switch to per day averages and show a note saying why.
  • Edge points aren’t measurements. A tooltip shouldn’t present them as captured values.

What you get

Windows you can flip between without the picture or the number shifting for reasons that have nothing to do with the data. The demo shows the window-over-window comparison: switch to 90 days and it flags the partial previous window and compares averages. Edge clipping and cadence snapping aren’t in it.

Related: Sparklines, Date brush