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

Cameras, zoom and drill-down

The stage’s units and center accept tracks, so a camera can be animated, dragged or zoomed, and <PanZoom> writes them from wheel, drag and pinch. Start with PanZoomPlane and CameraFlight; the chart examples zoom a plot’s domain, and the drill-down examples zoom into a hierarchy.

Pan and zoom

Scroll to zoom about the cursor and drag empty space to pan. PanZoom writes the same units and center tracks that the Stage reads.

PanZoomPlane.tsx
10 collapsed lines
import { track, trackPoint } from "@sophistry/core";
import {
Circle,
Dot,
NumberPlane,
PanZoom,
Stage,
Text,
useTheme,
} from "@sophistry/solidjs";
export default function PanZoomPlane() {
const theme = useTheme();
const palette = () => theme().palette;
const units = track(14);
const center = trackPoint(0, 0);
return (
<Stage units={units} center={center}>
<PanZoom units={units} center={center} minUnits={4} maxUnits={40} />
<NumberPlane xRange={[-8, 8]} yRange={[-5, 5]} width={14} height={8.75} />
<Circle
center={{ x: 3, y: 1 }}
radius={1.2}
fill={palette().series[0]}
fillOpacity={0.4}
/>
<Dot center={{ x: -3, y: -1.5 }} color={palette().series[1]} />
<Text
text="scroll to zoom, drag to pan"
x={-3.5}
y={3}
color={palette().muted}
/>
</Stage>
);
}

Animating the camera

A stage's units and center are ordinary tracks, so moveTo and valueTo fly the camera between landmarks. Nothing is rebuilt: the same scene repaints through a new view.

CameraFlight.tsx
18 collapsed lines
import {
hold,
moveTo,
parallel,
sequence,
track,
trackPoint,
valueTo,
} from "@sophistry/core";
import {
Dot,
NumberPlane,
Stage,
Text,
usePlayer,
useTheme,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const LANDMARKS = [
{ name: "A", at: { x: -6, y: 3 } },
{ name: "B", at: { x: 5, y: -2 } },
{ name: "C", at: { x: 1, y: 4 } },
];
export default function CameraFlight() {
const { play, signal } = usePlayer();
const theme = useTheme();
const palette = () => theme().palette;
const units = track(24);
const center = trackPoint(0, 0);
// Zoom in on a landmark, hold, then pull back out to the whole plane.
const visit = (at: { x: number; y: number }) =>
sequence(
parallel(
moveTo(center, at, { duration: 1.2 }),
valueTo(units, 6, { duration: 1.2 }),
),
hold(0.8),
parallel(
moveTo(center, { x: 0, y: 0 }, { duration: 1.2 }),
valueTo(units, 24, { duration: 1.2 }),
),
hold(0.4),
);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
for (const landmark of LANDMARKS) await play(visit(landmark.at));
}
})();
});
return (
<Stage units={units} center={center}>
<NumberPlane xRange={[-12, 12]} yRange={[-8, 8]} width={24} height={16} />
{LANDMARKS.map((landmark, i) => (
<>
<Dot center={landmark.at} radius={0.3} color={palette().series[i]} />
<Text
text={landmark.name}
x={landmark.at.x}
y={landmark.at.y + 0.9}
color={palette().muted}
/>
</>
))}
</Stage>
);
}

Picture-in-picture

One scene seen through two cameras. Drag the dot in the main view and the magnified inset follows it.

PictureInPictureViews.tsx
9 collapsed lines
import { draggable, toBounds, trackPoint } from "@sophistry/core";
import {
Dot,
MultiStage,
NumberPlane,
PictureInPicture,
Text,
useTheme,
} from "@sophistry/solidjs";
export default function PictureInPictureViews() {
const theme = useTheme();
const palette = () => theme().palette;
const dot = trackPoint(-1, 0.5);
const box = { minX: -5, maxX: 5, minY: -2.5, maxY: 2.5 };
const handle = draggable(dot, { constrain: toBounds(box, { half: 0.2 }) });
return (
<MultiStage units={10}>
<NumberPlane xRange={[-6, 6]} yRange={[-4, 4]} width={12} height={8} />
<Dot center={dot} radius={0.2} label="dot" {...handle} />
<Text
text="drag the dot"
x={3}
y={-2.4}
color={palette().muted}
fontSize={0.28}
/>
<PictureInPicture
main={{}}
inset={{ units: 3, center: dot, border: palette().accent }}
corner="top-left"
/>
</MultiStage>
);
}

