Examples
Practical examples. Live demos.
Pick an example to open it full screen, with the live preview and its source side by side.
Pick an example to open it full screen, with the live preview and its source side by side.
Five values on five axes. The shape is one path whose d is computed from the data, so update() morphs it.
import 'nuclo' const axes = ['Speed', 'Size', 'DX', 'SSR', 'Docs'] const start = [90, 95, 80, 70, 60] let values = [...start] // Axis i points at -90° + i * 72°; a value is a distance along it. const angle = (i: number) => ((-90 + i * 72) * Math.PI) / 180 const px = (i: number, v: number) => 100 + Math.cos(angle(i)) * 68 * (v / 100) const py = (i: number, v: number) => 100 + Math.sin(angle(i)) * 68 * (v / 100) // A closed path through one point per axis. The grid rings are // the same path at fixed values; the data shape reads the values. const ring = (v: number) => axes.map((_, i) => `${i ? 'L' : 'M'}${px(i, v)},${py(i, v)}`).join(' ') + ' Z' const shape = () => axes .map((_, i) => `${i ? 'L' : 'M'}${px(i, values[i])},${py(i, values[i])}`) .join(' ') + ' Z' export const RadarChart = () => div( svgSvg( { viewBox: '0 0 200 200', width: '200', height: '200' }, ...[25, 50, 75, 100].map((v) => pathSvg({ d: ring(v), fill: 'none', stroke: '#ddd' }), ), ...axes.map((_, i) => lineSvg({ x1: 100, y1: 100, x2: px(i, 100), y2: py(i, 100), stroke: '#ddd', }), ), pathSvg({ fill: '#ff3f00', 'fill-opacity': 0.25, stroke: '#ff3f00', 'stroke-width': 2, 'stroke-linejoin': 'round', style: { transition: 'd 0.4s' }, d: shape, }), ...axes.map((_, i) => circleSvg({ r: 3.5, fill: '#fff', stroke: '#ff3f00', 'stroke-width': 2, style: { transition: 'cx 0.4s, cy 0.4s' }, cx: () => px(i, values[i]), cy: () => py(i, values[i]), }), ), ...axes.map((name, i) => textSvg( { x: px(i, 126), y: py(i, 126) + 4, 'text-anchor': 'middle' }, { 'font-size': 11 }, name, ), ), ), div( { style: { display: 'flex', gap: '8px', justifyContent: 'center' } }, button('Randomize', { onClick: () => { values = values.map(() => Math.round(20 + Math.random() * 80)) update() }, }), button('Reset', { onClick: () => { values = [...start]; update() } }), ), )