Charts
A chart is data, so Merlion reads it as data: a merlion-chart/1 JSON spec of rows, and marks that name the fields to draw. Each value is written once. The bar, its label, its tooltip and the text outline all show the same text, and the SVG carries it in data-merlion-* attributes so anyone can check a mark against its row. The full contract is in specs/chart.md.
A dumbbell, step by step
Section titled “A dumbbell, step by step”Six teams’ scores in January and June. The rows hold both values; three marks draw them:
{ "chart": 1, "title": "Scores fell most where the baseline was highest", "x": { "type": "linear", "label": "Score" }, "y": { "type": "band" }, "data": [ { "team": "Atlas", "jan": 82, "jun": 61 }, { "team": "Birch", "jan": 77, "jun": 70 }, { "team": "Delta", "jan": 59, "jun": 41 } ], "series": { "domain": ["January", "June"] }, "marks": [ { "type": "interval", "y": "team", "x": "jan", "x2": "jun", "tone": "neutral" }, { "type": "dot", "y": "team", "x": "jan", "series": { "value": "January" } }, { "type": "dot", "y": "team", "x": "jun", "series": { "value": "June" }, "label": "jun" } ]}yis abandaxis, so each team gets a row;xislinear, so its domain covers every value drawn on it, widened to whole tick steps.- The
intervaldraws a grey segment fromjantojun.tone: "neutral"keeps context marks out of the series colours. - Each
dotmark names one series with{ "value": … }. Two series draw a legend; thelabelfield writes June’s value beside its dot, on the outer side of the segment. titlebecomes the SVG’s<title>. The chart draws no visible title: the page around it does, where the text can wrap.
Render it:
merlion render --chart teams.json -o teams.svgimport { initSync, renderChart } from "@fractalboxdev/merlion-wasm";const { svg, outline, diagnostics } = renderChart(spec, { width: 640 });renderChart accepts the spec as an object or as JSON text.
| Mark | Draws |
|---|---|
bar |
A bar from zero on one band and one linear axis; several series sit side by side, or stack with stack |
line, area |
A series through its rows in x order; an area fills to zero or to y2, or stacks with stack |
dot |
A point on any axes |
interval |
A segment between two values: ranges, dumbbells, scenario bands |
candlestick |
Open, high, low and close per row: a wick from low to high and a body from open to close |
rule |
A reference line across the plot, with an optional note |
span |
A shaded window across the plot, with an optional note |
Stacks
Section titled “Stacks”"stack": true on a bar or an area stacks its series: you write each part, and Merlion sums them. Series stack in the order of series.domain. Values at or above zero stack up from zero, negative values down from it. Each segment’s tooltip, data-merlion-* attributes and outline line keep the value you wrote, never the running sum.
A stacked area with a reference line
Section titled “A stacked area with a reference line”Monthly costs by category, with a dashed budget line on the same axis and a shaded window:
{ "chart": 1, "title": "Monthly costs stayed under budget", "x": { "type": "time", "format": "%b %Y" }, "y": { "type": "linear", "label": "Cost, $k per month" }, "data": [ { "month": "2026-01", "category": "Staff", "cost": "118.0" }, { "month": "2026-01", "category": "Rent", "cost": "38.0" }, { "month": "2026-02", "category": "Staff", "cost": "118.9" }, { "month": "2026-02", "category": "Rent", "cost": "46.5" }, { "month": "2026-01", "budget": "180.0", "name": "Budget" }, { "month": "2026-02", "budget": "180.0", "name": "Budget" } ], "series": { "domain": ["Staff", "Rent"] }, "marks": [ { "type": "span", "x": { "value": "2026-01-15" }, "x2": { "value": "2026-02" }, "text": "Office move" }, { "type": "area", "x": "month", "y": "cost", "series": "category", "stack": true }, { "type": "line", "x": "month", "y": "budget", "tone": "neutral", "dash": true, "label": "name" } ]}- Every series needs a value at every
xof the stack. A missing month fails withE022naming the series and the month; write0where a series has nothing. - The line is not stacked: it reads its own rows, and its
labelnames it at its last point.
Horizontal stacked bars with totals
Section titled “Horizontal stacked bars with totals”Hours per team, split by activity, with each team’s total beyond its bar:
{ "chart": 1, "title": "Build work takes the largest share of every team's hours", "x": { "type": "linear", "label": "Hours per week" }, "y": { "type": "band" }, "data": [ { "team": "Platform", "activity": "Design", "hours": "12.5" }, { "team": "Platform", "activity": "Build", "hours": "48.0" }, { "team": "Platform", "activity": "Test", "hours": "16.25" }, { "team": "Payments", "activity": "Design", "hours": "9.0" }, { "team": "Payments", "activity": "Build", "hours": "36.5" }, { "team": "Payments", "activity": "Test", "hours": "21.0" } ], "series": { "domain": ["Design", "Build", "Test"] }, "marks": [ { "type": "bar", "y": "team", "x": "hours", "series": "activity", "stack": true, "total": true } ]}totalprints the exact sum of the written values,76.75and66.50here: every total takes as many decimals as the most precise value in the mark, and the sum never passes through floating point.- Segments sit 2 px apart, and only the outer end of each stack is rounded.
- A stacked bar takes no
label: hover a segment for its value, or read the outline.
Candlesticks
Section titled “Candlesticks”Daily open, high, low and close, one candle per row:
{ "chart": 1, "title": "The sample index closed lower on Tuesday", "x": { "type": "time", "format": "%b %d" }, "y": { "type": "linear", "label": "Index level" }, "data": [ { "day": "2026-03-02", "open": "100.00", "high": "100.38", "low": "98.37", "close": "99.35" }, { "day": "2026-03-03", "open": "99.35", "high": "100.19", "low": "96.86", "close": "97.50" }, { "day": "2026-03-04", "open": "97.50", "high": "98.31", "low": "97.10", "close": "97.50" }, { "day": "2026-03-05", "open": "97.50", "high": "99.05", "low": "97.20", "close": "98.80" } ], "marks": [ { "type": "candlestick", "x": "day", "open": "open", "high": "high", "low": "low", "close": "close" } ]}- A rising candle, close at or above open, is hollow in the
--merlion-okcolour; a falling one is solid in--merlion-danger. The fill tells them apart without colour. - The body is 60% of the spacing between candles, at most 12 px: the category step on a
bandaxis, the closest two days here. The time axis extends half a day beyond the first and last candles, so their bodies stay inside the plot. A doji, open equal to close, still draws a body line. - Hover a candle, or read the outline, for
2026-03-02 | open 100.00 | high 100.38 | low 98.37 | close 99.35: all four values as written. - A high below the open or close, or a low above them, fails with
E022naming the cell.
A log axis
Section titled “A log axis”When values span orders of magnitude, "type": "log" gives equal ratios equal lengths. Two engines’ throughput, each about ten times higher every two years:
{ "chart": 1, "title": "Both engines gained about a decade of throughput every two years", "x": { "type": "time", "format": "%Y" }, "y": { "type": "log", "label": "Records per second" }, "data": [ { "year": "2010", "engine": "Batch", "rate": "120" }, { "year": "2018", "engine": "Batch", "rate": "520000" }, { "year": "2026", "engine": "Batch", "rate": "2600000000" }, { "year": "2010", "engine": "Streaming", "rate": "3.5" }, { "year": "2018", "engine": "Streaming", "rate": "27000" }, { "year": "2026", "engine": "Streaming", "rate": "160000000" } ], "marks": [ { "type": "line", "x": "year", "y": "rate", "series": "engine", "label": "engine" }, { "type": "rule", "y": { "value": "1000000" }, "text": "Target", "dash": true } ]}- Ticks sit at powers of ten, with 2 and 5 between them when there is room. Labels go through
format, so0.002prints as written and1000000as1,000,000. - Without
domain, the axis spans whole decades around the values; a short chart labels every second or fifth decade. - A value written as a power of ten, such as
"1000000"or the JSON number1e6, sits exactly on its grid line. - Every value, and each end of
domain, must be above zero. Abar, a stackedareaand anareawithouty2draw from zero, which a log axis does not have; anareawithy2draws the band between two values.
Values that fail loudly
Section titled “Values that fail loudly”A chart that draws a wrong number is worse than one that does not draw. These fail with an error naming the JSON path:
"6,538.76"on a linear axis (E022): write"6538.76", and setformatfor thousands separators on the ticks.- A field no row has, such as
"y": "chnage"(E021). - A member the spec does not define, such as
"colour"(E021). - A value outside an authored
domain(E022): Merlion never clips a mark. - A ninth series (
E023): fold the smallest into “Other” or split the chart. - A stack that reaches past an authored
domain(E022): the message names the stack and the sum it reaches. stackwithoutseries, or on an area withy2(E021).- A candle whose high is below its open or close, or whose low is above them (
E022). - Zero or a negative value on a log axis (
E022), or a bar on one (E021).
merlion check --chart and checkChart report every problem at once, each with its line and column.
Interaction
Section titled “Interaction”Every item carries a <title> tooltip, but a tooltip shows one point at a time. Comparing two series in one month, or narrowing a year to a quarter, would otherwise mean replacing the SVG with a canvas chart. Inside <merlion-view>, a chart can be read and narrowed without that. The viewer reads the values the SVG already carries and never re-renders it:
<script type="module">import "@fractalboxdev/merlion-view";</script>
<merlion-view link="costs"> <svg class="merlion merlion-chart" …>…</svg> <!-- renderChart(costs) --></merlion-view><merlion-view link="costs"> <svg class="merlion merlion-chart" …>…</svg> <!-- renderChart(headcount) --></merlion-view>- Crosshair. Point at the plot and a guide snaps to the nearest month. The readout lists every series at that month, with each value exactly as the spec wrote it:
118.0, not118. On a chart with a bandyaxis, such as the dumbbell above, the guide is horizontal and snaps to a team. With the chart focused, arrow keys move it point by point, and a screen reader announces each readout. - Linked charts. The two charts share
link="costs", so pointing at March on the costs chart moves the headcount chart’s crosshair to March too. - Legend. Click a legend entry, or press
1to8, to hide that series with its labels. Axes and stacks keep their place, so a hidden stack segment leaves a gap. Show all brings every series back. - Brush. Drag across a
time,linearorlogx axis and the host receives amerlion-brushevent with the range in data units. The viewer does not redraw the chart. Your page re-renders it with that range as the domain:
host.addEventListener("merlion-brush", (e) => { // `detail` is null when a tap or Esc clears the brush: draw every month again. const range = e.detail && [e.detail.from, e.detail.to]; // "2026-03-01T00:00:00Z", "2026-06-15T00:00:00Z" const [lo, hi] = range ? range.map(Date.parse) : [-Infinity, Infinity]; const data = costs.data.filter((row) => Date.parse(row.month) >= lo && Date.parse(row.month) <= hi); const x = range ? { ...costs.x, domain: range } : costs.x; const { svg } = renderChart({ ...costs, data, x }, { width: 640 }); if (!svg) return; // nothing left in the range, or a rule or span outside it: keep the chart host.querySelector("svg").replaceWith(new DOMParser().parseFromString(svg, "image/svg+xml").documentElement);});from and to are numbers on a linear or log axis and UTC instants on a time axis, the forms x.domain accepts. On a log axis the range follows the axis: a drag across its middle third spans the middle third of its decades. An authored domain must hold every value the chart draws, so the handler keeps only the rows inside the range; a rule or span at a constant position outside it still fails, and the handler then keeps the chart as drawn. It replaces only the <svg>, so the host keeps its controls and state. Zoomed in, a drag pans instead of brushing. With JavaScript off, the chart is the static SVG with its tooltips. The exact rules are in chart-interaction.md.