Zoom a chart with the wheel, a pinch or the keys

Scroll over the top chart to zoom about the cursor, drag it to pan, pinch on a touch screen, or Tab to it and use + and -, the arrows, and 0 to reset. The zoom writes two tracks and the overview's brush reads the same two, so it follows the zoom, and dragging the brush moves it.

ChartZoomPan.tsx
16 collapsed lines
import {
derive,
draw,
parallel,
sequence,
track,
valueTo,
} from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import {
Chart,
ChartZoom,
LinePlot,
RangeBrush,
} from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const last = 239;
const signal = Array.from({ length: last + 1 }, (_, t) => ({
t,
v: Math.sin(t / 9) + 0.4 * Math.sin(t / 2.7) + t / 90,
}));
export default function ChartZoomPan() {
const player = usePlayer();
const shown = track(0);
const from = track(0);
const to = track(last);
onSettled(() => {
void player.play(
sequence(
draw(shown, { duration: 1.5 }),
parallel(
valueTo(from, 70, { duration: 1.6 }),
valueTo(to, 150, { duration: 1.6 }),
),
),
);
});
return (
<Stage units={12}>
<Chart
width={9.5}
height={3.6}
center={{ x: 0.2, y: 1.0 }}
xRange={derive([from, to], (start, end) => [start, end])}
xAxis={{ position: "bottom" }}
yLabel="signal"
clip
progress={shown}
>
<ChartZoom x={[from, to]} bounds={[0, last]} minSpan={10} />
<LinePlot data={signal} x="t" y="v" />
</Chart>
<Chart
width={9.5}
height={0.9}
center={{ x: 0.2, y: -2.35 }}
yAxis={{ visible: false }}
progress={shown}
>
<LinePlot data={signal} x="t" y="v" />
<RangeBrush from={from} to={to} minSpan={10} />
</Chart>
</Stage>
);
}

Drag a box to zoom

Drag a rectangle over the plot to zoom into it (a thin one zooms one axis), hold Shift to pan instead, Backspace to step back out, and 0 or a double tap to reset; Escape mid-drag cancels. The toolbar in the corner switches between panning and box zoom and resets, and each button is a tab stop. The x axis is logarithmic: zooms and the bounds work in decades. The wheel over the plot zooms the chart, and anywhere else it zooms the stage's own PanZoom camera.

BoxZoom.tsx
12 collapsed lines
import {
derive,
draw,
parallel,
sequence,
track,
trackPoint,
valueTo,
} from "@sophistry/core";
import { PanZoom, Stage, usePlayer } from "@sophistry/solidjs";
import { Chart, ChartZoom, Scatter } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
/** A deterministic scatter: income on a log scale against a score. */
const people = Array.from({ length: 260 }, (_, i) => {
const u = ((i * 0.618034) % 1) * 3;
const income = 10 ** (3 + u);
const wobble = Math.sin(i * 12.9898) * 43758.5453;
const noise = wobble - Math.floor(wobble) - 0.5;
return { income, score: 20 + 18 * u + 22 * noise };
});
export default function BoxZoom() {
const player = usePlayer();
const units = track(12);
const center = trackPoint(0, 0);
const shown = track(0);
const x = [track(1e3), track(1e6)] as const;
const y = [track(0), track(100)] as const;
onSettled(() => {
void player.play(
sequence(
draw(shown, { duration: 1.5 }),
parallel(
valueTo(x[0], 1e4, { duration: 1.6 }),
valueTo(x[1], 1e5, { duration: 1.6 }),
valueTo(y[0], 25, { duration: 1.6 }),
valueTo(y[1], 85, { duration: 1.6 }),
),
),
);
});
return (
<Stage units={units} center={center}>
<PanZoom units={units} center={center} minUnits={6} maxUnits={30} />
<Chart
width={8.4}
height={5}
center={{ x: -0.3, y: 0 }}
xRange={derive(x, (start, end) => [start, end])}
yRange={derive(y, (start, end) => [start, end])}
xAxis={{ scale: "log", position: "bottom" }}
yAxis={{ position: "left" }}
xLabel="income"
yLabel="score"
clip
progress={shown}
>
<ChartZoom
x={x}
y={y}
dragMode="box"
toolbar
bounds={{ x: [1e3, 1e6], y: [0, 100] }}
label="income against score"
/>
<Scatter data={people} x="income" y="score" radius={0.05} />
</Chart>
</Stage>
);
}

