Skip to content

⚠ Early release: v0.3.2.sophistry is not ready for production use. Until 1.0, any minor release may break the API; expect missing features and bugs too.Stability and versioning

Chart transitions

Charts redraw from their data, so animating the data animates the chart: a new data value (a track, or a plain prop) reconciles items by key, and with a transition, tweens what changed. Start with BarChartMorph, then the Keyed* examples, which join two datasets by key so items enter, leave and move. SortedBars and BarChartRace animate order; StreamingSeries slides the domain.

Morphing chart data

Charts redraw from their data, so animating the data animates the chart: one blend track mixes two datasets and every bar follows.

BarChartMorph.tsx
4 collapsed lines
import { derive, hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, Text, usePlayer } from "@sophistry/solidjs";
import { BarChart } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const before = [3, 7, 4, 9, 6];
const after = [8, 2, 9, 3, 5];
export default function BarChartMorph() {
const { play, signal } = usePlayer();
const blend = track(0);
// The chart binds this derived array, so every change to `blend` redraws the bars.
const data = derive(blend, (t) =>
before.map((v, i) => v + (after[i] - v) * t),
);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(
sequence(
hold(0.8),
valueTo(blend, 1, { duration: 1.5 }),
hold(0.8),
valueTo(blend, 0, { duration: 1.5 }),
),
);
}
})();
});
return (
<Stage units={10}>
<BarChart
data={data}
label={(_, i) => "abcde"[i]}
yRange={[0, 10, 2]}
width={8}
height={4}
/>
<Text text="same chart, new data" y={2.5} fontSize={0.4} />
</Stage>
);
}

Line morph in data space

Each line morphs on one shared x grid, so values move vertically and a line that gains data grows at its end; the old forecast fades out, the new series sweeps in, and the axes rescale with them.

LineMorph.tsx
4 collapsed lines
import { hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Chart, LinePlot } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const wave = (n: number, f: (x: number) => number) =>
Array.from({ length: n }, (_, x) => f(x));
const rows = (name: string, ys: number[]) =>
ys.map((y, month) => ({ name, month, y }));
const before = [
...rows(
"north",
wave(7, (x) => 20 + 6 * Math.sin(x / 1.6)),
),
...rows(
"south",
wave(7, (x) => 12 + x * 1.5),
),
...rows(
"forecast",
wave(7, (x) => 16 + x),
),
];
const after = [
...rows(
"north",
wave(11, (x) => 24 + 10 * Math.sin(x / 1.8)),
),
...rows(
"south",
wave(11, (x) => 10 + x * 2.6),
),
...rows(
"east",
wave(11, (x) => 30 - x * 1.4),
),
];
export default function LineMorph() {
const { play, signal } = usePlayer();
const blend = track(0);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(
sequence(
hold(0.6),
valueTo(blend, 1, { duration: 3, easing: "linear" }),
hold(0.8),
valueTo(blend, 0, { duration: 3, easing: "linear" }),
),
);
}
})();
});
return (
<Stage units={12}>
<Chart width={10} height={4.4} clip grid="y" xLabel="month">
<LinePlot
data={before}
to={after}
blend={blend}
x="month"
y="y"
series="name"
strokeWidth={0.05}
/>
</Chart>
</Stage>
);
}

Stacked area morph

Each end is stacked on its own and every band's two edges morph together on one shared x grid, so each frame is still a stack: a leaving band shrinks and the bands above settle onto it, a new one grows in its place.

