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.
Rects from an array. Their y and height are functions of the data, so update() moves the bars.
import 'nuclo' const labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] let data = [40, 72, 55, 90, 30, 64] const rand = () => Math.round(10 + Math.random() * 90) const barY = (value: number) => 140 - value * 1.3 // Every SVG builder ends in "Svg". Attributes that depend on // data are functions, so update() moves the bars. A CSS // transition on y and height animates the move. export const BarChart = () => div( svgSvg( { viewBox: '0 0 320 160', width: '320', height: '160' }, lineSvg({ x1: 0, y1: 140.5, x2: 320, y2: 140.5, stroke: '#ccc' }), ...labels.map((label, i) => gSvg( rectSvg({ x: i * 52 + 14, width: 36, rx: 5, fill: '#ff3f00', style: { transition: 'y 0.4s, height 0.4s' }, y: () => barY(data[i]), height: () => data[i] * 1.3, }), textSvg( { x: i * 52 + 32, y: 156, 'text-anchor': 'middle' }, label, ), // A text's y does not transition; a CSS transform does. gSvg( { style: () => ({ transition: 'transform 0.4s', transform: `translateY(${barY(data[i]) - 6}px)`, }), }, textSvg( { x: i * 52 + 32, y: 0, 'text-anchor': 'middle' }, () => data[i], ), ), ), ), ), button('Randomize', { onClick: () => { data = data.map(rand); update() }, }), button('Sort', { onClick: () => { data = [...data].sort((a, b) => a - b); update() }, }), )