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

Facets and linked views

One dataset over several panels: facet grids with shared or free scales, pair and joint plots, and brushes whose two tracks drive the charts they are linked to. Start with FacetGrid, then LinkedBrush.

Small multiples with shared scales

One panel per island from a single long-format table. The panels share both scales, so the clusters compare across the row, and only the outer panels write numbers; the species colours come from one scale with one legend. Every frame draws first, then the panels fill left to right.

FacetGrid.tsx
5 collapsed lines
import { okabeIto, scatter } from "@sophistry/charts";
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Facet } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
// Deterministic jitter around each species' centre, so the gallery is stable.
const hash = (n: number) => Math.abs((Math.sin(n * 12.9898) * 43758.5453) % 1);
const centres = {
Adelie: [38.8, 18.3],
Chinstrap: [48.8, 18.4],
Gentoo: [47.5, 15.0],
} as const;
const islands: [string, (keyof typeof centres)[]][] = [
["Biscoe", ["Adelie", "Gentoo"]],
["Dream", ["Adelie", "Chinstrap"]],
["Torgersen", ["Adelie"]],
];
const birds = islands.flatMap(([island, species], k) =>
species.flatMap((name, s) =>
Array.from({ length: 14 }, (_, i) => {
const seed = k * 1000 + s * 100 + i;
return {
island,
species: name,
bill: centres[name][0] + 6 * (hash(seed) - 0.5),
depth: centres[name][1] + 2.4 * (hash(seed + 7) - 0.5),
};
}),
),
);
export default function FacetGrid() {
const { play } = usePlayer();
const shown = track(0);
onSettled(() => {
void play(draw(shown, { duration: 3 }));
});
return (
<Stage units={12}>
<Facet
data={birds}
column="island"
// Everything, titles and legend included, fits the stage.
autosize="contain"
width={11.4}
height={5.8}
xLabel="bill length (mm)"
yLabel="bill depth (mm)"
grid
color={{ domain: Object.keys(centres), range: okabeIto }}
progress={shown}
stagger={0.5}
panel={(panel) => [
scatter.create({
data: panel.data,
x: "bill",
y: "depth",
series: "species",
radius: 0.06,
}),
]}
/>
</Stage>
);
}

A row × column facet grid

One table split two ways: years down the rows, islands across the columns. Each row's year is written once in the right margin and each island once over the top row, the panels share both scales, and one legend runs along the top.

FacetRowCol.tsx
5 collapsed lines
import { okabeIto, scatter } from "@sophistry/charts";
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Facet } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
// Deterministic jitter around each species' centre, so the gallery is stable.
const hash = (n: number) => Math.abs((Math.sin(n * 12.9898) * 43758.5453) % 1);
const centres = {
Adelie: [38.8, 18.3],
Chinstrap: [48.8, 18.4],
Gentoo: [47.5, 15.0],
} as const;
const islands: [string, (keyof typeof centres)[]][] = [
["Biscoe", ["Adelie", "Gentoo"]],
["Dream", ["Adelie", "Chinstrap"]],
["Torgersen", ["Adelie"]],
];
const birds = [2008, 2009].flatMap((year, y) =>
islands.flatMap(([island, species], k) =>
species.flatMap((name, s) =>
Array.from({ length: 9 }, (_, i) => {
const seed = y * 5000 + k * 1000 + s * 100 + i;
return {
year,
island,
species: name,
bill: centres[name][0] + y + 6 * (hash(seed) - 0.5),
depth: centres[name][1] + 2.4 * (hash(seed + 7) - 0.5),
};
}),
),
),
);
export default function FacetRowCol() {
const { play } = usePlayer();
const shown = track(0);
onSettled(() => {
void play(draw(shown, { duration: 2.5 }));
});
return (
<Stage units={12}>
<Facet
data={birds}
row="year"
column="island"
legend="top"
gap={{ y: 0.6 }}
autosize="contain"
width={11.4}
height={6.2}
xLabel="bill length (mm)"
yLabel="bill depth (mm)"
grid
color={{ domain: Object.keys(centres), range: okabeIto }}
progress={shown}
panel={(panel) => [
scatter.create({
data: panel.data,
x: "bill",
y: "depth",
series: "species",
radius: 0.05,
}),
]}
/>
</Stage>
);
}