StackMorph.tsx
4 collapsed lines
import { hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Chart, StackPlot } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const rows = (source: string, f: (week: number) => number, weeks = 9) =>
Array.from({ length: weeks }, (_, week) => ({ source, week, gw: f(week) }));
const before = [
...rows("coal", (w) => 14 - w * 0.8),
...rows("gas", (w) => 10 + 2 * Math.sin(w / 1.5)),
...rows("wind", (w) => 4 + w * 0.6),
];
const after = [
...rows("gas", (w) => 8 + 1.5 * Math.cos(w / 2), 12),
...rows("wind", (w) => 7 + w * 0.9, 12),
...rows("solar", (w) => 3 + 4 * Math.sin(w / 3.5), 12),
];
export default function StackMorph() {
const { play, signal } = usePlayer();
const blend = track(0);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(
sequence(
hold(0.6),
valueTo(blend, 1, { duration: 3, easing: "linear" }),
hold(0.8),
valueTo(blend, 0, { duration: 3, easing: "linear" }),
),
);
}
})();
});
return (
<Stage units={12}>
<Chart
width={10}
height={4.4}
xLabel="week"
yLabel="GW"
// Weeks 0–11 fit 0–11, not 0–15: the axis stops at the data
// rather than running most of a tick step past it.
xAxis={{ nice: "tight" }}
color={{ domain: ["coal", "gas", "wind", "solar"] }}
>
<StackPlot
data={before}
to={after}
blend={blend}
x="week"
y="gw"
series="source"
/>
</Chart>
</Stage>
);
}

Bars into a scatter

One blend track turns a bar chart into a scatter plot: the bars shrink away, the category axis cross-fades into a number line, and the dots on the bars' tops slide to their new places in scene units while the y axis glides to the new axis's zero. Played forward, then back.

BarsToScatter.tsx
4 collapsed lines
import { hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Bars, Chart, Scatter } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const countries = [
{ name: "Chile", life: 79, growth: 2.4 },
{ name: "Ghana", life: 64, growth: 5.1 },
{ name: "India", life: 70, growth: 6.8 },
{ name: "Italy", life: 83, growth: -1.6 },
{ name: "Peru", life: 76, growth: 3.3 },
];
// The dots sit on the bars' tops (slot `i` is centred at `i + 0.5`), then
// on a number line of GDP growth (%), which spans 0.
const onBars = countries.map((c, i) => ({
name: c.name,
x: i + 0.5,
y: c.life,
}));
const spread = countries.map((c) => ({ name: c.name, x: c.growth, y: c.life }));
export default function BarsToScatter() {
const { play, signal } = usePlayer();
const blend = track(0);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(
sequence(
hold(0.8),
valueTo(blend, 1, { duration: 2.6, easing: "linear" }),
hold(1.2),
valueTo(blend, 0, { duration: 2.6, easing: "linear" }),
),
);
}
})();
});
return (
<Stage units={12}>
<Chart width={9.4} height={4.4} yLabel="life expectancy">
<Bars
data={countries}
to={[]}
blend={blend}
key="name"
category="name"
value="life"
/>
<Scatter
data={onBars}
to={spread}
blend={blend}
key="name"
x="x"
y="y"
radius={0.13}
/>
</Chart>
</Stage>
);
}

Animated axis rescale

One track drives the y domain and the data together, shared by the axis, the grid and the marks: the values grow tenfold as the axis rescales under them, so the points barely move while the numbers change. Ticks on both ends slide, the rest cross-fade, and labels that would crowd fade out as they close up.

AxisRescale.tsx
4 collapsed lines
import { derive, hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Chart, LinePlot, Scatter } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const before = [2, 6, 4, 8, 5];
const after = [20, 65, 35, 90, 55];
export default function AxisRescale() {
const { play, signal } = usePlayer();
const blend = track(0);
// Data and domain on one track: they move together.
const data = derive(blend, (t) =>
before.map((v, i) => v + (after[i] - v) * t),
);
const yRange = derive(blend, (t) => ({
from: [0, 10] as [number, number],
to: [0, 100] as [number, number],
t,
}));
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(
sequence(
hold(0.6),
valueTo(blend, 1, { duration: 1.6 }),
hold(0.6),
valueTo(blend, 0, { duration: 1.6 }),
),
);
}
})();
});
return (
<Stage units={12}>
<Chart
width={9}
height={4.5}
clip
grid="y"
xRange={[0, 4, 1]}
yRange={yRange}
>
<LinePlot data={data} />
<Scatter data={data} x={(_, i) => i} y={(d) => d} radius={0.08} />
</Chart>
</Stage>
);
}

