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.
A path whose d is built from the points, with a gradient area and one circle per point.
import 'nuclo' let points = [20, 45, 30, 70, 55, 85, 60] const x = (i: number) => (i / (points.length - 1)) * 300 + 10 const y = (value: number) => 140 - value * 1.2 const rand = () => Math.round(5 + Math.random() * 95) // A path's d is a plain string, so build it from the data. const line = () => points.map((v, i) => `${i ? 'L' : 'M'}${x(i)},${y(v)}`).join(' ') const area = () => `${line()} L${x(6)},150 L${x(0)},150 Z` // Each point becomes the average of itself and its neighbours. const smooth = () => points.map((v, i, all) => Math.round((all[i - 1] ?? v) * 0.25 + v * 0.5 + (all[i + 1] ?? v) * 0.25), ) export const LineChart = () => div( svgSvg( { viewBox: '0 0 320 160', width: '320', height: '160' }, defsSvg( linearGradientSvg( { id: 'fill', x1: 0, y1: 0, x2: 0, y2: 1 }, stopSvg({ offset: '0', 'stop-color': '#ff3f00', 'stop-opacity': 0.35, }), stopSvg({ offset: '1', 'stop-color': '#ff3f00', 'stop-opacity': 0, }), ), ), // Grid lines at fixed values. ...[0, 25, 50, 75, 100].map((v) => lineSvg({ x1: 10, x2: 310, y1: y(v) + 0.5, y2: y(v) + 0.5, stroke: '#ddd', 'stroke-dasharray': v ? '2 4' : '', }), ), pathSvg({ fill: 'url(#fill)', stroke: 'none', d: area, style: { transition: 'd 0.4s' }, }), pathSvg({ fill: 'none', stroke: '#ff3f00', 'stroke-width': 2.5, 'stroke-linejoin': 'round', style: { transition: 'd 0.4s' }, d: line, }), ...points.map((_, i) => circleSvg({ r: 4, fill: '#fff', stroke: '#ff3f00', 'stroke-width': 2, style: { transition: 'cx 0.4s, cy 0.4s' }, cx: x(i), cy: () => y(points[i]), }), ), ), div( { style: { display: 'flex', gap: '8px', justifyContent: 'center' } }, button('New data', { onClick: () => { points = points.map(rand); update() }, }), button('Smooth', { onClick: () => { points = smooth(); update() } }), ), )