Facet grid with x shared down each column

scales={{ x: "column" }} gives each travel mode its own distance axis, shared by the rows below it (ggplot's facet_grid with scales = "free_x"), and space="free" sizes each column by its range, so a kilometre is one length across the grid. Time stays shared.

FacetGridScales.tsx
5 collapsed lines
import { scatter } from "@sophistry/charts";
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Facet } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const hash = (n: number) => Math.abs(Math.sin(n * 78.233) * 43758.5453) % 1;
const modes = [
{ mode: "walk", far: 5, speed: 5 },
{ mode: "bike", far: 12, speed: 16 },
{ mode: "bus", far: 20, speed: 22 },
];
const trips = modes.flatMap(({ mode, far, speed }, m) =>
["weekday", "weekend"].flatMap((day, d) =>
Array.from({ length: 14 }, (_, i) => {
const km = 0.5 + (far - 0.5) * hash(i + 31 * m + 7 * d);
return {
mode,
day,
km,
min: (km / speed) * 60 * (d ? 1.2 : 1) + 4 * hash(i + 90),
};
}),
),
);
export default function FacetGridScales() {
const { play } = usePlayer();
const shown = track(0);
onSettled(() => {
void play(draw(shown, { duration: 2 }));
});
return (
<Stage units={12}>
<Facet
data={trips}
row="day"
column="mode"
scales={{ x: "column" }}
space={{ x: "free" }}
width={10.6}
height={5.4}
center={{ x: -0.1, y: -0.1 }}
// The size is the whole grid, numbers and titles included.
autosize="contain"
xLabel="distance (km)"
yLabel="time (min)"
progress={shown}
panel={(panel) => [
scatter.create({ data: panel.data, x: "km", y: "min", radius: 0.05 }),
]}
/>
</Stage>
);
}

Facet panels sized by their categories

Each department has its own teams, so the x axes are independent; space="free" makes each panel as wide as its number of bars, so every bar is the same width and the panels read as one row of bars. The value axis is shared.

FacetFreeSpace.tsx
5 collapsed lines
import { bars } from "@sophistry/charts";
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Facet } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const teams: [string, [string, number][]][] = [
[
"Product",
[
["Web", 30],
["App", 22],
],
],
[
"Sales",
[
["EU", 18],
["US", 26],
["Asia", 14],
["Retail", 9],
["Online", 12],
],
],
[
"Ops",
[
["Infra", 16],
["Help", 10],
["IT", 7],
],
],
];
const budget = teams.flatMap(([department, list]) =>
list.map(([team, k]) => ({ department, team, k })),
);
export default function FacetFreeSpace() {
const { play } = usePlayer();
const shown = track(0);
onSettled(() => {
void play(draw(shown, { duration: 2 }));
});
return (
<Stage units={12}>
<Facet
data={budget}
column="department"
scales={{ x: "independent" }}
space={{ x: "free" }}
gap={{ x: 0.3 }}
autosize="contain"
width={11.4}
height={5.6}
yLabel="budget (k)"
grid="y"
progress={shown}
panel={(panel) => [
bars.create({ data: panel.data, category: "team", value: "k" }),
]}
/>
</Stage>
);
}

Pair plot

Every pair of three measurements against each other, each measurement's density on the diagonal. Panels in a column share x and panels in a row share y, so a flower lines up across the matrix; only the bottom row and left column write numbers and names.