Keyed scatter update

One blend track joins two datasets by key: Lima shrinks away where it stood, the fitted y axis grows, matched cities move in data space, then Oslo grows in. Played forward, then scrubbed back.

KeyedScatter.tsx
5 collapsed lines
import { okabeIto } from "@sophistry/charts";
import { hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Chart, Scatter } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const before = [
{ city: "Accra", region: "Africa", income: 2.1, life: 61 },
{ city: "Dhaka", region: "Asia", income: 1.6, life: 70 },
{ city: "Hanoi", region: "Asia", income: 3.4, life: 72 },
{ city: "Lima", region: "Americas", income: 6.6, life: 74 },
{ city: "Nairobi", region: "Africa", income: 2.0, life: 63 },
];
const after = [
{ city: "Accra", region: "Africa", income: 4.4, life: 66 },
{ city: "Dhaka", region: "Asia", income: 2.7, life: 73 },
{ city: "Hanoi", region: "Asia", income: 8.3, life: 76 },
{ city: "Nairobi", region: "Africa", income: 3.1, life: 67 },
{ city: "Oslo", region: "Europe", income: 9.5, life: 83 },
];
export default function KeyedScatter() {
const { play, signal } = usePlayer();
const blend = track(0);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(
sequence(
hold(0.6),
// Staged transitions ease each stage themselves: drive them linearly.
valueTo(blend, 1, { duration: 2.4, easing: "linear" }),
hold(0.8),
valueTo(blend, 0, { duration: 2.4, easing: "linear" }),
),
);
}
})();
});
return (
<Stage units={12}>
<Chart
width={10}
height={4.4}
clip
grid
xLabel="income"
yLabel="life"
color={{
range: okabeIto,
domain: ["Africa", "Asia", "Americas", "Europe"],
}}
>
<Scatter
data={before}
to={after}
blend={blend}
key="city"
x="income"
y="life"
series="region"
radius={0.14}
/>
</Chart>
</Stage>
);
}

Keyed bubbles with a staggered entrance

Bubbles sized by population through the `size` channel (area ∝ value), keyed by the frame's size legend, blend to a later year by key: Lima shrinks away, the others move and resize, then three new cities grow in one after another, left to right (`enterStagger`). Only the entrance is staggered; the moves stay together, so they are easy to follow.

KeyedBubbles.tsx
5 collapsed lines
import { okabeIto } from "@sophistry/charts";
import { hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Chart, Scatter } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const before = [
{ city: "Accra", region: "Africa", income: 2.1, life: 61, pop: 2.5 },
{ city: "Dhaka", region: "Asia", income: 1.6, life: 70, pop: 10 },
{ city: "Hanoi", region: "Asia", income: 3.4, life: 72, pop: 4 },
{ city: "Lima", region: "Americas", income: 6.6, life: 74, pop: 9 },
{ city: "Nairobi", region: "Africa", income: 2.0, life: 63, pop: 3.5 },
];
const after = [
{ city: "Accra", region: "Africa", income: 4.4, life: 66, pop: 4 },
{ city: "Dhaka", region: "Asia", income: 2.7, life: 73, pop: 16 },
{ city: "Hanoi", region: "Asia", income: 8.3, life: 76, pop: 6 },
{ city: "Nairobi", region: "Africa", income: 3.1, life: 67, pop: 5 },
{ city: "Oslo", region: "Europe", income: 9.5, life: 83, pop: 1 },
{ city: "Quito", region: "Americas", income: 6.1, life: 77, pop: 2.8 },
{ city: "Lagos", region: "Africa", income: 5.2, life: 62, pop: 15 },
];
export default function KeyedBubbles() {
const { play, signal } = usePlayer();
const blend = track(0);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(
sequence(
hold(0.6),
// Staged transitions ease each stage themselves: drive them linearly.
valueTo(blend, 1, { duration: 3, easing: "linear" }),
hold(0.8),
valueTo(blend, 0, { duration: 3, easing: "linear" }),
),
);
}
})();
});
return (
<Stage units={12}>
<Chart
width={10}
height={4.4}
clip
grid
xLabel="income"
yLabel="life"
color={{
range: okabeIto,
domain: ["Africa", "Asia", "Americas", "Europe"],
}}
// A size key for `size`, under the legend: two reference circles.
sizeLegend={{ title: "pop (M)", values: [4, 16] }}
>
<Scatter
data={before}
to={after}
blend={blend}
key="city"
x="income"
y="life"
series="region"
size="pop"
sizeRange={[0, 0.42]}
sizeMaxValue={16}
enterStagger={0.6}
fillOpacity={0.85}
/>
</Chart>
</Stage>
);
}

