Skip to content

Chart interaction

@fractalboxdev/merlion-view/interact-chart makes a merlion-chart/1 SVG inside <merlion-view> interactive:

  • a crosshair reads out every series at one x;
  • a tap on a legend key hides or shows that series;
  • a drag across a time, linear or log x axis reports the range in data units;
  • charts that share a link attribute move their crosshairs together.

Interaction is presentation only. The viewer never re-renders. Every class, element and attribute it adds to the SVG is removed when it clears, so svg.outerHTML after clearing equals its value before the first interaction. A host that wants the brushed range drawn re-renders the chart with that domain itself (Brush).

The module reads the plot box, the axis types and the x domain from the chart’s root (chart.md § SVG). It reads values and series from the data-merlion-* attributes of items and points, and positions from their geometry. ADR-0015 records why the root carries the scale.

  • <merlion-view> imports ./interact-chart.js the first time it adopts an SVG with the classes merlion and merlion-chart. It imports ./interact.js for any other Merlion SVG (viewer.md). interactive="off" runs neither module. import "@fractalboxdev/merlion-view/interact-chart" loads the chart module eagerly.
  • The chart module activates only for an SVG whose root carries data-merlion-plot and data-merlion-axes with valid values. A chart SVG without them still gets pan, zoom and fullscreen, but no chart interaction. interact.js never activates on a chart SVG.
  • The model is built the first time it is needed while the SVG is rendered: the first pointer move over the host, key press, tap or linked move. Building it reads each entry’s bounding box once, so it costs O(E) for E entries. Adopting an SVG reads only the root’s attributes. An SVG that is not rendered (inside an element with display: none, such as a closed tab, where every bounding box is empty) builds no model and shows no crosshair; it builds one on the first move after it is rendered.
  • A late extension registration does not restart the module on hosts already on the page, so the crosshair, the hidden series and the brush survive another module loading.
Term Definition
Plot box P data-merlion-plot = {x} {y} {w} {h}, in viewBox units
Index axis y when data-merlion-axes names y as band, else x. The guide is perpendicular to it: vertical for x, horizontal for y
Entry Each item of a bar or dot mark (.merlion-chart-bar > .merlion-chart-item, .merlion-chart-dot > .merlion-chart-item), and each .merlion-chart-point of a line or area mark
Key of an entry The text of its index-axis attribute: data-merlion-x, or data-merlion-y for index axis y
Value of an entry The text of the other position attribute (data-merlion-y, or data-merlion-x for index axis y), followed by – {y2} when the entry carries data-merlion-y2
Series of an entry data-merlion-series of the entry, or of the item that holds the point; absent for a mark without series
Key position The midpoint, along the index axis, of the union of the bounding boxes of the key’s visible entries. A category’s bars sit centred on the category, and points and dots sit on it, so on a band axis this is the category centre and on a continuous axis it is the circle centre
Keys The distinct keys with at least one visible entry, ordered by position
  • Items of interval, rule and span marks are not entries. They stay drawn under the guide, and the readout never names them.
  • Every attribute value is used as text, exactly as written. The readout shows -2.40 for a value written "-2.40".
  • With a mouse or pen pointer inside P, the crosshair moves to the key whose position is nearest the pointer along the index axis. On a tie it takes the key earlier in order. Pointer coordinates map to viewBox units through the SVG’s screen transform, so the snap holds at every zoom level and in fullscreen.
  • Moving the pointer out of P, or out of the host, hides a crosshair the pointer placed. It does not hide one the keyboard placed.
  • A tap inside P moves the crosshair to the nearest key and keeps it there when the pointer leaves, so a touch pointer shows it with a tap. A tap outside P that is not on a legend key hides the crosshair.
  • While it shows, a <line class="merlion-chart-guide"> appended to the SVG runs across P at the key’s position. Every visible entry of the key has the class merlion-lit. A lit point’s hit circle fills with its series colour, so the point the readout names is visible.
  • Readout. The base popover (host.tip, interaction.md § Placement) anchors to the guide, so it sits beside a vertical guide and below or above a horizontal one. It is built with textContent:
    • the heading, in bold, is the key’s text;
    • then one row per visible entry of the key, in document order. A row is a swatch ■ in the entry’s computed color, then {series}: {value}, or {value} when the entry has no series. Rows with the same colour and text show once, so a line drawn with dots on its points reads out once.
  • The readout is aria-hidden like every popover. A keyboard move announces it through the live region (host.say) as the key, then each row’s text after . . Pointer moves and linked moves announce nothing.

<merlion-view> stays the single tab stop. The module handles keydown on the host in the capture phase when no modifier key is held. It calls preventDefault on every key it consumes, so the base viewer skips those keys (viewer.md § Extension hook).