PairPlotMatrix.tsx
5 collapsed lines
import { okabeIto } from "@sophistry/charts";
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { PairPlot } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const hash = (n: number) => Math.abs((Math.sin(n * 78.233) * 43758.5453) % 1);
const species = [
{ name: "setosa", sepal: 5.0, petal: 1.5, width: 0.25 },
{ name: "versicolor", sepal: 5.9, petal: 4.3, width: 1.3 },
{ name: "virginica", sepal: 6.6, petal: 5.5, width: 2.0 },
];
const flowers = species.flatMap((s, k) =>
Array.from({ length: 18 }, (_, i) => {
const seed = k * 100 + i;
const size = hash(seed) - 0.5;
return {
species: s.name,
sepal: s.sepal + 0.9 * size + 0.3 * (hash(seed + 1) - 0.5),
petal: s.petal + 0.8 * size + 0.3 * (hash(seed + 2) - 0.5),
width: s.width + 0.3 * size + 0.15 * (hash(seed + 3) - 0.5),
};
}),
);
export default function PairPlotMatrix() {
const { play } = usePlayer();
const shown = track(0);
onSettled(() => {
void play(draw(shown, { duration: 3 }));
});
return (
<Stage units={12}>
<PairPlot
data={flowers}
variables={["sepal", "petal", "width"]}
variableLabels={{
sepal: "sepal (cm)",
petal: "petal (cm)",
width: "width (cm)",
}}
series="species"
color={{ range: okabeIto }}
// Everything, names and legend included, fits the stage.
autosize="contain"
width={9}
height={6.4}
progress={shown}
/>
</Stage>
);
}

Scatter with marginal densities

A joint plot: two groups in a scatter, with each variable's density along its edge, the right-hand one a vertical density, values up y. The marginals follow the scatter's scales, so the curves line up with the points, and they are normalised together, so each curve's area is its group's share and the larger group stands taller.

JointDensity.tsx
4 collapsed lines
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { JointPlot } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
// Two blobs from a deterministic sine hash.
const hash = (n: number) => (Math.sin(n * 12.9898) * 43758.5453) % 1;
const points = Array.from({ length: 90 }, (_, i) => {
const big = i < 60;
return {
group: big ? "many" : "few",
x: (big ? 3 : 6) + 1.1 * (hash(i + 1) + hash(i + 101)),
y: (big ? 4 : 2) + 0.9 * (hash(i + 201) + hash(i + 301)),
};
});
export default function JointDensity() {
const { play } = usePlayer();
const shown = track(0);
onSettled(() => {
void play(draw(shown, { duration: 2.5 }));
});
return (
<Stage units={12}>
<JointPlot
data={points}
x="x"
y="y"
series="group"
width={8.4}
height={5.4}
center={{ x: 0, y: 0.2 }}
ratio={4}
radius={0.06}
xRange={[0, 9]}
yRange={[0, 7]}
progress={shown}
/>
</Stage>
);
}

Brushing linked charts

An overview with a draggable range brush drives two detail charts linked on x. The brush writes two tracks and the link reads them, so dragging it, using the arrow keys, or animating the tracks (as here) zooms both details together, and scrubbing the timeline replays it.

LinkedBrush.tsx
17 collapsed lines
import {
derive,
draw,
parallel,
sequence,
track,
valueTo,
} from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import {
Chart,
ChartLink,
LinePlot,
RangeBrush,
Rects,
} from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const days = Array.from({ length: 90 }, (_, t) => ({
t,
price: 20 + 3 * Math.sin(t / 7) + Math.sin(t / 2.3) + t * 0.06,
volume: 2 + 1.5 * Math.abs(Math.cos(t / 3.1)) + (t % 7 === 0 ? 1.5 : 0),
}));
export default function LinkedBrush() {
const { play } = usePlayer();
const shown = track(0);
const from = track(10);
const to = track(30);
onSettled(() => {
void play(
sequence(
draw(shown, { duration: 2 }),
parallel(
valueTo(from, 52, { duration: 2.4 }),
valueTo(to, 80, { duration: 2.4 }),
),
),
);
});
return (
<Stage units={12}>
<ChartLink xRange={derive([from, to], (low, high) => [low, high])}>
<Chart
width={9.5}
height={1.6}
center={{ x: 0.2, y: 1.95 }}
yLabel="price"
clip
progress={shown}
>
<LinePlot data={days} x="t" y="price" />
</Chart>
<Chart
width={9.5}
height={1}
center={{ x: 0.2, y: -0.6 }}
yLabel="volume"
clip
progress={shown}
>
<Rects
data={days}
x1={(d) => d.t - 0.35}
x2={(d) => d.t + 0.35}
y1={() => 0}
y2="volume"
color="#89b4fa"
/>
</Chart>
</ChartLink>
<Chart
width={9.5}
height={0.8}
center={{ x: 0.2, y: -2.4 }}
yAxis={{ visible: false }}
progress={shown}
>
<LinePlot data={days} x="t" y="price" />
<RangeBrush from={from} to={to} minSpan={5} />
</Chart>
</Stage>
);
}