Keyed beeswarm update

Week 1 to week 6 of a trial on one blend track: participants who drop out shrink away, everyone else slides from their place in the first swarm to their place in the second (both ends packed), then a new arm's band opens and its dots grow in.

KeyedBeeswarm.tsx
5 collapsed lines
import { okabeIto } from "@sophistry/charts";
import { hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Beeswarm, Chart } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
/** A participant's own spread around their arm's mean, the same every week. */
const spread = (id: number) => Math.sin(id * 12.9898) * 14;
const effect: Record<string, number> = { placebo: -2, "low dose": -10 };
const week1 = Array.from({ length: 40 }, (_, id) => ({
id,
arm: id % 2 ? "low dose" : "placebo",
score: 50 + spread(id),
}));
const week6 = [
// Every seventh participant drops out; the rest move by their arm's effect.
...week1
.filter((d) => d.id % 7 !== 3)
.map((d) => ({ ...d, score: d.score + effect[d.arm] })),
...Array.from({ length: 16 }, (_, k) => ({
id: 100 + k,
arm: "high dose",
score: 34 + spread(100 + k),
})),
];
export default function KeyedBeeswarm() {
const { play } = usePlayer();
const blend = track(0);
onSettled(() => {
// Staged transitions ease each stage themselves: drive them linearly.
void play(
sequence(hold(0.6), valueTo(blend, 1, { duration: 3, easing: "linear" })),
);
});
return (
<Stage units={12}>
<Chart
width={10}
height={4.4}
grid="y"
// Pad the fitted range so the lowest dots clear the axis line.
yAxis={{ margin: 0.05 }}
yLabel="score"
legend={false}
color={{
range: okabeIto,
domain: ["placebo", "low dose", "high dose"],
}}
>
<Beeswarm
data={week1}
to={week6}
blend={blend}
key="id"
category="arm"
value="score"
series="arm"
radius={0.09}
/>
</Chart>
</Stage>
);
}

Keyed box plot update

One blend track joins two years of trial arms by name: placebo closes into its median, the fitted axis grows, the matched arms' quartiles and whiskers move in data units, then a new arm opens. Played forward, then scrubbed back.

