@softure-ai/charts
v0.5.1
Published
SOFTURE AI charts: scales, ticks and nearest-point search, plus server-rendered SVG line charts with axes, legend, flags, a keyboard cursor and a data table, areas and stacked areas, step series, surfaces, an event axis, a Sankey and bar lists on the --sf
Readme
@softure-ai/charts
Accessible line charts rendered on the server, and the arithmetic under them. The arithmetic is pure functions, with no DOM and no React, so the drawing, the cursor and the tests share one source:
- linear and time scales with an inverse (Scales);
- the peak and the trough of several series and round value ticks between them (Value ticks);
- calendar date ticks (days, months, years) at midnight in the app's time zone, labelled through
Intl(Date ticks); - the point nearest to a cursor (Nearest point);
- a step in a series and stacked bands (Steps and stacked areas), and stable colours for an open set of keys (Colour by meaning).
On top of it, React components (Components): LineChart for time series, and the
primitives it is made of (plot, grid, lines, axes, legend, flags, event pins, a keyboard and pointer cursor, a data
table) for charts of your own, coloured by a six-colour series palette that a test guard keeps legible and
apart for colour-blind readers (Series palette). Extracted from the hand-rolled
charts of an adopting app.
Installation
npm install @softure-ai/chartsIt needs @softure-ai/ui 0.1.6 or later (the --sft-chart-* tokens and @softure-ai/ui/testing) and React 19, both
peer dependencies: the app installs them once, so the tokens, the theme and the components come from one copy of
@softure-ai/ui.
Each version ships to npm, to GitHub Packages as @softure/charts, and as a tarball on the GitHub Release
[email protected]; the first one is 0.1.0, released with @softure-ai/ui 0.1.6.
Points
A series is an array of ChartPoint, { x: Date | number; y: number }. y is in the caller's unit (PLN,
cents, percent); nothing here assumes one.
Scales
import { linearScale, peakOf, timeScale } from "@softure-ai/charts";
const peak = peakOf([base, variant]); // the maximum over every series, not the first
const y = linearScale({ domain: [0, peak], range: [height - 24, 8] }); // the peak sits 8 px under the top
const x = timeScale({ domain: [start, horizon], range: [8, width - 8] });
y(value); // pixel
x(date); // pixel; a Date or epoch milliseconds
x.invert(pointerX); // Date under the pointer- The domain is what you state (a horizon), not the last point drawn, so a sparse series and the cursor stay on the same scale.
- A zero-width domain maps every value to the range start, so an empty chart draws finite numbers.
peakOf(series, { getValue, floor })takes another value per point (a stack of parts) and a lowest peak; it floors at 0 by default.troughOf(series, { getValue, ceiling })is its mirror: the lowest value, at most 0, the bottom of a domain with negative values ([troughOf(…), peakOf(…)]).invertextrapolates outside the range; it does not clamp.
Value ticks
import { valueTicks, yearTicks } from "@softure-ai/charts";
valueTicks(2_827_053, 4, { minStep: 1 }); // [500000, 1000000, 1500000, 2000000, 2500000]
yearTicks(2026, 2081, 5); // [2030, 2040, 2050, 2060, 2070, 2080]Steps are 1, 2, 2.5 and 5 × 10ⁿ; the tick count closest to the target wins and a tie goes to the larger
step. Ticks are multiples of the step in (0, max]: the scale's peak never moves, only the grid lines are
placed, and zero is left to the baseline. minStep keeps a step from going below what the labels can show
(1 for whole units). With min below zero (a trough) the step is chosen for the whole span and ticks also fall
on its multiples down to min: valueTicks(3_000, 4, { min: -1_000 }) is [-1000, 1000, 2000, 3000].
Date ticks
import { dateTicks, formatDateTick } from "@softure-ai/charts";
const axis = dateTicks({ start, end, target: 5, timeZone: config.timezone });
// { unit: "year", step: 10, ticks: [Date, …] } or null
const labels = axis?.ticks.map((tick) => formatDateTick(tick, axis.unit, { locale, timeZone: config.timezone }));- A tick is local midnight on a calendar boundary in the given IANA zone (core's
config.timezone), not in the machine's zone: days (steps 1, 2, 7, 14; weeks start on Monday), months (1, 2, 3, 6, aligned to January) and years (1, 2, 5, 10, 20, 25, 50, aligned to multiples). Across a DST change each tick stays on midnight; a midnight that does not exist (a gap at 00:00) becomes the first instant after it. - Ticks lie in
[start, end]; the step rule is the value ticks'.nullmeans nothing to show: an empty span, a target below one, or no boundary inside the span. - Labels come from
Intl.DateTimeFormatin the app's locale:2026,Apr 2026,Apr 1(en-US). Core has no date formatter, so nothing else is needed. - Candidates with far more ticks than the target are skipped before they are built, so a century never builds day ticks.
- An invalid zone throws
Intl'sRangeError: it is a configuration bug, caught by core at startup.
Nearest point
import { nearestPointIndex } from "@softure-ai/charts";
const index = nearestPointIndex(points, x.invert(pointerX)); // number | nullThe pointer's pixel goes through the same scale that drew the line, so the cursor and the drawing cannot
drift apart. Points may come in any order; a position off either end gives the edge point; of two equally
close points the first wins; no points gives null.
Components
Import the stylesheet after @softure-ai/ui's, which defines the --sft-chart-* tokens:
@import "@softure-ai/ui/styles.css";
@import "@softure-ai/charts/styles.css";LineChart
import { LineChart } from "@softure-ai/charts";
<LineChart
title={messages.chart.title}
series={[
{ key: "savings", label: messages.chart.savings, points: savings }, // { x: Date; y: number }[]
{ key: "spending", label: messages.chart.spending, points: spending, dashed: true },
]}
flags={[{ key: "raise", x: raiseDate, label: messages.chart.raise }]}
locale={config.locale}
timeZone={config.timezone}
formatValue={(value) => money.format(value)}
/>;A server component. It draws a <figure>: the title as caption, value labels from zero (or from the lowest
value when it is negative, with the baseline staying at zero) to the peak (formatValue), grid, one line per series, a dashed guide and a chip per flag, month or year labels in the
app's time zone, a legend, the cursor and the data table. Series share their x values (one table row and one
cursor stop per date; different dates throw a TypeError). formatDate changes the readout's and the
table's dates (a medium date in timeZone by default); valueTickTarget and dateTickTarget the number of
labels; messages the built-in copy.
Stacked flags
Close flags overlap on the top row. flagStack stacks them into rows: give each chip's width in percent of the
plot's width (measured in the browser, or estimated from the label), an optional gap (same unit) and maxRows.
<LineChart
{...props}
flags={flags}
flagStack={{ widthOf: (flag) => flag.label.length * 1.2 + 3, gap: 1, maxRows: 2 }}
/>;Each chip takes the lowest row it fits, by x, then input order. A flag that fits in no row keeps its guide, and the
hidden flags share one outline chip on the row after the last, at the leftmost one's x, reading "+N"
(lineChart.moreFlags) with their labels as its title. Without flagStack every chip sits on the top row as before.
layoutFlags(flags, { gap, maxRows }) is the layout on its own, server-safe, for an app that places its own chips:
each flag is { x, width, align?, rows? } in one unit (align as ChartFlag aligns it, edgeAlign(x) there;
center by default). It returns placements in input order ({ hidden: false, row, start, end } or
{ hidden: true, row: null, start, end }), rows and hiddenCount, the same for the same input; chips in one row
are at least gap apart. A flag with rows: 2 (a two-line chip, e.g. two close dates merged into one) takes its
row and the one below it, and a flag takes a row only where every row it spans is free at its extent, so it lifts
only the flags it collides with. rows in the result counts the spanned rows, and maxRows applies to them: a flag
whose span would end below maxRows is hidden. A non-finite x or width, a negative width or gap, or a
rows or maxRows that is not a whole number of at least 1 throws a RangeError. ChartFlag's row moves a chip down by its own height and
--sft-space-1 per row (--sft-chart-flag-row); give a spanning chip the same rows, and it is as tall as its rows
(one line and the chip's padding per row) and still moves by one row per row:
const { placements, hiddenCount } = layoutFlags(
events.map((event) => ({ x: event.xPercent, width: measure(event.label), align: edgeAlign(event.xPercent), rows: event.rows })),
{ gap: 1, maxRows: 3 },
);
events.map((event, index) => {
const placement = placements[index];
return placement && !placement.hidden ? (
<ChartFlag key={event.key} xPercent={event.xPercent} row={placement.row} rows={event.rows}>
{event.label}
</ChartFlag>
) : null;
});An app with its own greedy stacking (rows by x and measured width, a row cap, a "+N" overflow) drops it for
layoutFlags, or for flagStack when it draws a LineChart.
Accessibility
- The SVG is hidden from assistive technology. Every chart carries its data as a visually hidden
<table>(ChartDataTable) with the title as caption. - The cursor (
ChartCursor, a client component) is a focusable group named "{title}. Use the left and right arrow keys…" (chartsMessages.en.cursor.label). ← and → step through the points (the first → lands on the first, the first ← on the last), Home and End jump, Escape clears. The readout under the plot is a polite live region, so each point is announced with the text a sighted user reads. - A pointer snaps to the nearest point; pointer events cover mouse, pen and touch, and
touch-action: pan-yleaves vertical scrolling to the page while a sideways drag moves the cursor. - Labels are HTML, never SVG text, so they do not stretch with the plot; strokes do not scale either.
Primitives
| Component | What |
| --- | --- |
| ChartPlot | the SVG in one viewBox (PLOT_WIDTH × PLOT_HEIGHT, 1000 × 400) stretched to its box, plus an HTML overlay |
| GridLines, Baseline, GuideLine, SeriesLine | SVG lines in viewBox units; a guide is dashed, dotted or solid, a series takes a colour slot (seriesSlot(index)), a tone or a color, a pattern (solid, dashed, dotted) and curve (linear or smooth); every line takes look options |
| Area | the filled areaPath under a line or between two, tinted (--sft-chart-tint-opacity, 0.24) in a slot, tone or color; baseline, curve, opacity (fill), className, style, data-* |
| ValueAxis, valueAxisTicks | value labels at heights in %; from four labels, every other one is hidden on narrow screens, counted from the top (narrow="all" keeps them all) |
| TimeAxis, timeAxisTicks, numberAxisTicks | date or number labels at % of the width; with ends, the ends sit at the edges and middle labels stay clear of them; an optional second row (Horizontal axes) |
| Legend, LegendItem, LegendSwatch | swatches box, dot, line, dashed, dotted in a slot, a tone or a color, faded; LegendItem takes as (li, div, span) to sit outside Legend's list, and faded (Colour by meaning) |
| ChartFlag | a chip at the top of the plot; within 18 % of an edge it aligns to that edge (edgeAlign); variants and sizes (options); row, rows and layoutFlags stack it (Stacked flags); title |
| ChartPin | an event pin on a curve: a dashed line from the bottom up to a point and a round dot on it (Pins) |
| ChartDataTable | the visually hidden table |
| Sankey, layoutSankey | a two-sided flow: inputs → hub → outputs (Sankey) |
| BarList, renderBarListHtml, getBarListRows | labelled horizontal bars with values, in React or as an HTML string (Bar list) |
| ChartCursor | the client cursor around the frame; takes points as percentages computed on the server (Cursor) |
| StackedAreas, stackSeries | series stacked into bands, each an Area between its edges (Steps and stacked areas) |
| EventAxis, layoutEventAxis, layoutEventRail | a timeline of spans and milestones, or of milestones on one rail, with a keyboard and pointer readout (Event axis) |
| ChartSurface, getChartSurfaceAttributes | a chart on the page, a dark band or a paper card (Surfaces) |
Paths
| Helper | What |
| --- | --- |
| linePath(points) | a polyline through the points, in order |
| smoothLinePath(points) | a smooth curve through every point (monotone cubic, Fritsch–Carlson): it never overshoots between two points, so a step in the data draws no dip or peak; a vertical step stays vertical |
| areaPath(points, { baseline, curve }) | the closed shape under a line, to fill: down to baseline (a y, PLOT_HEIGHT by default) or back along a lower edge of points (same x-range, left to right), so a stacked band is areaPath(upper, { baseline: lower }); curve: "smooth" smooths both edges |
<path d={areaPath(points, { curve: "smooth" })} className="app-area" />
<path d={areaPath(upper, { baseline: lower })} style={{ fill: band.colour }} />
<SeriesLine points={points} slot={0} curve="smooth" />
<Area points={points} curve="smooth" tone="accent" /> {/* the same path as a component */}Steps and stacked areas
withStepAt(points, index) gives a series a vertical jump at one point (funds unlocking on a date): a copy of the
point carrying the previous y goes in before it, so the line runs flat to that x and then rises or falls straight,
with linePath and smoothLinePath alike. Every other field of the point is kept; the first point, or an index
outside the series, returns an unchanged copy.
stackSeries(series) stacks series in data space, first at the bottom: each input is { key, points, slot?, tone?,
color? }, all sharing their x values (a mismatch throws a RangeError naming the series). At each x a positive value
sits on the positive ones before it and a negative one under the negative ones, so bands never overlap. Each band
keeps its look and gets lower and upper edges; StackedAreas draws them as Areas through the chart's scales,
each in its own slot, tone or color, else seriesSlot(index), with data-series set to its key.
const bands = stackSeries([
{ key: "bonds", points: bonds },
{ key: "stocks", points: withStepAt(stocks, unlockIndex) },
]);
const peak = peakOf(bands.map((band) => band.upper));
const trough = troughOf(bands.map((band) => band.lower));
<StackedAreas bands={bands} x={xScale} y={yScale} curve="smooth" />;A stepped series stacks only with series stepped at the same index, since stacking needs shared x values.
Positions are percentages of the plot (toPercent), computed once from the scales, so the overlay and the
drawing cannot drift apart. Classes are sft-chart-* in the softure layer; an app's own classes win.
Line and marker options
Every line (GridLines, Baseline, GuideLine, SeriesLine) and marker (ChartFlag, ChartPin) takes
className (added next to the package class, so a stroke keeps non-scaling-stroke), style (applied last) and
data-* attributes. On top of that:
| Option | On | What |
| --- | --- | --- |
| tone | lines | a role colour from the tokens: cursor, axis, grid, flag, foreground, muted, accent, danger, success, warning |
| slot | lines, ChartPin | a series colour (seriesSlot(index)) instead of the tone |
| strokeWidth, opacity | lines | stroke width in screen pixels and stroke-opacity, inline so they beat the class; for SVG opacity (it blends a shade differently) pass style={{ opacity }} |
| variant | ChartFlag | flag (the default chip), ink (text colour with the page colour as text, on any surface), outline |
| variant | ChartPin | the dot's fill: flag (default) or ink |
| size | ChartFlag sm md; ChartPin sm md lg | sizes derived from --sft-text-xs and --sft-chart-dot-size |
| ring | ChartPin | axis (default, 3:1 on a light card) or surface, the card colour, so the dot cuts the line on a dark band |
| classNames | ChartPin | { line, dot }: classes for the line and the dot, after the package classes (className goes to the column) |
| xPercent optional | ChartFlag, ChartPin | omitted, the marker is not positioned across and its parent places it (a column with the event's left); a free flag has no top, so the parent may place it by bottom |
<GuideLine x={xScale(exit)} tone="accent" strokeWidth={1.5} data-testid="exit-guide" />
<GuideLine x={xScale(unlock)} style={{ stroke: position.colour }} /> {/* an app colour that is no token */}
<GridLines ys={ys} opacity={1} />
<ChartFlag variant="ink" size="sm" className="app-flag-lift">{label}</ChartFlag>
<ChartPin yPercent={y} ring="surface" size="sm" classNames={{ dot: "app-dot-on-band" }} />
<ValueAxis ticks={ticks} narrow="all" /> {/* a small chart with few short labels */}A chart on another surface: see Surfaces.
Surfaces
The same chart on the page theme, on a dark band and on a paper card: LineChart surface="dark", or any parts in
<ChartSurface surface="paper"> (as: div, section, span). A surface is a colour scheme of
@softure-ai/ui, which scopes all of its tokens to any element with data-theme, so grid, axis, labels, flags and
series switch together and nothing inside needs a prop:
| Surface | data-theme | Use |
| --- | --- | --- |
| page | none: follows the page | the default |
| dark | dark | a dark band, whatever the page theme |
| paper | light | a light card, whatever the page theme |
getChartSurfaceAttributes(surface) gives { className, "data-theme" } for an element the app renders itself;
CHART_SURFACES lists the names. Each surface element carries sft-chart-surface-<name>, the hook for tuning one
surface's tokens (.sft-chart-surface-paper { --sft-chart-grid: var(--app-paper-line); }). An app with its own theme
(SoftureThemeProvider) gets its colours on the attribute scopes too; schemeScopes adds them to its own band
classes.
Colour by meaning
A series coloured by what it means (a goal, a loss) takes a tone instead of a slot on SeriesLine, Area,
LegendSwatch, Sankey nodes and BarList rows; a colour that is no token goes in color (a CSS colour or
var(--app-…), set inline as --sft-chart-series). A slot wins over a tone, a color over both.
<SeriesLine points={goal} tone="success" pattern="dotted" />
<LegendItem swatch={<LegendSwatch tone="success" shape="dotted" />}>{copy.goal}</LegendItem>
<LegendItem as="div" faded swatch={<LegendSwatch color="var(--app-retirement)" shape="box" />}>{copy.retirement}</LegendItem>For an open set of bands or categories, distinctColors(keys, palette?) maps each key to a colour: series slots by
default, or the values of an app's palette (CSS colours, class names). Each key hashes to a preferred colour and keys
are placed in sorted order, so the map does not depend on the keys' order, a key keeps its colour when another joins
(unless the newcomer sorts first and takes that colour), and two keys share a colour only past the palette's size.
const slots = distinctColors(bands.map((band) => band.key));
<StackedAreas bands={bands.map((band) => ({ ...band, slot: slots.get(band.key) }))} x={xScale} y={yScale} />;CHART_TONES lists the tones; ChartToneName is ChartTone under a name that does not collide with an app's own
ChartTone type. faded dims a swatch or a whole item to --sft-chart-faded-opacity (0.45).
Sankey
layoutSankey(items, { nodeWidth, gap, labelSpacing, width, height }) lays out a two-sided flow in viewBox units:
items with side: "in" stack on the left, side: "out" on the right, both top to bottom in order with gap between
nodes, and every node has one ribbon to its slice of the hub in the middle. One scale serves both sides, so a ribbon is
as tall at both ends; the hub is the larger side's sum, and each side is centred. Label centres move apart to
labelSpacing where nodes are thin and stay inside the height. Values below zero count as zero.
Sankey draws it: label column | plot | label column, the labels HTML (label, then valueLabel under it), the
plot hidden from assistive technology, so pair it with a ChartDataTable. A node is fill: "solid" (default),
"tint", or "hatch" (tinted with diagonal lines in its own colour); ribbons are tinted.
<Sankey
items={[
{ key: "salary", side: "in", value: 9000, label: copy.salary, valueLabel: format(9000), slot: 1 },
{ key: "rent", side: "out", value: 3000, label: copy.rent, valueLabel: format(3000), tone: "danger" },
{ key: "invest", side: "out", value: 2000, label: copy.invest, color: "var(--app-invest)", fill: "hatch" },
]}
/>Variants, all optional (without them the markup is that of 0.1.7):
labels="none"draws the plot alone, without label columns: a compact drawing (a phone) whose labels the app lists outside it.leadersdraws a line from each label that was spread away from its node to the node's middle, in a strip between the label column and the plot (layoutSankeygives every nodecentreYandisLabelSpread). Only with label columns.aspect="fixed"keeps the plot at the viewBox's aspect ratio instead of stretching it to the plot height, so ribbons are not distorted; the label columns follow its height and the labels stay HTML. Pick the ratio withlayout={{ width, height }}(1000 × 400 by default).
<Sankey items={items} leaders aspect="fixed" layout={{ width: 600, height: 300 }} />
<Sankey items={items} labels="none" aspect="fixed" layout={{ width: 320, height: 320 }} />Custom properties on .sft-chart-sankey: --sft-chart-sankey-label-width (wider labels),
--sft-chart-sankey-leader-width (the leader strip), --sft-chart-sankey-link-opacity (ribbons) and
--sft-chart-sankey-node-opacity (tinted and hatched nodes); both opacities fall back to --sft-chart-tint-opacity.
Bar list
BarList draws labelled horizontal bars with their value text: each bar is value / max wide (max defaults to the
largest value), clamped to 0–100 %, in a slot, tone or color. It is a list whose text a screen reader reads, the
bars hidden, so it needs no data table. renderBarListHtml(props) returns the same markup as an escaped string, for a
renderer outside React (a blog body); getBarListRows gives the rows to an app's own markup.
<BarList items={rows.map((row) => ({ key: row.id, label: row.name, value: row.share, valueLabel: format(row.share) }))} />const html = renderBarListHtml({ items, max: 100 });Options, in both renderers:
layout="stacked"puts label and value on one row and the bar full width under them (inline, label | bar | value, by default).- An item with
missing: truedraws an empty track outlined dashed in itsslot,toneorcolor, and no bar; it does not count toward the defaultmax. ItsvalueLabelsays it is missing ("no data"): the outline is hidden from assistive technology. descriptionadds a line of text under the bar.
<BarList
layout="stacked"
items={[
{ key: "bonds", label: copy.bonds, value: 50, valueLabel: format(50), description: copy.bondsNote },
{ key: "gold", label: copy.gold, value: 0, valueLabel: copy.noData, missing: true, tone: "muted" },
]}
/>Horizontal axes
timeAxisTicks labels dates on a timeScale; numberAxisTicks labels numbers (a month index, an age) on a
linearScale, with the same edge rules:
const ticks = numberAxisTicks({
ticks: yearStartMonths, // month indexes from today
scale: xScale, // linearScale({ domain: [0, lastMonth], range: [0, PLOT_WIDTH] })
format: (month) => String(startYear + Math.floor(month / 12)),
ends: [0, lastMonth],
sublabel: (month) => String(startAge + Math.floor(month / 12)), // a second row: the age under the year
narrow: "alternate",
});
<TimeAxis ticks={ticks} />;sublabelputs a second row under each label (edges included);TimeAxisthen reserves two rows of height.narrowsets which labels stay on narrow screens:edgeskeeps only the end labels (the default withends),alternatekeeps every other middle label (withends, starting by hiding the one next to the start label),allhides none (the default withoutends).
Pins
import { ChartFlag, ChartPin, ChartPlot, PLOT_HEIGHT, PLOT_WIDTH, toPercent } from "@softure-ai/charts";
const xPercent = toPercent(xScale(event.date), PLOT_WIDTH);
const yPercent = 100 - toPercent(yScale(event.value), PLOT_HEIGHT); // from the bottom
<ChartPlot
overlay={
<>
<ChartFlag xPercent={xPercent}>{messages.chart.retirement}</ChartFlag>
<ChartPin xPercent={xPercent} yPercent={yPercent} />
</>
}
>
{/* grid, series */}
</ChartPlot>;The pin goes in ChartPlot's overlay, in HTML, so the dot stays round however the plot stretches (an SVG circle
in the stretched viewBox would be an ellipse). The line is the guide's dash (--sft-chart-dash,
--sft-chart-dash-gap, declared on the pin's column so a class there changes them) in --sft-chart-pin-line when an
app sets it (on the column or a wrapper, e.g. per surface), else in --sft-chart-cursor; the dot is --sft-chart-dot-size, filled with --sft-chart-flag
(the colour of the chip over it) or, with slot, with that series' colour, and ringed in --sft-chart-axis, so a
light fill on a light card keeps 3:1 (WCAG 1.4.11). line={false} draws the dot alone, for an event a GuideLine
already marks. Positions are not clamped, as for flags: they come from the drawing's scales. The pin is hidden from
assistive technology; the value belongs in the data table.
Cursor
ChartCursor (a client component) wraps the frame: points are the stops as percentages from the server's scales,
each with a heading and its values (a dot per value at yPercent). Options for an app that keeps its own readout
or plot; without them the markup is unchanged:
renderReadout(point, index)replaces the readout's content (heading and values). It still renders inside the package's polite live region, so what it shows is announced.onActiveChange(index | null)is called once per change of the active stop (keys, pointer, Escape, blur), not on every pointer move within one stop.frame={false}drops the frame grid: the children and the cursor layer share one positioned box (.sft-chart-cursor-box) and the layer covers it, so the guide, the dots and the pointer are measured on the app's own plot, e.g. one<svg>with HTML axis labels over it.- A value's colour is its series
slot; without one,tone(aChartTone) colours the dot and its readout swatch, andclassNameis added to both for an app's own colour. readoutClassNameis added to the readout..sft-chart-cursorisposition: relative, so a class can float the readout over the plot; pick the side from the indexonActiveChangereports.
"use client";
const [index, setIndex] = useState<number | null>(null);
const isPastMiddle = index !== null && (points[index]?.xPercent ?? 0) > 60;
<ChartCursor
title={title}
points={points}
frame={false}
onActiveChange={setIndex}
readoutClassName={isPastMiddle ? "app-readout app-readout-left" : "app-readout app-readout-right"}
renderReadout={(point, i) => <AppReadout point={point} month={months[i]} />}
>
<AppPlot />
</ChartCursor>;ChartCursor and EventAxis are client components: their built-in copy takes locale from the prop, then from the
nearest UiLocaleProvider of @softure-ai/ui, then en. The server components (LineChart and the primitives) cannot
read the provider and take locale as a prop.
Event axis
EventAxis (a client component) is a horizontal timeline: items are spans ({ kind: "span", startPercent,
endPercent }, a bar tinted in its colour with its label) and milestones ({ kind: "milestone", xPercent }, a dot on
a row of their own above the spans), each with key, label, an optional detail and a slot, tone or color.
Positions come from the chart's x scale as percentages, so the axis lines up with a chart above it. Spans are packed
into the fewest rows that keep them apart (layoutEventAxis, server-safe, also for an app's own markup).
It works like the cursor: a focusable group named from chartsMessages (eventAxis.label), ← and → step through
the items by start, Home and End jump to the ends, Escape clears; the pointer activates the item under it, and
leaving or blurring clears. The readout under the track is a polite live region with the label and the detail, or
renderReadout(item, index); onActiveChange(index | null) reports the item's index in items. Row height is
--sft-chart-event-lane-height (--sft-space-6) on .sft-chart-event-axis.
<EventAxis
title={copy.lifeEvents}
items={[
{ kind: "span", key: "school", label: copy.school, detail: "2026–2030", startPercent: x(schoolStart), endPercent: x(schoolEnd) },
{ kind: "milestone", key: "unlock", label: copy.unlock, xPercent: x(unlock), tone: "accent" },
]}
/>Rail
layout="rail" draws milestones only, on one line: each milestone after the first is joined to the previous one by a
segment, the rail itself, and no rows are packed (layoutEventRail, server-safe). Three optional milestone fields
shape the rail:
| Field | What it does |
| --- | --- |
| railSlot, railTone, railColor | the colour of the segment leading to this milestone, apart from its dot (color over slot over tone, as for the dot); without them the segment takes the dot's colour |
| segmentLabel | a short text on that segment, e.g. a duration; hidden when the segment is narrower than minLabelWidth (axis percent, default 0) and clipped with an ellipsis when it still overflows |
| tick | a short text under the dot, e.g. the year |
Keys, pointer and readout work as above; a segment activates the milestone it leads to. The track is hidden from
assistive technology, so the readout (detail or renderReadout) should carry the date and the duration. The rail is
--sft-chart-event-rail-width thick (--sft-space-1).
<EventAxis
layout="rail"
title={copy.plan}
minLabelWidth={8}
items={[
{ kind: "milestone", key: "today", label: copy.today, xPercent: x(today), tick: "2026" },
{ kind: "milestone", key: "deadline", label: copy.deadline, detail: "2050", xPercent: x(deadline), tick: "2050",
tone: "danger", railTone: "success", segmentLabel: copy.duration },
]}
/>Tokens
Colours: --sft-chart-{grid,axis,cursor,flag,on-flag} and the series palette --sft-chart-series-{1…6};
sizes: --sft-chart-{line-width,grid-width,dot-size,plot-height} (all in @softure-ai/ui). The value axis
column is --sft-chart-axis-width (5 rem); set it on .sft-chart for wider labels.
Series palette
Series take the colours in this order (SERIES_TOKENS, SERIES_SLOTS = 6); a seventh series repeats the
first, so tell it apart with dashed.
| Slot | Colour | Light | Dark |
| --- | --- | --- | --- |
| 1 | brand | #356912 | #cff26b |
| 2 | ink | #16171a | #f2f3f5 |
| 3 | purple | #a855f7 | #c084fc |
| 4 | pink | #db2777 | #db2777 |
| 5 | blue | #1e40af | #2563eb |
| 6 | teal | #0d9488 | #14b8a6 |
Every colour keeps 3:1 (WCAG 1.4.11) on color-background, color-surface and color-surface-raised, and
every pair stays at least 10 ΔE00 apart in normal vision and under protan, deutan and tritan simulation, in
both schemes. Measured floors: 14.4 ΔE00 in light, 11.5 in dark (the brand and ink pair). Later slots sit
further from the colours already used, so a chart with three series has 27.6 in light. Six is a choice, not
a limit: a seventh colour family still fits at 12.4 ΔE00
(research).
An app that overrides the series colours checks them the same way, in its own tests:
import { DEFAULT_THEME, mergeThemes } from "@softure-ai/ui";
import { checkSeriesPalette } from "@softure-ai/charts/testing";
it("our series colours stay legible and apart for colour-blind readers", () => {
expect(checkSeriesPalette(mergeThemes(DEFAULT_THEME, appTheme))).toEqual([]);
});checkSeriesPalette(schemes?, options?) returns every failure at once: low-contrast, missing-token or
unreadable-color (from @softure-ai/ui/testing's checkThemeContrast) and collision (scheme, pair as
chart-series-1 and chart-series-3, vision, distance, minimum). Options: tokens (the series, in slot
order), grounds (SERIES_GROUNDS by default), minDistance (10), visions and metric; with tokens and
grounds it checks an app's own token names. @softure-ai/charts/testing is never imported by the
components, so it stays out of app bundles.
Adopting from an app's own charts
The package came out of an adopting app's hand-rolled charts. What each part of such a chart maps to:
| An app's own chart | @softure-ai/charts | Stays in the app |
| --- | --- | --- |
| y and x scales | linearScale, timeScale (with invert) | — |
| the peak of stacked points | peakOf(series, { getValue }), troughOf for debt below zero | the point type and its sum |
| value ticks counted in cents | valueTicks(peak, target, { minStep: 100, min }) (unit-free) | the axis format, passed as formatValue |
| year ticks | yearTicks | month helpers |
| the nearest point to the pointer | nearestPointIndex(points, x.invert(pointerX)), in data space | — |
| grid, baseline and guide lines | GridLines, Baseline, GuideLine (dashed, dotted, solid; tone, slot, strokeWidth, opacity, style, data-*) | — |
| value and time axes | valueAxisTicks({ ticks, scale, format }), ValueAxis, timeAxisTicks / numberAxisTicks (with ends, sublabel, narrow), TimeAxis | — |
| legend swatches | LegendSwatch shapes box, dot, line, dashed, dotted in a slot, tone or color, faded; LegendItem as="div" outside a list | outlines from stored colours |
| raw <path d={linePath(…)}> and <path d={areaPath(…)}> | SeriesLine and Area with tone or color | — |
| a hand-built two-sided Sankey | layoutSankey, Sankey (solid, tint, hatch) | the flows and their copy |
| labelled horizontal bars | BarList, renderBarListHtml for an HTML body | the rows and their format |
| event chips and pins | ChartFlag (edge rule edgeAlign; variant, size, className, style), ChartPin (slot, variant, size, ring); both placed by their parent when xPercent is omitted | — |
| percentages | percent, toPercent, edgeAlign | — |
| a surface-to-tone map and wrapper components | surface on LineChart, ChartSurface (page, dark, paper) | tuned tokens on .sft-chart-surface-<name> |
| a step in a series, stacked band paths | withStepAt, stackSeries, StackedAreas | — |
| colours for an open set of bands | distinctColors(keys, palette?) | — |
| a hand-built timeline of spans and milestones, or a rail of milestones | EventAxis (layout), layoutEventAxis, layoutEventRail | the events and their copy |
| the cursor | ChartCursor: arrows, Home/End, Escape, pointer events, a polite live readout; renderReadout, onActiveChange, frame={false} for an app's own readout and plot | the readout's content |
| colour-vision and contrast checks | @softure-ai/ui/testing (findColorCollisions, contrastRatio, checkThemeContrast) and checkSeriesPalette from @softure-ai/charts/testing | the colours themselves |
Two differences to carry over deliberately:
- A guard measuring colour distance in CIE76 with a threshold of 12 differs from the defaults here (CIEDE2000
and 10). To keep such numbers while migrating, pass
{ metric: "cie76", minDistance: 12 }. - Labels and copy come from
chartsMessages(English, the only built-in language), so an app's string, or its own translation, moves to themessagesprop, not into the component.
Limitations
- The y domain always contains zero:
[min(0, trough), peak]. A domain away from zero (a stock price from 90 to 110) is not supported yet. LineCharttakes dates on x; numeric x needs the primitives.- Weeks start on Monday (ISO 8601) whatever the locale.
- Ticks below a day (hours, minutes) are not supported.