Key, host focused Action
ArrowRight, ArrowDown Move the crosshair to the next key; from none, to the first. Wraps from the last to the first
ArrowLeft, ArrowUp Move it to the previous key; from none, to the last. Wraps
1–8 Toggle the series of the n-th legend key; with fewer than n keys, the key is not consumed
Esc Hide the crosshair and clear the brush; with neither shown, the key passes to the base viewer, which closes fullscreen
Shift + arrow keys, +, -, 0, Tab Unchanged: pan, zoom, reset, leave the chart
  • A tap on a .merlion-chart-key toggles its series, named by the key’s data-merlion-series. So does a digit key (Keyboard).
  • While a series is hidden:
    • every element inside a .merlion-chart-mark or the .merlion-chart-labels group whose own data-merlion-series names it has the class merlion-hidden (display: none). This covers a line’s or area’s points and an item’s label;
    • its legend key has the class merlion-chart-off (opacity 0.4) and stays a tap target.
  • Each toggle announces {series} hidden or {series} shown through the live region.
  • Axes, domains, grid lines, stack positions and stack totals stay as rendered. A stacked bar with a hidden series keeps a gap where its segment was, and its total stays the sum of every series, as the outline prints it.
  • Hidden entries leave the readout. A key whose entries are all hidden leaves the keys, and a crosshair on it hides.
  • While any series is hidden, the host has data-merlion-hidden, so the base viewer shows Show all. Clicking it (merlion-show-all) shows every series.
  • A brush is available when data-merlion-axes names x as linear, log or time and the root carries a valid data-merlion-x-domain. A log domain is valid when both ends are above zero.
  • A drag brushes when all of these hold:
    • it is a primary mouse or pen drag with no modifier key;
    • it starts inside P and not on <text>;
    • the drawing fits the box (the host has no pannable attribute).
  • On a pannable drawing a drag pans, as for diagrams. A touch pointer never brushes: one finger scrolls the page or pans.
  • The pointer-down of a brush prevents its default action, so the drag selects no text, and focuses the host. Once the pointer has moved 4 CSS px from where it went down, a <rect class="merlion-chart-brush"> appended to the SVG spans P’s height between the two x positions, clamped to P.
  • On release, the brushed range [a, b] (a < b, in viewBox units) converts to data units through the x domain [d0, d1]: v(p) = d0 + (p − P.x) / P.w × (d1 − d0), or on a log axis v(p) = 10^(lg d0 + (p − P.x) / P.w × (lg d1 − lg d0)) with lg = log₁₀. A position past P counts as P’s nearer edge.
    • On a linear axis, from and to are numbers with k decimals: the decimals of one viewBox unit, k = clamp(⌈−log₁₀((d1 − d0) / P.w)⌉, 0, 20). A domain of 0–100 over 600 units gives k = 1.
    • On a log axis, from and to are numbers above zero with g significant digits: the digits one viewBox unit resolves. One unit spans u = (lg d1 − lg d0) / P.w decades, which multiplies a value by 10^u, and g = clamp(1 + ⌈−log₁₀(10^u − 1)⌉, 1, 17). A domain of 10 to 10,000,000 over 600 units gives u = 0.01 and g = 3.
    • On a linear or log axis, from rounds down and to rounds up at that precision, so the range holds every value the brush covers: 16.67 to 33.33 reports 16.6 to 33.4. A value within 10⁻¹⁴ of its own magnitude from a rounding step takes that step, so float error never widens a range. An end at or past P’s edge reports that end of data-merlion-x-domain exactly: a brush to the right edge of a domain ending at 316.2 reports to: 316.2, not 316.
    • On a time axis, from and to are instants as YYYY-MM-DDTHH:MM:SSZ strings, rounded to the nearest second.
  • After the conversion, the host dispatches a merlion-brush CustomEvent that bubbles and is composed, with detail: { from, to }. These are the forms an x.domain accepts (chart.md § Axes), so a host may re-render with "domain": [from, to]. The rect stays drawn.
  • If from equals to after rounding, or the pointer is cancelled, no event is dispatched and the previous brush, if any, stays drawn.
  • A tap inside P or Esc clears an existing brush and dispatches merlion-brush with detail: null. A new brush replaces the old one with a single event carrying the new range.
  • Charts whose hosts have the same non-empty link attribute form a group. Values are compared as exact strings. A host without link belongs to no group.
  • When the crosshair of a chart in a group moves by pointer, tap or keyboard, every other chart in the group moves its crosshair to the source key, as follows:
    • band index axis: to the key with the same text, or hides when it has none.
    • Continuous index axis: reads the source key’s text as a number (linear), log₁₀ of a number above zero (log), or a UTC instant (time), and moves to its own key nearest that reading, so on a log axis the nearest in decades. It hides when the text does not read on its axis, or the reading lies outside its x domain on an x index axis.
  • When the source’s crosshair hides, every follower’s crosshair hides. So does removing the source chart while its crosshair shows.
  • A chart propagates only a crosshair placed on it. A legend toggle on a chart propagates only when it hides the key that chart’s own crosshair was on; a chart with no crosshair, or one placed by a linked move, moves nothing.
  • A follower draws its guide, lit entries and readout. It announces nothing and does not propagate the move.
  • The group is read at each move, so changing link takes effect on the next move.

The module changes only class tokens on SVG elements and attributes on the host, and appends the guide and the brush rect while they show. Each is removed when it clears:

Where Token or element Meaning
Host data-merlion-interactive The module is active
Host data-merlion-state The crosshair shows
Host data-merlion-hidden A series is hidden; the base shows Show all
Entries of the crosshair’s key class merlion-lit Read out
Elements of a hidden series class merlion-hidden Not drawn
Legend key of a hidden series class merlion-chart-off Dimmed
SVG <line class="merlion-chart-guide"> The crosshair
SVG <rect class="merlion-chart-brush"> The brushed range

The rules live in the base element’s light-DOM sheet (MerlionView.style), which applies under a CSP without 'unsafe-inline':

[data-merlion-interactive] .merlion-chart-key{cursor:pointer}
.merlion-hidden{display:none}
.merlion-chart-off{opacity:.4}
.merlion-chart-guide,.merlion-chart-brush{pointer-events:none;vector-effect:non-scaling-stroke}
.merlion-chart-guide{stroke:var(--merlion-fg,#1f2328);stroke-opacity:.5}
.merlion-chart-brush{fill:var(--merlion-highlight,var(--merlion-accent,#0969da));fill-opacity:.12;stroke:var(--merlion-highlight,var(--merlion-accent,#0969da));stroke-opacity:.6}
.merlion-chart-point.merlion-lit>circle{fill:currentColor;stroke:var(--merlion-bg,#fff);stroke-width:2}
  • The guide and the brush rect take no pointer events, so the items’ native <title> tooltips and the snap keep working under them.
  • A lit point keeps its series colour from its item’s color. Bars and dots are not restyled; the guide marks their category.
  • The guide and the brush rect keep a 1 px stroke at every zoom level (vector-effect: non-scaling-stroke).
  • The module adds no markup from chart text. It creates elements with createElement and createElementNS and fills them with textContent. Attribute values are compared as strings and parsed as numbers. They are never interpolated into a selector, a style or markup.
  • The module makes no request and reads no URL. It writes only:
    • class tokens on the SVG’s elements, and the guide and brush rect, which it removes;
    • the host’s attributes;
    • the base element’s popover and live region;
    • the shared light-DOM sheet;
    • merlion-brush events, whose detail holds only numbers, strings or null.
Item Budget
@fractalboxdev/merlion-view/interact-chart, minified + gzip ≤ 3.5 KB, enforced by scripts/size.mjs
Model Built once per adopted SVG on first use, O(E) bounding-box reads for E entries
Per pointer move One screen-transform read, a binary search over the keys, class changes on the previous and the new key’s entries, one popover placement
Per linked move The same, in each follower

The render is unchanged as a function: the root’s chart attributes depend on the spec and the target width only, and native and WASM output stay byte-identical. Interaction state never reaches the SVG’s bytes, the outline or the chart id.

Unit (node --test, no DOM), on interact-chart-model.js:

  • the nearest-key search: empty, one key, ties, and pointer positions before the first key and after the last;
  • the conversion from viewBox units to data units: the k rule at domain spans from 0.001 to 10⁹, the g rule and the conversion in decades on a log axis, rounding a time range to whole seconds, and the ISO form;
  • reading values on a log axis as log₁₀, and rejecting zero, negative values and a domain end at or below zero;
  • reading each time value form (YYYY, YYYY-MM, YYYY-MM-DD, YYYY-MM-DDTHH:MM[:SS]Z) as a UTC instant;
  • linked matching: exact text on band, the nearest key on a continuous axis, in decades on a log axis, and a value outside the domain or not a number;
  • parsing the root attributes, where a missing or malformed value leaves the module inactive.

Acceptance (Playwright, headless Chromium, chart fixtures rendered by merlion render --chart and inlined in <merlion-view>):

  • On a line chart with a time x axis, hovering at a point’s x shows one guide and a readout. The readout’s heading and rows equal the points’ data-merlion-x and {series}: {data-merlion-y}.
  • On grouped bars, hovering anywhere in a category’s step shows that category. On bars with a band y axis, the guide is horizontal.
  • ArrowRight walks the keys in order and announces each readout. Esc hides the crosshair, and a second Esc in fullscreen closes the dialog.
  • A tap on a legend key hides exactly the elements carrying its series and dims the key. Show all restores svg.outerHTML byte for byte. Digit 2 toggles the second series.
  • A drag across a time axis dispatches one merlion-brush whose range matches the dragged positions to one second. A tap then dispatches detail: null. Zoomed in, the same drag pans and dispatches nothing.
  • A drag across the log x axis of log-scatter dispatches numbers whose log₁₀ lies within two viewBox units, in decades, of the dragged positions.
  • Two charts with the same link mirror the crosshair. A third with another link value does not move.
  • After every interaction is cleared, svg.outerHTML equals its value before the first one.

TODO(owner): run axe-core over the acceptance pages once it is recorded in licensing.md, as interaction.md also requires.

  • A brush on y or on a band axis, and zooming to the brushed range inside the viewer.
  • A crosshair on interval, rule and span marks.
  • Streaming data, canvas rendering and editing.
  • Focusing legend keys one by one with Tab. The digit keys toggle series instead, and the host stays the single tab stop.