KeyedBoxes.tsx
5 collapsed lines
import { okabeIto } from "@sophistry/charts";
import { hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { BoxPlot } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
// A deterministic "uniform" in [0, 1): the fractional part of a sine hash.
const unit = (n: number) => {
const v = Math.sin(n * 12.9898) * 43758.5453;
return v - Math.floor(v);
};
// Deterministic, roughly bell-shaped samples: sums of four such uniforms.
const sample = (seed: number, center: number, spread: number, n = 40) =>
Array.from({ length: n }, (_, i) => {
let sum = 0;
for (let k = 1; k <= 4; k++) sum += unit((i + 1) * 4 + k + seed * 997);
return center + (sum - 2) * spread;
});
const before = [
{ arm: "placebo", scores: sample(1, 4, 2) },
{ arm: "dose A", scores: [...sample(2, 6, 1.5), 11] },
{ arm: "dose B", scores: sample(3, 5, 2.5) },
];
const after = [
{ arm: "dose A", scores: sample(4, 8, 1.5) },
{ arm: "dose B", scores: [...sample(5, 9, 2), 15.5] },
{ arm: "dose C", scores: sample(6, 11, 2) },
];
/** One colour per arm, so an arm keeps its colour as it changes place. */
const colors: Record<string, string> = {
placebo: okabeIto[7],
"dose A": okabeIto[0],
"dose B": okabeIto[2],
"dose C": okabeIto[5],
};
const armColor = (d: { arm: string }) => colors[d.arm];
export default function KeyedBoxes() {
const { play, signal } = usePlayer();
const blend = track(0);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(
sequence(
hold(0.6),
// Staged transitions ease each stage themselves: drive them linearly.
valueTo(blend, 1, { duration: 2.4, easing: "linear" }),
hold(0.8),
valueTo(blend, 0, { duration: 2.4, easing: "linear" }),
),
);
}
})();
});
return (
<Stage units={12}>
<BoxPlot
data={before}
to={after}
blend={blend}
values="scores"
label="arm"
color={armColor}
width={8}
height={4.4}
/>
</Stage>
);
}

Keyed pie morph

One blend track joins two years of browser share by name: IE's slice closes and its neighbours take the room, the shares change in data space, then Edge opens where the new year has it. Every frame is a whole pie.

KeyedPie.tsx
4 collapsed lines
import { hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { PieChart } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const before = [
{ name: "Chrome", share: 45 },
{ name: "IE", share: 25 },
{ name: "Firefox", share: 20 },
{ name: "Safari", share: 10 },
];
const after = [
{ name: "Chrome", share: 64 },
{ name: "Edge", share: 5 },
{ name: "Firefox", share: 8 },
{ name: "Safari", share: 19 },
];
export default function KeyedPie() {
const { play, signal } = usePlayer();
const blend = track(0);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(
sequence(
hold(0.6),
// Staged transitions ease each stage themselves: drive them linearly.
valueTo(blend, 1, { duration: 2.4, easing: "linear" }),
hold(0.8),
valueTo(blend, 0, { duration: 2.4, easing: "linear" }),
),
);
}
})();
});
return (
<Stage units={11}>
<PieChart
data={before}
to={after}
blend={blend}
value="share"
label="name"
radius={2.6}
innerRadius={1.1}
labelFontSize={0.26}
/>
</Stage>
);
}

Radar with a changing axis set

Two phones rated on five axes, then on a new set of five. Axes are matched by name: Weight and Price fold away, every polygon's corner on them flattening into its edge, the shared axes turn to their new places, then Camera and Repair unfold. The polygons blend axis by axis on the same track.

