| skill | score |
|---|---|
| Speed | 86 |
| Range | 64 |
| Control | 92 |
| Stamina | 71 |
| Accuracy | 58 |
| Recovery | 77 |
- 1max · 100
- 2accent · the shape
- 3grid · polygon
- 4data · 6 spokes
Schedule of props
| Mark | Prop | Type | Default | Description |
|---|---|---|---|---|
| P1 | data | Datum[] | SAMPLE | One object per spoke. |
| P2 | index | string | 'skill' | The key in each object that names its spoke. |
| P3 | config | ChartConfig | — | The series to draw, by key: a label and, if you like, a colour. Defaults to every number in the first object. |
| P4 | max | number | — | The value at the outer ring. Defaults to a round number above the largest value. |
| P5 | grid | 'polygon' | 'circle' | 'polygon' | Rings, as polygons (like a web) or circles. |
| P6 | legend | boolean | — | Show the key. By default, when there is more than one series. |
| P7 | accent | string | ACCENT | The first series' colour. |
| P8 | label | string | 'Radar chart' | What the chart shows, for screen readers (it captions the data table). |
| P9 | trigger | ChartTrigger | 'scroll' | `scroll`: inked as it rises, done at mid-screen. `view`: played once as it comes into view. |
| P10 | duration | number | 1800 | With `view`, how long the chart takes, in ms. |
| P11 | className | string | '' | — |
Notes
- 1.A radar chart: rings and spokes, then each series' shape hatched in and outlined, the first in the accent.
- 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, Single series. The web first, then the shape hatched in and outlined in the accent, a dot on every reading.
- 5.Usage, as drawn:
import { ChartRadar } from '@/components/scrollfx/chart-radar'; export function Single() { return <ChartRadar label="Scores by skill, out of 100" />; }
- 4.Detail B, Two series, round grid. A second series in ink with its own hatch and markers; grid="circle" draws the rings round.
- 5.Usage, as drawn:
import { ChartRadar } from '@/components/scrollfx/chart-radar'; export function Compare() { return ( <ChartRadar data={[ { metric: 'Speed', v1: 62, v2: 88 }, { metric: 'Size', v1: 80, v2: 70 }, { metric: 'Docs', v1: 55, v2: 84 }, { metric: 'Types', v1: 70, v2: 92 }, { metric: 'Tests', v1: 48, v2: 76 }, ]} index="metric" config={{ v2: { label: 'Version 2' }, v1: { label: 'Version 1' } }} max={100} grid="circle" label="Version 1 and version 2 compared on five measures, out of 100" /> ); }
View source · components/scrollfx/chart-radar.tsx
'use client';
/**
* chart-radar — a radar chart a pen plots as you scroll: the rings and spokes
* first, then each series' shape hatched in, then its outline, the first in
* the accent colour, with a dot on every reading.
*
* 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 names, 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, hatchMarks, marker, niceTicks, num, seriesOf, toPath, useSize,
type ChartTrigger, type ChartConfig, type Datum, type Mark, type Pen, type Pt,
} from '@/lib/chart';
export interface ChartRadarProps {
/** One object per spoke. */
data?: Datum[];
/** The key in each object that names its spoke. */
index?: string;
/** The series to draw, by key: a label and, if you like, a colour. Defaults to every number in the first object. */
config?: ChartConfig;
/** The value at the outer ring. Defaults to a round number above the largest value. */
max?: number;
/** Rings, as polygons (like a web) or circles. */
grid?: 'polygon' | 'circle';
/** Show the key. By default, when there is more than one series. */
legend?: boolean;
/** The first series' 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;
}
const SAMPLE: Datum[] = [
{ skill: 'Speed', score: 86 }, { skill: 'Range', score: 64 }, { skill: 'Control', score: 92 },
{ skill: 'Stamina', score: 71 }, { skill: 'Accuracy', score: 58 }, { skill: 'Recovery', score: 77 },
];
// The web's radius and where the spoke names sit, as fractions of its square box
// (the names fall in the padding round it).
const R = 0.46, RL = 0.53;
export function ChartRadar({
data = SAMPLE,
index = 'skill',
config,
max,
grid = 'polygon',
legend,
accent = ACCENT,
label = 'Radar chart',
trigger = 'scroll',
duration = 1800,
className = '',
}: ChartRadarProps) {
const series = seriesOf(data, index, config, accent);
const ticks = niceTicks(0, max ?? Math.max(...data.flatMap((d) => series.map((s) => num(d[s.key])))), 4);
const top = max ?? ticks[ticks.length - 1];
const n = data.length;
const angle = (i: number) => (i / n) * Math.PI * 2;
const [ref, size] = useSize<HTMLDivElement>();
const pens = useMemo((): Pen[] | null => {
if (!size) return null;
const s = Math.min(size.w, size.h);
const cx = size.w / 2, cy = size.h / 2, r = s * R;
const at = (i: number, v: number): Pt => [cx + Math.sin(angle(i)) * r * v, cy - Math.cos(angle(i)) * r * v];
const rings: Mark[] = ticks.filter((t) => t > 0 && t <= top).map((t) => {
const k = t / top;
const pts: Pt[] = grid === 'circle'
? Array.from({ length: 97 }, (_, j): Pt => [cx + Math.sin((j / 96) * Math.PI * 2) * r * k, cy - Math.cos((j / 96) * Math.PI * 2) * r * k])
: [...Array.from({ length: n }, (_, i) => at(i, k)), at(0, k)];
return { d: toPath(pts), width: k === 1 ? 0.9 : 0.6, opacity: k === 1 ? 0.6 : 0.28 };
});
const spokes: Mark[] = data.map((_, i) => ({ d: toPath([[cx, cy], at(i, 1)]), width: 0.6, opacity: 0.35 }));
const fill: Mark[] = [];
const outline: Mark[] = [];
const dots: Mark[] = [];
series.forEach((ser, k) => {
const pts = data.map((d, i) => at(i, Math.max(0, num(d[ser.key])) / top));
fill.push(...hatchMarks(pts, ser.hatch, { opacity: k === 0 ? 0.75 : 0.6, width: 0.9, inset: 3 }));
outline.push({ d: toPath([...pts, pts[0]]), color: ser.color, width: k === 0 ? 2 : 1.5 });
pts.forEach(([x, y]) => dots.push({ d: marker(ser.marker, x, y, 2.6), color: ser.color, width: 1.2 }));
});
return [
{ marks: [...rings, ...spokes], ...PHASE.axes, spread: 0.8 },
{ marks: fill, ...PHASE.fill, spread: 2.5 },
{ marks: outline, from: PHASE.ink.from, to: 0.6, spread: 0.5 * (outline.length - 1) },
{ marks: dots, from: 0.7, to: PHASE.ink.to, spread: 2 },
];
}, [size, data, index, config, top, grid, accent]);
return (
<figure className={`mx-auto w-full max-w-md ${className}`}>
<DataTable caption={label} head={[index, ...series.map((s) => s.label)]} rows={data.map((d) => [String(d[index]), ...series.map((s) => num(d[s.key]))])} />
<div aria-hidden>
<div className="px-16 py-7">
<div ref={ref} data-slot="plot" className="relative aspect-square w-full">
{size && pens ? <PenLayers size={size} pens={pens} trigger={trigger} duration={duration} /> : null}
{data.map((d, i) => {
const sx = Math.sin(angle(i)), sy = -Math.cos(angle(i));
const tx = sx > 0.3 ? '0%' : sx < -0.3 ? '-100%' : '-50%';
const ty = sy < -0.6 ? '-100%' : sy > 0.6 ? '0%' : '-50%';
return (
<span
key={i}
className="absolute whitespace-nowrap font-mono text-[11px] uppercase tracking-[0.12em] text-muted-foreground"
style={{ left: `${(50 + sx * RL * 100).toFixed(2)}%`, top: `${(50 + sy * RL * 100).toFixed(2)}%`, translate: `${tx} ${ty}` }}
>
{String(d[index])}
</span>
);
})}
</div>
</div>
{(legend ?? series.length > 1) && <Legend series={series} kind="hatch" className="mt-2 justify-center" />}
</div>
</figure>
);
}
Parts list
| Item | Part, as installed |
|---|---|
| 1 | components/scrollfx/chart-radar.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 |
ViewSingle series
Pens1ink2vermilion
Install
npx shadcn@latest add
https://scrollfx-ui.vercel.app/r/chart-radar.jsonTitle
Radar chart
scrollfx · Charts- Dwg no.
- chart-radar
- Sheet
- 05/14
- Scale
- 1:1
- Rev
- 2.29.0
- Drawn by
- svg-scroll-draw
- Licence
- MIT