Zooming a category axis

The top chart is a window onto twelve of thirty-six months. Scroll over it to zoom, drag to pan, or Tab to it and use the arrows and + and -: drags, pinches and keys settle on whole months, and the names leaving the window fade. The overview's brush reads the same two tracks; scroll over the brush to grow or shrink it.

CategoryZoom.tsx
11 collapsed lines
import {
derive,
draw,
parallel,
sequence,
track,
valueTo,
} from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Bars, Chart, ChartZoom, RangeBrush } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const NAMES = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const months = Array.from({ length: 36 }, (_, i) => ({
month: `${2023 + Math.floor(i / 12)}-${String((i % 12) + 1).padStart(2, "0")}`,
units: Math.round(
40 + 18 * Math.sin((i / 12) * 2 * Math.PI - 1.2) + i * 0.9 + ((i * 7) % 5),
),
}));
/** "Jan 24" on a January, else the month. */
const monthName = (key: string | number) => {
const [year, month] = String(key).split("-").map(Number);
const name = NAMES[month - 1];
return month === 1 ? `${name} ${String(year).slice(2)}` : name;
};
export default function CategoryZoom() {
const player = usePlayer();
const shown = track(0);
// Slot edges: the window holds categories from..to (category i spans i..i + 1).
const from = track(0);
const to = track(12);
onSettled(() => {
void player.play(
sequence(
draw(shown, { duration: 1.5 }),
parallel(
valueTo(from, 12, { duration: 1.6 }),
valueTo(to, 24, { duration: 1.6 }),
),
),
);
});
return (
<Stage units={12}>
<Chart
width={10}
height={3.4}
center={{ x: 0.2, y: 1.1 }}
xRange={derive([from, to], (start, end) => [start, end])}
xAxis={{ categoryLabel: (key) => monthName(key) }}
yLabel="units"
clip
progress={shown}
>
<ChartZoom x={[from, to]} label="monthly units" />
<Bars data={months} category="month" value="units" />
</Chart>
<Chart
width={10}
height={0.9}
center={{ x: 0.2, y: -2.35 }}
xAxis={{
categoryLabel: (key, i) => (i % 6 === 0 ? monthName(key) : ""),
}}
yAxis={{ visible: false }}
progress={shown}
>
<Bars data={months} category="month" value="units" />
<RangeBrush from={from} to={to} wheel label="months shown" />
</Chart>
</Stage>
);
}

Tap to drill into a treemap

Tap a tile to zoom into its folder, tap a crumb above to zoom back out; Tab and Enter do the same. The zoom is a tween of the chart's focus, so it is exactly the scrubbable drill-down transition, and under reduced motion it dissolves instead.

TreemapDrillDown.tsx
4 collapsed lines
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Treemap } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
type Item = { name: string; kb?: number; children?: Item[] };
const files = (...entries: [string, number][]) =>
entries.map(([name, kb]) => ({ name, kb }));
const repo: Item = {
name: "repo",
children: [
{
name: "src",
children: [
{
name: "charts",
children: files(["bars", 40], ["pie", 22], ["tree", 30]),
},
{ name: "scene", children: files(["shape", 35], ["text", 18]) },
{ name: "anim", kb: 26 },
],
},
{ name: "docs", children: files(["guides", 30], ["api", 24]) },
{ name: "tests", children: files(["unit", 28], ["e2e", 14]) },
],
};
export default function TreemapDrillDown() {
const player = usePlayer();
const progress = track(0);
onSettled(() => {
void player.play(draw(progress, { duration: 1.5 }));
});
return (
<Stage units={10}>
<Treemap
data={repo}
value="kb"
label="name"
width={8}
height={3.9}
center={{ x: 0, y: -0.4 }}
paddingTop={0.3}
labelFontSize={0.2}
progress={progress}
drill
breadcrumbs
tooltip
/>
</Stage>
);
}