RadarAxes.tsx
4 collapsed lines
import { draw, hold, sequence, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { RadarChart } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const axes = ["Battery", "Screen", "Weight", "Speed", "Price"];
const nextAxes = ["Battery", "Camera", "Screen", "Speed", "Repair"];
const ratings = [
{ phone: "Aster", v: [8, 7, 6, 9, 4] },
{ phone: "Birch", v: [6, 9, 8, 6, 7] },
];
const nextRatings = [
{ phone: "Aster", v: [7, 9, 8, 9, 3] },
{ phone: "Birch", v: [9, 6, 9, 7, 8] },
];
export default function RadarAxes() {
const { play, signal } = usePlayer();
const progress = track(0);
const blend = track(0);
onSettled(() => {
void (async () => {
await play(draw(progress, { duration: 1.5 }));
while (!signal.aborted) {
await play(
sequence(
hold(1),
// Staged transitions ease each stage themselves: drive them linearly.
valueTo(blend, 1, { duration: 1.8, easing: "linear" }),
hold(1.2),
valueTo(blend, 0, { duration: 1.8, easing: "linear" }),
),
);
}
})();
});
return (
<Stage units={10}>
<RadarChart
data={ratings}
to={nextRatings}
blend={blend}
axisValues="v"
key="phone"
axisLabels={axes}
toAxisLabels={nextAxes}
maxValue={10}
radius={2}
progress={progress}
/>
</Stage>
);
}

Sorting bars with slots

A slots model owns the order; the chart's band axis reads each category's slot through `slot`, so bars and their names slide together. Each sort is one seekable `permute`, staggered.

SortedBars.tsx
4 collapsed lines
import { createSlots, derive, hold, sequence } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Bars, Chart } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const fruit = [
{ name: "fig", sold: 4 },
{ name: "kiwi", sold: 9 },
{ name: "lime", sold: 2 },
{ name: "pear", sold: 7 },
{ name: "plum", sold: 5 },
{ name: "yuzu", sold: 8 },
];
const names = fruit.map((d) => d.name);
const bySales = [...fruit].sort((a, b) => b.sold - a.sold).map((d) => d.name);
export default function SortedBars() {
const { play, signal } = usePlayer();
// Slot i laid out at x = i: the chart turns slots into band positions.
const slots = createSlots(names, {
layout: (i) => ({ x: i, y: 0 }),
duration: 0.7,
lag: 0.08,
});
// The axis rebuilds whenever any slot position moves.
const xAxis = derive(
names.flatMap((name) => slots.item(name)?.position ?? []),
() => ({
slot: (key: string | number) =>
slots.item(String(key))?.position.peek().x,
}),
);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(sequence(hold(0.8), slots.plan().permute(bySales)));
await play(sequence(hold(1.2), slots.plan().permute(names)));
}
})();
});
return (
<Stage units={10}>
<Chart width={8} height={4} yLabel="sold" xAxis={xAxis}>
<Bars data={fruit} category="name" value="sold" labels />
</Chart>
</Stage>
);
}

Bar chart race

Values interpolate between yearly keyframes, ranks come from the interpolated values, and each overtake slides on its own short ease. The category axis follows the ranks through its `slot`, so names ride with their bars, and only the top five are shown: whoever drops out slides under the last place and out of view.

BarChartRace.tsx
5 collapsed lines
import { rankRace } from "@sophistry/charts";
import { derive, track, valueTo } from "@sophistry/core";
import { Stage, Text, usePlayer } from "@sophistry/solidjs";
import { Bars, Chart } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const NAMES = ["Ada", "Basil", "Cleo", "Dara", "Esme", "Farid", "Greta"];
const YEARS = Array.from({ length: 11 }, (_, k) => 2010 + k);
const rows = NAMES.flatMap((name, i) =>
YEARS.map((year, k) => ({
name,
year,
score: Math.round(
20 + (i % 3) * 6 + k * (3 + i) + 9 * Math.sin(k * 0.8 + i * 1.7),
),
})),
);
const race = rankRace(rows, {
key: "name",
time: "year",
value: "score",
swap: 0.35,
top: 5,
});
const MAX = Math.max(...rows.map((d) => d.score));
export default function BarChartRace() {
const { play } = usePlayer();
const year = track(YEARS[0]);
onSettled(() => {
// Playback through time is linear; the swaps ease on their own.
void play(valueTo(year, 2020.4, { duration: 10.4, easing: "linear" }));
});
// Five slots on the axis; entries past them slide out under the last.
// They come and go, so the colours are pinned to every name below.
const view = derive(year, (time) => race.view(time));
const yAxis = derive(view, (frame) => ({
categories: frame.categories,
slot: frame.slot,
}));
const entries = derive(view, (frame) => frame.entries);
const caption = derive(year, (time) =>
String(Math.min(2020, Math.floor(time))),
);
return (
<Stage units={12}>
<Chart
width={9}
height={4.6}
center={{ x: -0.4, y: -0.2 }}
clip
grid="x"
legend={false}
color={{ domain: race.keys }}
xRange={[0, Math.ceil(MAX / 20) * 20]}
yAxis={yAxis}
>
<Bars
data={entries}
category="key"
value="value"
key="key"
series="key"
orientation="horizontal"
labels={(d) => d.value.toFixed(0)}
/>
</Chart>
<Text text={caption} x={3.6} y={-1.9} fontSize={0.9} opacity={0.5} />
</Stage>
);
}

