Span timeline

Slant / Charts

Nested spans as bars on a time axis, for traces and Gantt views: expand, collapse, and drag a range to zoom.

Demo

Run detail

Usage

@tuttn/slant
import { SpanTimeline } from '@tuttn/slant'

// A trace: times in milliseconds from its start. Children fold under their span.
const time = (ms: number) => ms >= 1000 ? `${(ms / 1000).toFixed(1)} s` : `${ms} ms`
<SpanTimeline label="Run trace" format={time} onSpanSelect={id => open(id)} spans={[
  { id: 'run', label: 'Run', start: 0, end: 4200, children: [
    { id: 'plan', label: 'Plan', start: 0, end: 600, color: 2 },
    { id: 'test', label: 'Bash · npm test', start: 650, end: 3900, tone: 'danger', detail: 'exit 1' },
  ] },
]} />

// A read-only Gantt view: timestamps on the axis, durations in days,
// a tick each Monday and a line at now.
<SpanTimeline label="Release plan" spans={tasks} format={day} formatDuration={days}
  ticks={mondays} markers={[{ value: now, label: 'Today' }]} table />

// Uncontrolled, every span with children starts open and the whole range
// shows. Pass expanded, range and active (with their handlers) to drive them.
<SpanTimeline label="Run trace" spans={spans} format={time}
  expanded={expanded} onExpandedChange={setExpanded}
  range={range} onRangeChange={setRange}
  active={active} onActiveChange={setActive} />

Props

PropTypeDefaultDescription
spansreadonly TimelineSpan[]—The top-level spans; each may hold children.
labelstring—The chart's accessible name.
unitstring—Read after each time in announcements and the table, e.g. "ms".
format(value: number) => stringvalue => String(value)Formats a time on the axis, in the tooltip, the table and announcements. Durations use it too, unless `formatDuration` is given.
formatDuration(value: number) => stringformatFormats a duration (a span's end minus its start) in the tooltip, the table, announcements and the label after each bar. Give it when `format` reads a timestamp, such as a date, which a duration is not.
ticksreadonly number[]—Times to draw the axis ticks and gridlines at, in place of round values: each Monday of a Gantt view. With them the axis spans the spans and markers exactly, instead of rounding out to the next round value. Those outside the window shown are left out.
expandedreadonly string[]—The ids of the spans whose children show, for a controlled tree.
defaultExpandedreadonly string[]every span with childrenThe ids of the spans whose children show at first, when uncontrolled.
onExpandedChange(expanded: string[]) => void—Called with the new list of expanded ids when a span opens or folds.
rangereadonly [number, number] | null—The time window shown, `[start, end]`, for a controlled zoom; `null` shows everything.
defaultRangereadonly [number, number] | nullnullThe window shown at first, when uncontrolled.
onRangeChange(range: [number, number] | null) => void—Called with the new window when a drag, a key or Show all zooms; `null` when it shows everything again.
activestring—The id of the span the keyboard cursor sits on, for a controlled chart.
defaultActivestring—The span the keyboard cursor starts on. By default the first.
onActiveChange(id: string) => void—Called with a span's id when the keyboard, or a click on its bar or label, moves the cursor to it.
onSpanSelect(id: string) => void—Called with a span's id when it is clicked, or Enter is pressed on it.
markersreadonly { value: number; label: string }[]—Vertical lines across the plot at these times, each with a short label: "now", "deadline".
rowHeightnumber24The height of one row in pixels.
tablebooleanfalseShows the data table under the chart. It is always there for screen readers.
TimelineSpan{ id: string; label: string; start: number; end: number; detail?: string; color?: 1 | 2 | 3 | 4 | 5; tone?: Tone; children?: TimelineSpan[] }—One entry of spans: its name, its start and end on the time axis, an optional muted detail, its colour slot or tone, and the spans inside it.
…restHTMLAttributes<HTMLDivElement>—className, style, the ref and other attributes go on the root <div class="sl-chart sl-span-timeline">, which carries data-kind="span" and data-zoomed while a window is shown.

Keyboard

Tabfocuses the plot, one tab stop; while zoomed, Show all is its own stop before it
Down / Upmoves to the next or previous row shown
Rightopens a folded span; on an open one, moves to its first child
Leftfolds an open span; otherwise moves to its parent
Home / Endmoves to the first or last row
Enterselects the span (onSpanSelect)
+ or =zooms in around the cursor’s span, halving the window
−zooms out, doubling the window, up to everything
0shows everything
Shift + Left / Rightpans a zoomed window by a tenth of its width
Escapehides the tooltip until the next move

Accessibility

  • The plot is a group with the role description “chart”, named by label and described by a summary: how many spans, from when to when, and the longest (messages.spanSummary, messages.spanLongest).
  • The drawing is hidden from assistive technology. Each move is announced through the root’s polite live region instead: the label, its range and duration, its level and whether it is open (messages.spanRange, messages.spanTree), then its detail: “Bash · npm test, 720 ms to 3.9 s, 3.2 s, level 2, 2 inside, expanded, exit 1”.
  • Opening or folding a span announces its new state, and every zoom, by key, drag or Show all, announces the window (messages.spanZoomed).
  • The data table lists every span, open or folded, with its start, end and duration and its name indented by level. It is always there for screen readers; table shows it under the chart.
  • Colour is never the only cue: give a failed span a detail (“exit 1”) as well as tone="danger", so the failure is read as well as seen.
  • The chevrons, dragging and double-click are pointer-only shortcuts; each has a key (Right and Left, + and −, 0). Show all is a real button outside the plot’s tab stop, and focus moves to the plot after it.

Times and formats

A span’s start and end are numbers on one axis: milliseconds from the start of a trace, or timestamps. format writes a time on the axis, in the tooltip, the table and announcements, and unit is read after it. Durations use format too, which suits a trace; for timestamps pass formatDuration as well, since a duration is not a date. The axis ticks fall on round values, or on the times in ticks, such as each Monday of a plan.

Zoom

Drag across the bars to zoom into that range (a press that moves less than 3px is a click), double-click to show everything, or use + and − from the keyboard. The window stays inside the spans and markers, and while it is narrowed a Show all button appears above the plot. Pass range and onRangeChange to hold the window yourself; null shows everything.

Rows

The label column sizes itself to the longest label at any depth, up to 40% of the width, so opening a span never moves the bars; longer labels end in an ellipsis, and the tooltip shows them whole. A muted duration follows each bar where it fits. Uncontrolled, every span with children starts open, unless defaultExpanded says.

Right to left

The drawing is physical, like every chart: under dir="rtl" the label column stays on the left, time runs from left to right, and Right still opens a span. The zoom row and the table around the plot follow the page’s direction.

Full source