Tap to zoom into a tree

Tap a node to spread its subtree over the chart, tap it again (or the back arrow, or a crumb) to zoom out; Tab and Enter do the same. Deep trails collapse their middle into an ellipsis that climbs back up.

TreeDrillDown.tsx
4 collapsed lines
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Tree } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
type Item = { name: string; children?: Item[] };
const leaves = (...names: string[]) => names.map((name) => ({ name }));
const repo: Item = {
name: "repo",
children: [
{
name: "packages",
children: [
{
name: "core",
children: [
{
name: "charts",
children: leaves("bars", "pie", "tree", "facet"),
},
{ name: "scene", children: leaves("shape", "text") },
{ name: "anim", children: leaves("tween", "clock") },
],
},
{ name: "three", children: leaves("mesh", "camera") },
],
},
{ name: "docs", children: leaves("guides", "api", "gallery") },
{ name: "e2e", children: leaves("visual", "bench") },
],
};
export default function TreeDrillDown() {
const player = usePlayer();
const progress = track(0);
onSettled(() => {
void player.play(draw(progress, { duration: 1.5 }));
});
return (
<Stage units={10}>
<Tree
data={repo}
label="name"
width={8}
height={4.2}
center={{ x: 0, y: -0.3 }}
progress={progress}
drill
breadcrumbs={{ back: true, maxWidth: 3.2 }}
tooltip
/>
</Stage>
);
}

Zoomable circle packing

Tap a circle to zoom into it and the background to zoom out, along d3's smooth zoom path. The chart is controlled: createDrillDown holds the focus and tweens it, and the same focus drives the breadcrumb trail, so both scrub together.

ZoomableCirclePack.tsx
3 collapsed lines
import { createDrillDown } from "@sophistry/charts";
import { Stage, useScene } from "@sophistry/solidjs";
import { Breadcrumbs, CirclePack } from "@sophistry/solidjs/charts";
type Item = { name: string; n?: number; children?: Item[] };
const leaves = (...entries: [string, number][]) =>
entries.map(([name, n]) => ({ name, n }));
const life: Item = {
name: "life",
children: [
{
name: "animals",
children: [
{
name: "birds",
children: leaves(["owl", 4], ["wren", 2], ["heron", 3]),
},
{ name: "fish", children: leaves(["cod", 3], ["eel", 2]) },
{
name: "insects",
children: leaves(["bee", 5], ["ant", 6], ["moth", 2]),
},
],
},
{
name: "plants",
children: [
{ name: "trees", children: leaves(["oak", 4], ["fir", 3]) },
{ name: "ferns", n: 3 },
],
},
{ name: "fungi", children: leaves(["yeast", 2], ["mould", 3]) },
],
};
/** Inside the stage, so the zoom runs on its clock and reduced-motion policy. */
function Zoomable() {
const scene = useScene();
const zoom = createDrillDown({
duration: 0.9,
clock: scene.clock,
reducedMotion: scene.reducedMotion,
});
return (
<>
<Breadcrumbs
path={zoom.focus}
root="life"
y={2.45}
align="center"
onSelect={zoom.zoomTo}
/>
<CirclePack
data={life}
value="n"
label="name"
radius={2.15}
center={{ x: 0, y: -0.25 }}
labelFontSize={0.17}
focus={zoom.focus}
onFocusChange={zoom.zoomTo}
tooltip
/>
</>
);
}
export default function ZoomableCirclePack() {
return (
<Stage units={10}>
<Zoomable />
</Stage>
);
}