Animated scatter with trails

Time as an encoding, Gapminder-style: one marker per country at the moment `at`, sized by population (area ∝ value, the `size` channel) and interpolated between decades, with a trail of the path it took. The axes fit every decade, so they hold still while time plays.

AnimatedScatterTrails.tsx
4 collapsed lines
import { derive, track, valueTo } from "@sophistry/core";
import { Stage, Text, usePlayer } from "@sophistry/solidjs";
import { Chart, Scatter } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const DECADES = [1960, 1970, 1980, 1990, 2000, 2010, 2020];
const COUNTRIES = [
{ name: "A", region: "Asia", income: 600, growth: 1.42, life: 42, pop: 9 },
{ name: "B", region: "Asia", income: 900, growth: 1.25, life: 48, pop: 7 },
{ name: "C", region: "Africa", income: 1100, growth: 1.1, life: 41, pop: 4 },
{ name: "D", region: "Africa", income: 450, growth: 1.12, life: 36, pop: 3 },
{ name: "E", region: "Europe", income: 9000, growth: 1.22, life: 69, pop: 2 },
{
name: "F",
region: "Americas",
income: 4000,
growth: 1.16,
life: 58,
pop: 5,
},
];
const rows = COUNTRIES.flatMap((c) =>
DECADES.map((year, k) => ({
country: c.name,
region: c.region,
year,
income: Math.round(c.income * c.growth ** k),
life: Math.min(
84,
c.life + k * (5.5 - c.life / 20) + 2 * Math.sin(k + c.pop),
),
pop: c.pop * (1 + 0.25 * k),
})),
);
export default function AnimatedScatterTrails() {
const { play } = usePlayer();
const year = track(DECADES[0]);
onSettled(() => {
void play(valueTo(year, 2020, { duration: 8, easing: "linear" }));
});
return (
<Stage units={12}>
<Chart
width={10}
height={4.6}
center={{ x: 0, y: -0.2 }}
grid
xAxis={{ scale: "log", tickFormat: "$,.0f" }}
xLabel="income"
yLabel="life"
>
<Scatter
data={rows}
x="income"
y="life"
key="country"
series="region"
time="year"
at={year}
size="pop"
sizeRange={[0.06, 0.32]}
trail
fillOpacity={0.85}
/>
</Chart>
<Text
text={derive(year, (time) => String(Math.floor(time)))}
x={1.9}
y={-1.8}
fontSize={0.9}
opacity={0.45}
/>
</Stage>
);
}

Streaming series

New samples arrive off-screen right and the x domain slides left under a clip; the leaving tick fades out. Everything is a function of one time track, so it scrubs.

StreamingSeries.tsx
4 collapsed lines
import { derive, track, valueTo } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Chart, LinePlot } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const WINDOW = 10;
/** The same sample at the same x however the timeline is scrubbed. */
const sample = (x: number) => 5 + 2 * Math.sin(x * 0.9) + Math.sin(x * 2.3);
export default function StreamingSeries() {
const { play } = usePlayer();
const time = track(0);
onSettled(() => {
void play(valueTo(time, 40, { duration: 40, easing: "linear" }));
});
// Whole steps change the data; the fraction slides the x domain.
const step = derive(time, (t) => Math.floor(t));
// One sample past the right edge, so it slides in rather than popping.
const data = derive(step, (step) =>
Array.from({ length: step + WINDOW + 2 }, (_, x) => ({
x,
y: sample(x),
})),
);
const xRange = derive(time, (t) => {
const step = Math.floor(t);
return {
from: [step, step + WINDOW] as [number, number],
to: [step + 1, step + WINDOW + 1] as [number, number],
t: t - step,
};
});
return (
<Stage units={12}>
<Chart
width={9}
height={4.5}
clip
grid="y"
yRange={[0, 10]}
xRange={xRange}
>
<LinePlot data={data} x="x" y="y" />
</Chart>
</Stage>
);
}