| Seats | Hours saved | plan |
|---|---|---|
| 16 | 24 | Team |
| 41 | 39 | Solo |
| 25 | 28 | Solo |
| 21 | 36 | Team |
| 46 | 49 | Solo |
| 30 | 38 | Solo |
| 26 | 49 | Team |
| 51 | 59 | Solo |
| 35 | 48 | Solo |
| 31 | 61 | Team |
| 56 | 69 | Solo |
| 40 | 58 | Solo |
| 36 | 73 | Team |
| 8 | 36 | Solo |
| 45 | 68 | Solo |
| 41 | 56 | Team |
| 13 | 17 | Solo |
| 50 | 49 | Solo |
| 46 | 69 | Team |
| 18 | 27 | Solo |
| 55 | 59 | Solo |
| 51 | 81 | Team |
| 23 | 37 | Solo |
| 7 | 27 | Solo |
| 56 | 93 | Team |
| 28 | 47 | Solo |
| 12 | 37 | Solo |
| 61 | 105 | Team |
| 33 | 57 | Solo |
| 17 | 18 | Solo |
| 66 | 89 | Team |
| 38 | 38 | Solo |
| 22 | 28 | Solo |
| 18 | 35 | Team |
| 43 | 48 | Solo |
| 27 | 38 | Solo |
- 1yLabel
- 2group · plan
- 3xLabel
- height · 260
Schedule of props
| Mark | Prop | Type | Default | Description |
|---|---|---|---|---|
| P1 | data | Datum[] | SAMPLE | One object per point. |
| P2 | x | string | 'seats' | The key holding each point's x value. |
| P3 | y | string | 'hours' | The key holding each point's y value. |
| P4 | group | string | data === SAMPLE ? 'plan' : undefined | The key that puts each point in a group (a series). Leave it out for one group. |
| P5 | config | ChartConfig | data === SAMPLE ? SAMPLE_CONFIG : undefined | Labels (and colours, if you like) for the groups, by group name. |
| P6 | xLabel | string | data === SAMPLE ? 'Seats' : undefined | What the x axis measures, written under it. |
| P7 | yLabel | string | data === SAMPLE ? 'Hours saved' : undefined | What the y axis measures, written above it. |
| P8 | trend | boolean | false | A least-squares line through each group, drawn last. |
| P9 | height | number | 260 | The plot's height in pixels; it takes the full width. |
| P10 | format | Intl.NumberFormatOptions | — | How numbers on the axes are written (Intl.NumberFormat options). |
| P11 | legend | boolean | — | Show the key. By default, when there is more than one group. |
| P12 | accent | string | ACCENT | The first group's colour. |
| P13 | label | string | 'Scatter chart' | What the chart shows, for screen readers (it captions the data table). |
| P14 | trigger | ChartTrigger | 'scroll' | `scroll`: inked as it rises, done at mid-screen. `view`: played once as it comes into view. |
| P15 | duration | number | 1800 | With `view`, how long the chart takes, in ms. |
| P16 | className | string | '' | — |
Notes
- 1.Points plotted left to right as small marks, one shape per series, with an optional fitted trend line drawn last.
- 2.Labels, ticks, the legend and a data table for screen readers are in the server HTML. The strokes are made in the browser at the chart’s measured size, so the hatching keeps its spacing at any width.
- 3.Tied to the scroll by default: the last stroke lands as the drawing reaches the middle of the screen. With
trigger="view"it plays once as it comes into view instead. Reduced motion shows it drawn.
- 4.Detail A, Groups. group splits the points into series, each its own mark: accent circles, then ink squares.
- 5.Usage, as drawn:
import { ChartScatter } from '@/components/scrollfx/chart-scatter'; export function Groups() { return <ChartScatter label="Hours saved against seats, by plan" />; }
- 4.Detail B, With a trend line. trend fits a least-squares line through each group, drawn last, once the points are down.
- 5.Usage, as drawn:
import { ChartScatter } from '@/components/scrollfx/chart-scatter'; export function Trend() { return ( <ChartScatter data={[ { temp: 14, sales: 215 }, { temp: 16, sales: 325 }, { temp: 12, sales: 185 }, { temp: 19, sales: 412 }, { temp: 21, sales: 522 }, { temp: 18, sales: 445 }, { temp: 23, sales: 544 }, { temp: 22, sales: 614 }, { temp: 25, sales: 590 }, { temp: 17, sales: 421 }, { temp: 13, sales: 240 }, { temp: 20, sales: 470 }, { temp: 24, sales: 640 }, { temp: 15, sales: 300 }, { temp: 26, sales: 680 }, { temp: 11, sales: 170 }, ]} x="temp" y="sales" xLabel="Temperature, °C" yLabel="Ice creams sold" trend label="Ice creams sold against the day's temperature, with a fitted trend line" /> ); }
View source · components/scrollfx/chart-scatter.tsx
'use client';
/**
* chart-scatter — a scatter chart a pen plots as you scroll: the axes and
* grid first, then every point as a small mark, left to right (circles in the
* accent for the first group, then squares, triangles, diamonds and crosses in
* ink), then, if you ask for it, a fitted trend line through each group (in
* ink, for a single group).
*
* Built on svg-scroll-draw (MIT): https://github.com/DhruvilChauahan0210/scrollfx
* Needs: react, svg-scroll-draw, lib/chart and lib/plotter (installed with it).
* Styling: Tailwind CSS with shadcn/ui tokens.
*
* The axis labels, the legend and a data table for screen readers are in the
* server HTML; the strokes are made in the browser at the chart's size and
* finish as the chart reaches the middle of the screen. Reduced motion shows
* the chart drawn.
*/
import { useMemo } from 'react';
import 'svg-scroll-draw/no-flash.css';
import {
ACCENT, DataTable, Legend, PHASE, PenLayers, XLabels, YLabels, formatter, gutter, linear, marker, niceTicks, num, seriesOf, toPath, useSize,
type ChartTrigger, type ChartConfig, type Datum, type Mark, type Pen,
} from '@/lib/chart';
export interface ChartScatterProps {
/** One object per point. */
data?: Datum[];
/** The key holding each point's x value. */
x?: string;
/** The key holding each point's y value. */
y?: string;
/** The key that puts each point in a group (a series). Leave it out for one group. */
group?: string;
/** Labels (and colours, if you like) for the groups, by group name. */
config?: ChartConfig;
/** What the x axis measures, written under it. */
xLabel?: string;
/** What the y axis measures, written above it. */
yLabel?: string;
/** A least-squares line through each group, drawn last. */
trend?: boolean;
/** The plot's height in pixels; it takes the full width. */
height?: number;
/** How numbers on the axes are written (Intl.NumberFormat options). */
format?: Intl.NumberFormatOptions;
/** Show the key. By default, when there is more than one group. */
legend?: boolean;
/** The first group's colour. */
accent?: string;
/** What the chart shows, for screen readers (it captions the data table). */
label?: string;
/** `scroll`: inked as it rises, done at mid-screen. `view`: played once as it comes into view. */
trigger?: ChartTrigger;
/** With `view`, how long the chart takes, in ms. */
duration?: number;
className?: string;
}
// Sample points: two loose groups with a rising trend, from integer arithmetic only, so the server and the browser agree.
const SAMPLE: Datum[] = Array.from({ length: 36 }, (_, i) => {
const plan = i % 3 === 0 ? 'team' : 'solo';
const x = 4 + ((i * 37) % 53) + (plan === 'team' ? 12 : 0);
const wobble = ((i * 7919) % 29) - 14;
return { seats: x, hours: Math.round(18 + x * (plan === 'team' ? 1.25 : 0.8) + wobble), plan };
});
const SAMPLE_CONFIG: ChartConfig = { solo: { label: 'Solo' }, team: { label: 'Team' } };
export function ChartScatter({
data = SAMPLE,
x = 'seats',
y = 'hours',
group = data === SAMPLE ? 'plan' : undefined,
config = data === SAMPLE ? SAMPLE_CONFIG : undefined,
xLabel = data === SAMPLE ? 'Seats' : undefined,
yLabel = data === SAMPLE ? 'Hours saved' : undefined,
trend = false,
height = 260,
format,
legend,
accent = ACCENT,
label = 'Scatter chart',
trigger = 'scroll',
duration = 1800,
className = '',
}: ChartScatterProps) {
const names = group ? (config ? Object.keys(config) : [...new Set(data.map((d) => String(d[group])))]) : ['all'];
const series = seriesOf([], '', Object.fromEntries(names.map((g) => [g, { label: config?.[g]?.label ?? g, color: config?.[g]?.color }])), accent);
const xs = data.map((d) => num(d[x])), ys = data.map((d) => num(d[y]));
const xt = niceTicks(Math.min(...xs), Math.max(...xs), 6);
const yt = niceTicks(Math.min(0, ...ys), Math.max(...ys), 4);
const [x0, x1, y0, y1] = [xt[0], xt[xt.length - 1], yt[0], yt[yt.length - 1]];
const fmt = formatter(format);
const groupOf = (d: Datum) => (group ? String(d[group]) : 'all');
const [ref, size] = useSize<HTMLDivElement>();
const pens = useMemo((): Pen[] | null => {
if (!size) return null;
const { w, h } = size;
const sx = linear(x0, x1, 0, w), sy = linear(y0, y1, h, 0);
const axes: Mark[] = [
...yt.map((t) => ({ d: toPath([[0, sy(t)], [w, sy(t)]]), width: t === y0 ? 1 : 0.6, opacity: t === y0 ? 0.9 : 0.2 })),
...xt.map((t) => (t === x0 ? { d: toPath([[0, h], [0, 0]]), width: 1, opacity: 0.9 } : { d: toPath([[sx(t), h], [sx(t), 0]]), width: 0.6, opacity: 0.14 })),
];
const points: Mark[] = data
.map((d) => ({ d, s: series.find((s) => s.key === groupOf(d)) }))
.filter((p) => p.s)
.sort((a, b) => num(a.d[x]) - num(b.d[x]))
.map(({ d, s }) => ({ d: marker(s!.marker, sx(num(d[x])), sy(num(d[y])), 3.4), color: s!.color, width: 1.3 }));
const lines: Mark[] = [];
if (trend) {
for (const s of series) {
const pts = data.filter((d) => groupOf(d) === s.key).map((d) => [num(d[x]), num(d[y])]);
if (pts.length < 2) continue;
const mx = pts.reduce((m, p) => m + p[0], 0) / pts.length, my = pts.reduce((m, p) => m + p[1], 0) / pts.length;
const sxx = pts.reduce((m, p) => m + (p[0] - mx) ** 2, 0);
if (!sxx) continue;
const slope = pts.reduce((m, p) => m + (p[0] - mx) * (p[1] - my), 0) / sxx;
const lo = Math.min(...pts.map((p) => p[0])), hi = Math.max(...pts.map((p) => p[0]));
lines.push({ d: toPath([[sx(lo), sy(my + slope * (lo - mx))], [sx(hi), sy(my + slope * (hi - mx))]]), color: series.length > 1 ? s.color : 'currentColor', width: 1.4, opacity: 0.85 });
}
}
return [
{ marks: axes, ...PHASE.axes, spread: 0.6 },
{ marks: points, from: 1.0, to: lines.length ? 0.64 : PHASE.ink.to, spread: 3 },
{ marks: lines, from: 0.74, to: PHASE.ink.to, spread: 0.4 * (lines.length - 1) },
];
}, [size, data, x, y, group, config, trend, accent, x0, x1, y0, y1]);
const yTicks = yt.map((t) => ({ label: fmt(t), at: (t - y0) / (y1 - y0) }));
return (
<figure className={`w-full ${className}`}>
<DataTable caption={label} head={[xLabel ?? x, yLabel ?? y, ...(group ? [group] : [])]} rows={data.map((d) => [num(d[x]), num(d[y]), ...(group ? [config?.[groupOf(d)]?.label ?? groupOf(d)] : [])])} />
<div aria-hidden>
{yLabel && <p data-slot="y-title" className="mb-3 font-mono text-[11px] uppercase tracking-[0.14em] text-muted-foreground">{yLabel}</p>}
<div className="flex">
<YLabels ticks={yTicks} />
<div ref={ref} data-slot="plot" className="relative min-w-0 flex-1" style={{ height }}>
{size && pens ? <PenLayers size={size} pens={pens} trigger={trigger} duration={duration} /> : null}
</div>
</div>
<XLabels ticks={xt.map((t) => ({ label: fmt(t), at: (t - x0) / (x1 - x0) }))} offset={gutter(yTicks.map((t) => t.label))} />
{xLabel && <p data-slot="x-title" className="mt-3 text-right font-mono text-[11px] uppercase tracking-[0.14em] text-muted-foreground">{xLabel}</p>}
{(legend ?? series.length > 1) && <Legend series={series} kind="marker" className="mt-4" />}
</div>
</figure>
);
}
Parts list
| Item | Part, as installed |
|---|---|
| 1 | components/scrollfx/chart-scatter.tsxThe component |
| 2 | lib/chart.tsScales, ticks, curves, hatching and the pens the charts share |
| 3 | lib/plotter.tsStroke geometry: hatching, sketched lines, scroll windows |
| 4 | svg-scroll-draw ^2.29.0Dependency, from npm |
ViewGroups
Pens1ink2vermilion
Install
npx shadcn@latest add
https://scrollfx-ui.vercel.app/r/chart-scatter.jsonTitle
Scatter chart
scrollfx · Charts- Dwg no.
- chart-scatter
- Sheet
- 06/14
- Scale
- 1:1
- Rev
- 2.29.0
- Drawn by
- svg-scroll-draw
- Licence
- MIT