Brushing a box on both axes

A two-dimensional brush on the left selects a box of the cloud and the chart on the right zooms to it. Drag the box, an edge or a corner, or Tab to it and use the arrow keys; the four tracks it writes are the right chart's ranges, animated here.

BoxBrush.tsx
11 collapsed lines
import {
derive,
draw,
parallel,
sequence,
track,
valueTo,
} from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Chart, RangeBrush, Scatter } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const hash = (n: number) => Math.abs(Math.sin(n * 12.9898) * 43758.5453) % 1;
const cloud = Array.from({ length: 160 }, (_, i) => {
const a = hash(i + 1) * Math.PI * 2;
const r = 1 + 3 * hash(i + 400);
return { x: 5 + r * Math.cos(a), y: 5 + 0.6 * r * Math.sin(a) + hash(i) };
});
export default function BoxBrush() {
const { play } = usePlayer();
const shown = track(0);
const x = [track(2), track(5)] as const;
const y = [track(4), track(6.5)] as const;
onSettled(() => {
void play(
sequence(
draw(shown, { duration: 1.5 }),
parallel(
valueTo(x[0], 5, { duration: 1.4 }),
valueTo(x[1], 8.5, { duration: 1.4 }),
valueTo(y[0], 3, { duration: 1.4 }),
),
),
);
});
return (
<Stage units={12}>
<Chart
width={4.6}
height={4.6}
center={{ x: -3, y: -0.2 }}
progress={shown}
>
<Scatter data={cloud} x="x" y="y" radius={0.04} />
<RangeBrush x={x} y={y} label="selection" />
</Chart>
<Chart
width={4.6}
height={4.6}
center={{ x: 3.2, y: -0.2 }}
xRange={derive(x, (low, high) => [low, high])}
yRange={derive(y, (low, high) => [low, high])}
clip
progress={shown}
>
<Scatter data={cloud} x="x" y="y" radius={0.07} />
</Chart>
</Stage>
);
}

Brushing categories on a band axis

On a category axis the brush snaps to whole bars, and an arrow key moves it one bar. brushedCategories turns its two tracks into the months it covers, which the chart below breaks down by product.

CategoryBrush.tsx
12 collapsed lines
import { brushedCategories } from "@sophistry/charts";
import {
derive,
draw,
parallel,
sequence,
track,
valueTo,
} from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Bars, Chart, RangeBrush } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const sales = months.flatMap((month, i) =>
["tea", "coffee", "cocoa"].map((product, j) => ({
month,
product,
units:
4 + ((i * (j + 2) + 3 * j) % 7) + (j === 2 ? 6 - Math.abs(i - 6) : 0),
})),
);
const totals = months.map((month) => ({
month,
units: sales
.filter((d) => d.month === month)
.reduce((sum, d) => sum + d.units, 0),
}));
export default function CategoryBrush() {
const { play } = usePlayer();
const shown = track(0);
const from = track(1);
const to = track(4);
onSettled(() => {
void play(
sequence(
draw(shown, { duration: 1.5 }),
parallel(
valueTo(from, 6, { duration: 1.4 }),
valueTo(to, 9, { duration: 1.4 }),
),
),
);
});
// The months the brush covers, and the rows for them, follow its two tracks.
const picked = derive([from, to], (low, high) =>
brushedCategories(months, low, high),
);
return (
<Stage units={12}>
<Chart width={10} height={1.6} center={{ x: 0, y: 2.2 }} progress={shown}>
<Bars data={totals} category="month" value="units" />
<RangeBrush from={from} to={to} label="months" />
</Chart>
<Chart
width={10}
height={2.6}
center={{ x: 0, y: -1.6 }}
xAxis={derive(picked, (categories) => ({ categories }))}
legend="top"
progress={shown}
>
<Bars
data={derive(picked, (categories) =>
sales.filter((d) => categories.includes(d.month)),
)}
category="month"
value="units"
series="product"
layout="dodge"
/>
</Chart>
</Stage>
);
}