render_chart draws a data chart from a merlion-chart/1 JSON spec: rows of values and the marks that draw them. Output follows the shared SVG contract (svg-output.md): one self-contained SVG, labels as <text>, colours as CSS custom properties, role="img" with <title> and <desc>, no script, and byte-identical bytes from the native core and WASM. A chart is not Mermaid source; ADR-0012 records why charts take JSON. Mermaid’s own xychart header stays on the compatibility roadmap (roadmap.md).
- The input is one UTF-8 JSON document (RFC 8259) of at most
limits.input_bytes (1 MiB); a larger one fails with E004.
- Nesting deeper than
limits.nesting (64) fails with E020.
- An object with more than 64 members, or an array with more than 10,000 items, fails with
E020 before the excess value is read. Schema-specific limits also apply.
- Invalid JSON fails with
E020 at the byte where reading stopped. A duplicate member name in one object fails with E020.
- A member the spec does not define fails with
E021 naming its JSON Pointer path (/marks/0/colour), so a misspelt member never draws a different chart.
- Every spec error carries its line, column and byte span in the input.
| Member |
Type |
Required |
Meaning |
chart |
the number 1 |
yes |
Format version |
title |
string, 1–200 characters |
yes |
The chart’s accessible name, written into <title>. The chart draws no visible title: the host page draws the figure’s frame (number, title, source, notes) as text it can wrap and select |
description |
string, at most 2,000 characters |
no |
Replaces the generated <desc> |
x, y |
axis object |
yes |
The two axes (Axes) |
data |
array of 1–10,000 row objects |
yes |
Each row maps at most 64 field names to a string, a number or null |
marks |
array of 1–16 mark objects |
yes |
Drawn in array order, except that span marks draw first (Marks) |
series |
{ "domain": [...] } |
no |
Series names in colour order, 1–8 of them |
legend |
boolean |
no |
Default: drawn when two or more series are drawn |
height |
number, 120–2,000 |
no |
Total SVG height in px for a chart whose y axis is not band; a band y axis sets the height from its categories |
| Member |
Applies to |
Meaning |
type |
all |
"band" (categories), "linear" (numbers), "log" (numbers above zero on a logarithmic scale, Log axes) or "time" (UTC instants) |
label |
all |
Axis title, at most 80 characters. The y title sits above the plot at the left edge; the x title sits below the tick labels at the right edge |
domain |
all |
Band: the categories in drawing order, 1–500, each once. Linear, log or time: [min, max] with min below max |
zero |
linear |
true includes zero in the computed domain |
format |
linear, log |
{ "decimals": 0–6, "prefix": ≤ 8 chars, "suffix": ≤ 8 chars, "group": boolean } for tick labels; group (default true) separates thousands with , |
format |
time |
A D3 time format string the Gantt axis accepts (gantt.md), such as "%b %Y" |
x and y must not both be band, and must not both be time (E021).
- Without
domain, a linear axis spans the values drawn on it, widened to whole tick steps; a log axis spans whole decades (Log axes); a time axis spans the values drawn on it exactly. A single value widens to ±1, or ±10% of a nonzero value; a single instant widens to ±1 day.
- A time axis that a candlestick draws on extends half the candle spacing, rounded up to a whole second, beyond its domain, computed or authored, at each end (Candlesticks). With an authored domain, the drawn axis then runs past the authored ends; values are still checked against the authored domain, so the padding never rejects a value. A host that renders again with the drawn domain (
data-merlion-x-domain) as an authored one sees it padded again.
- With
domain, every value drawn on the axis must lie inside it, or the chart fails with E022. A chart never clips a mark.
- An authored value domain for a bar, or the
y domain for an area without y2, must include zero (E022).
- A band axis without
domain lists categories in order of first appearance: marks in array order, rows in array order.
| Axis |
A value is |
Rejected with E022 |
| Linear |
A JSON number, or a string matching -?[0-9]+(\.[0-9]+)? |
"6,538.76", "1e3", "+1", " 1", "n/a", "5%" |
| Log |
A linear value greater than zero |
Every value a linear axis rejects, and 0, "0.00", -2.5 |
| Time |
A string: YYYY, YYYY-MM, YYYY-MM-DD or YYYY-MM-DDTHH:MM[:SS]Z, read as UTC |
A JSON number, an impossible date, a time without Z |
| Band |
A string, or a number’s text as written |
— |
- Every drawn item keeps the exact text its values were written as. The SVG carries it in
data-merlion-* attributes and the outline prints it, so "-2.40" stays -2.40 and a reader can check each mark against its row.
- Linear values must be representable as finite IEEE 754 binary64 numbers, including subnormal values. A nonzero value that rounds to zero, or a value that rounds to infinity, fails with
E022 before layout.
- A mark that names a field no row has fails with
E021.
- A row whose field is missing or
null for a mark is not drawn by that mark; the render adds one I041 per mark with the count.
type |
Members |
Axes |
Draws |
bar |
x, y; series, label, tone, stack, total |
One band, one linear |
A bar from zero to the value. The value axis always includes zero; zero: false on it fails with E021. Bars of several series in one category sit side by side in series order, or stack with stack (Stacking) |
line |
x, y; series, label, tone, dash |
y linear or log |
One 2 px polyline per series through its rows in x order |
area |
x, y; y2, series, tone, stack |
y linear, or log with y2 |
A filled region between y and y2, or between y and zero without y2, or one layer of a stack with stack |
dot |
x, y; series, label, tone |
Any |
A circle of radius 4.5 px with a 1.5 px ring in the background colour |
interval |
x, y, and exactly one of x2, y2; series, label, tone, thickness |
The x2 or y2 axis linear, log or time |
A segment from the value to the second value, with round caps |
rule |
Exactly one of x, y; text, tone, dash, thickness |
That axis linear, log or time |
A line across the whole plot at the value |
span |
x and x2, or y and y2; text |
That axis linear, log or time |
A shaded band across the whole plot between the values |
candlestick |
x, open, high, low, close; label |
x band or time, y linear |
A wick from low to high and a body from open to close (Candlesticks) |
- A position member (
x, y, x2, y2, and a candlestick’s open, high, low, close) is a field name, or { "value": … } for a constant. A mark whose position members are all constants draws once; any field draws once per row.
series is a field whose value names the item’s series, or { "value": "<name>" } naming one series for the whole mark. A mark with series must keep the default tone.
label is a field; its value is drawn beside the item exactly as written. A bar labels outward from zero; an interval labels past its far end, to the right or above; a dot labels to the right, except that on a band y axis a dot at the low end of its row’s dots and intervals labels to the left, so a dumbbell’s labels sit outside its segment; a candlestick labels above its wick; a line labels its last point.
tone is "series" (default), "neutral" (the line role, for context marks) or "accent" (the accent role).
text is the note of a rule or span, at most 80 characters. A rule across x writes its note in a row above the plot, right of the line, or left of it when the right side is taken; a third note at the same place sits inside the plot. A rule across y writes its note above the line at the plot’s right edge. A span writes its note inside its top-left corner.
thickness is the stroke width in px, 1–40. An interval defaults to 2 px when neutral, else 45% of the band width up to 12 px; a rule defaults to 1.25 px.
- A member a mark type does not accept fails with
E021, as does a mark missing a required member.
"stack": true on a bar or an area stacks the mark’s items: a stacked bar has one stack per category, a stacked area one per x value. Each stacked mark stacks only its own items.
- A stacked mark needs
series (E021 at /marks/{i}/stack). An area with y2 cannot stack (E021 at /marks/{i}/stack). stack on any other mark type fails with E021.
- Segments stack in series order (Series and colour). Values at or above zero stack up from zero and negative values stack down from zero, each side on its own.
- A segment runs from the sum of the values below it on its side to that sum plus its own value. Sums use exact decimal arithmetic on the values as written; a position is the correctly rounded binary64 value of its sum, so
0.1 and 0.2 end exactly on an authored bound of 0.3. A sum beyond the finite binary64 range fails with E022.
- A stack holds one value per series. A second row of the same series at the same category or
x fails with E022 at that row.
- A stacked area needs every series it draws at every
x that any of its series has. A missing point, from an absent row or a row the mark skips for a missing or null value, fails with E022 naming the series and the first x it lacks. Write 0 where a series has nothing.
- Every segment keeps its own value: its
<title>, its data-merlion-* attributes and its outline line carry the value as written, never a running sum.
- A computed value domain spans every stack. Every stack must lie inside an authored domain (
E022, naming the stack and the sum it reaches). The zero rule of bars and areas without y2 applies unchanged.
- A stacked bar takes its category’s whole bar width. Segments next to each other in one stack are separated by a 2 px gap; the outermost segment on each side of zero has the rounded end and the others are square.
- A stacked area draws its layers in series order at 85% opacity, each outlined 1 px in the background colour, so the boundary between two layers shows.
"total": true on a stacked bar writes each stack’s total beyond the stack’s end: the exact sum of all its values, both signs, in plain decimal notation with as many fractional digits as the mark’s most precise value. Precision counts digits as written, including the exponent, so a value such as 0e-99 gives every total 99 fractional digits, and a total longer than the drawn-text limit fails with E004. A total at or above zero sits beyond the positive end, a negative total beyond the negative end. total without stack: true fails with E021; an area takes no total.
- A stacked bar takes no
label (E021 at /marks/{i}/label): its segments are too short to hold a label beside each, and their values are in the tooltips and the outline.
A candlestick mark draws one candle per row: a wick from low to high and a body from open to close.
x must be on a band or time axis and the four values on a linear y axis; any other pair of axes fails with E021 at /marks/{i}. A candlestick takes no series and no tone (E021): its direction sets its colour.
high must be at or above open and close, and low must be at or below them. A value out of order fails with E022 at its JSON path (/data/{row}/{field}, or /marks/{i}/{member}/value for a constant), naming the value it must bound.
- A candle rises when
close is at or above open, and falls otherwise. The order check and the direction compare exact decimal values as written, so "1" is below "1.00000000000000001" and "48.25" equals "48.250".
- A rising candle draws in the
ok role (--merlion-ok) with a hollow body filled with the background colour; a falling candle draws in the danger role (--merlion-danger) with a solid body. The fill shows the direction without colour.
- The body is 60% of the spacing between candles, from 1 to 12 px wide, centred on the wick. The spacing is the category step on a band axis. On a time axis it is the distance between the two closest distinct instants of the chart’s candles, or the domain’s span when every candle sits at one instant, and the axis extends half the spacing, rounded up to a whole second, beyond its domain at each end, so the first and last bodies lie inside the plot and
data-merlion-x-domain writes the drawn domain exactly.
- A body is at least 1 px tall: a doji,
open equal to close, draws a body line centred on its value. The wick and the body outline are 1.25 px wide.
low and high widen a computed y domain and must lie inside an authored one (E022); open and close lie between them.
label writes its value above the wick, wrapped to the spacing less 4 px, from 24 to 160 px, and moved inward where it would cross the plot’s left or right edge. The gutter above the plot holds the tallest candle label.
- A row missing any of the five values, or its
label, is not drawn and counts toward the mark’s I041.
- A candlestick adds no legend entry.
"type": "log" places each value at log10 of the value, so equal ratios take equal lengths. ADR-0018 records why positions come from fixed-point arithmetic on the written decimal.
- Every value drawn on a log axis must be greater than zero. Zero or a negative value fails with
E022 naming its JSON path and the value as written: `/data/1/mwh`: a log axis needs a value greater than zero, found "0".
- Both ends of a log axis
domain must be greater than zero (E022 at /{axis}/domain/{i}).
- The ends of a log axis
domain must have distinct positions. Ends whose log10 rounds to one binary64 value, such as [100000, 100000.0000000001], fail with E022 at /{axis}/domain, although the two values differ.
- A log axis must not set
zero (E021 at /{axis}/zero): it has no zero.
line, dot, interval, rule and span draw on a log axis, and so does an area with y2, which fills between two values above zero. A bar, a stacked area and an area without y2 draw from zero, so each fails with E021 at /marks/{i} when its value axis is log. A candlestick takes no log axis (Candlesticks).
x and y may both be log.
- A value’s position, in decades, is
log10 of its text as written, a JSON number lexeme or a decimal string, never of its binary64 approximation. The integer part, floor(log10), is exact. The fraction is log10 of the first 19 significant digits, truncated, computed in integer arithmetic in units of 2^-64 decade and within 2^-59 decade of the exact value. The position is then rounded once to binary64.
- A value written as a power of ten (
0.001, 1e-3, 1000.0) sits at its integer exponent exactly, on the tick of that power.
- A position never decreases as the value grows, across decade boundaries included.
- A value sits
(p − p0) / (p1 − p0) of the plot from the domain start, where p, p0 and p1 are the positions of the value and the domain ends. A tick at 10^k in a domain from 10^e0 to 10^e1 sits at exactly (k − e0) / (e1 − e0) of the plot.
- Native and WASM output are byte-identical: no step calls a platform logarithm.
- Without
domain, a log axis spans whole decades, from the power of ten at or below the smallest value drawn on it to the power of ten at or above the largest. Each end then moves out to a multiple of the decade stride, so both ends are ticks. Moving the ends out lengthens the domain; while one stride then spans less than 56 px of a y axis or 90 px of an x axis, the stride takes the next step of 1, 2, 5, 10, … and the ends move out to its multiples. 1 to 1,000 over 84 px of height takes a stride of 5 decades and the domain 1 to 100,000.
- A single value fills its decade: 30 alone gives 10 to 100. A single power of ten widens one decade each way: 100 alone gives 10 to 1,000.
- An authored
domain keeps its ends as written; they need not be ticks. Every value must lie inside it (E022), as on a linear axis, compared by position: 10.0000000000000001 lies outside [1, 10], although its binary64 value is 10.
- The decade stride is the smallest of 1, 2, 5, 10, 20, 50, … decades that spans at least 56 px of a
y axis or 90 px of an x axis, the per-tick lengths of a linear axis, and at most 10,000 decades. Powers of ten at multiples of the stride are ticks.
- With a stride of 1, ticks at 2 × 10^k and 5 × 10^k appear when
log10(2) of a decade, the narrowest gap among 1, 2, 5 and 10, spans at least 56 px of a y axis or 90 px of an x axis.
- If fewer than two of these ticks fall inside an authored domain, the axis takes linear ticks over the domain’s values, each at the position of its exact decimal. Equal steps in value narrow toward the top of a log axis, so the step is the linear step (Layout) or the next 1-2-5 step above it whose top gap spans at least 56 px of a
y axis or 90 px of an x axis: [30, 45] over 600 px of width ticks at 30, 35, 40 and 45.
- Neighbouring ticks of a log axis are at least 56 px apart on a
y axis and 90 px on an x axis.
- An axis has at most 64 ticks.
- A tick label prints through the axis
format from the tick’s integer significand and exponent, never from a binary fraction, so 0.001, 0.002 and 1,000,000,000 print exactly. Without decimals, the label of m × 10^k has max(−k, 0) fractional digits.
- Each tick has a grid line carrying
data-merlion-value with its label, as on a linear axis. A log axis has no zero line.
- Series take colours
--merlion-series-1 … -8 in order: series.domain when given, else first appearance in marks, then rows. A series name outside series.domain fails with E022.
- A ninth series fails with
E023: the categorical palette has eight colours, and a generated colour would repeat or clash. Fold the smallest series into “Other” or split the chart.
- The legend lists every drawn series with a 10 px swatch, in series order, above the plot, wrapping to the chart width.
- Colour never carries meaning alone: with two or more series the legend names each one, and every item has a
<title> naming its series and values. A candle’s direction is also in its body’s fill and its values.
- The chart is
target_width px wide (at least 240). The text of ticks, labels, notes, titles and the legend is drawn at 6/7 of font_size (12 px at the default 14 px) and measured with the same font tables as diagram labels (text-measurement.md).
- Margins come from measured text: the widest
y tick label or category label sets the left margin, and a mark that labels beyond its end, including a stacked bar’s totals, gets a gutter of its widest label on that side, so no label crosses the plot edge.
- A linear axis has about one tick per 56 px of height or 90 px of width, between 2 and 10, at the 1-2-5 steps of Heckbert’s nice numbers. A tick label is printed from the integer multiple of its step, so it is exact. If widening to a whole step exceeds the finite binary64 range, the computed domain keeps that finite endpoint. A time axis uses the Gantt automatic interval for one tick per 90 px, aligned to UTC boundaries.
- A band axis divides its length evenly among categories. A bar fills 72% of its category’s step, at most 48 px per series on a vertical chart and 22 px per series on a horizontal one, where a stacked bar counts as one series; bars of one category are separated by a 2 px gap. A horizontal chart gives each category at least 28 px. Category labels wrap: below a vertical chart to their step, beside a horizontal chart to 180 px or 32% of the width.
- Without
height, a chart whose y axis is not band has a plot of clamp(round(0.45 × width), 180, 380) px.
- Grid lines run from each tick of a linear, log or time axis across the plot. A zero line runs across the value axis of bars and areas, else across a linear
y axis that spans zero. A log axis takes its ticks from Log axes.
- The root has
class="merlion merlion-chart" and data-merlion-chart="1". <title> holds title; <desc> holds description or a generated sentence naming the mark types (stacked bar, stacked area for stacked marks), axes with their types (y axis Run time (log)), row count and series count.
- The root also carries the chart’s scale, which the viewer’s crosshair and brush read (chart-interaction.md, ADR-0015):
| Root attribute |
Value |
data-merlion-plot |
{x} {y} {w} {h}: the plot box in viewBox units, with the same two-decimal numbers as the geometry |
data-merlion-axes |
{x type} {y type}, each band, linear, log or time |
data-merlion-x-domain |
Present when x is linear, log or time: {min} {max}, the domain the chart draws with, after widening. Linear and log: in data units, the shortest decimal that reads back as the same binary64 value, never with an exponent; a computed log domain prints its ends as powers of ten (10 10000000), and is absent when an end lies outside the binary64 range. Time: YYYY-MM-DDTHH:MM:SSZ, UTC |
- An item’s position along a linear or time
x axis is x + (v − min) / (max − min) × w to the geometry’s two decimals, with v, min and max read as numbers, or on a time axis as milliseconds since the Unix epoch. Along a log x axis it is x + (lg v − lg min) / (lg max − lg min) × w with lg = log₁₀, to the same two decimals.
- Each mark is a
<g class="merlion-chart-mark merlion-chart-{type}" data-merlion-mark="{index}">. Each item inside is a <g class="merlion-chart-item …"> with a <title> tooltip and these attributes:
| Attribute |
Value |
data-merlion-row |
The row index, absent for a constant |
data-merlion-x, -y, -x2, -y2 |
The value as written in the spec |
data-merlion-open, -high, -low, -close |
On a candle: the value as written in the spec |
data-merlion-series |
The series name |
data-merlion-points |
On a line or area path: the number of points |
- Each line or area path contains a
merlion-chart-point group per source point with its row index, raw channel and series attributes, and value tooltip. A transparent circle of radius 6 px at the point exposes that tooltip without adding a visible mark; an area’s tooltip includes y2 when present. On a stacked area the circle sits on the layer’s edge farther from zero, where its value ends.
- A stacked area’s path has the class
merlion-chart-layer; the stylesheet carries that rule only when the chart has a stacked area.
- A candle item has the class
merlion-chart-up or merlion-chart-down, which sets color from --merlion-ok or --merlion-danger, in place of a series class. It holds a <line class="merlion-chart-stroke"> wick, then a <rect> body stroked in currentColor: merlion-chart-hollow, filled with --merlion-bg, when rising; merlion-chart-solid, filled with currentColor, when falling. Its <title> is {x} | open {o} | high {h} | low {l} | close {c}. The stylesheet carries these four rules only when the chart draws a candle.
- Grid lines carry
data-merlion-value with the tick label; the zero line carries data-merlion-value="0". Tick labels are merlion-chart-tick, item labels merlion-chart-label, stack totals merlion-chart-label merlion-chart-total, notes merlion-chart-note, axis titles merlion-chart-title. An item label carries its item’s data-merlion-series when the item has a series.
- Items paint with
currentColor. The class merlion-chart-s{k} sets color from --merlion-series-{k}; merlion-chart-neutral from --merlion-line; merlion-chart-accent from --merlion-accent. Every element also carries its literal colour as a presentation attribute. A palette bakes literals and a dark variant exactly as for diagrams.
- The outline is
Chart. {title}. {n} rows., the series list, then one line per row with the fields the marks read, band fields first, joined by |, - for a missing value. A field a candlestick reads is prefixed with its role, open, high, low or close, so a candle’s row reads {x} | open {o} | high {h} | low {l} | close {c}; a field read by several candlesticks takes the role of the first. Each stacked bar with total then adds a {category field} | total line and one {category} | {total} line per stack, in category order.
| Code |
Severity |
Meaning |
E020 |
Error |
The input is not valid JSON, exceeds a nesting, object-member or array-item limit, or repeats a member name |
E021 |
Error |
The spec’s shape: an unknown or missing member, a wrong type, a mark on the wrong axes, a field no row has, a stack without series or on an area with y2, total without stack, label on a stacked bar; zero on a log axis, a bar, stacked area or area without y2 on a log axis |
E022 |
Error |
A value: not a decimal or date, outside an authored domain, a category or series outside its domain, zero or negative on a log axis, log domain ends that share one position; a stack outside an authored domain or past the binary64 range, two values of one series in one stack, a stacked area missing a series at an x; a candle whose high is below its open or close, or whose low is above them |
E023 |
Error |
More than eight series |
I041 |
Info |
Rows a mark skips for a missing or null value |
strict promotes warnings and repairs to errors as for diagrams; charts emit none of either.
| Limit |
Value |
| Input / nesting |
limits.input_bytes / limits.nesting |
| Rows / fields per row |
10,000 / 64 |
| Marks |
16 |
| Categories per band axis |
500 |
| Series |
8 |
| Ticks per axis |
64 |
| Fuel |
One unit per drawn item (each line or area point and each candle counts once), a second unit per item of a stacked mark for its sum and total, plus one per mark, against fuel |
| Drawn item text |
8 MiB before XML escaping: position and series values, a candle’s five values among them, count twice (attribute and tooltip), labels once, rule and span notes twice, stack totals once |
The fuel and drawn-text checks precede resolved-item allocation; exceeding either fails with E004. A total’s length depends on its sum, so each stack total counts toward the text limit once summed, before its text is built. The text limit bounds repeated constants in SVG metadata and tooltips. Empty marks cost one unit; an all-constant mark costs two units. Missing or null fields consume no item unit. Resolved source text shares each constant across its rows; geometry and tooltip parts share that same text until SVG serialization.
Stacks normalised to 100%, labels inside stack segments, facets and small multiples, a second value axis, log scales in a base other than 10 or across zero, and annotations other than rule and span. A second value axis is excluded by design: two measures with different units get two charts or an index to a common base.
crates/merlion-render/tests/chart_acceptance.rs renders every fixture in tests/fixtures/chart/ strictly, checks byte-identical repeats, source values in data-merlion-* attributes and the outline, the root’s plot box, axis types and x domain against the grid and the items, the series on item labels, the zero line, the legend rule, stack geometry and totals, candle geometry, direction and order, log tick positions against (k − e0) / (e1 − e0) by exact equality, log subdivisions, labels and computed domains, and each error code with its JSON path, including one fixture per stacking, candlestick or log-axis error in tests/fixtures/chart/invalid/. It records each fixture’s SVG and outline digest (FNV-1a 64, data-merlion-version blanked) in tests/fixtures/chart-digests.txt.
crates/merlion-render/src/math.rs tests the fixed-point log10 against 80-digit reference values and checks that it never decreases.
packages/merlion-wasm/test/glue.test.mjs renders a fixture through renderChart from a string and from an object, checks errors through checkChart, and renders every fixture in chart-digests.txt through the WASM build to the recorded native digests.
crates/merlion-cli/tests/cli.rs checks that merlion render --chart writes the same bytes as the core.