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 grid of rects whose fill-opacity follows a level. Click a cell to bump it; a titleSvg child is its tooltip.
import 'nuclo' const COLS = 14 const ROWS = 7 const OPACITY = [0.08, 0.3, 0.55, 0.8, 1] const random = () => Math.floor(Math.random() * 5) let levels = Array.from({ length: COLS * ROWS }, random) // One rect per cell, column by column. Its fill-opacity is a // function of the cell's level, and a titleSvg child is the // browser's own tooltip. onClick works on SVG elements as on any. export const Heatmap = () => div( svgSvg( { viewBox: '0 0 235 116', width: '235', height: '116' }, ...levels.map((_, i) => rectSvg( { x: Math.floor(i / ROWS) * 17, y: (i % ROWS) * 17, width: 14, height: 14, rx: 3, fill: '#ff3f00', style: { cursor: 'pointer', transition: 'fill-opacity 0.2s' }, 'fill-opacity': () => OPACITY[levels[i]], onClick: () => { levels[i] = (levels[i] + 1) % 5; update() }, }, titleSvg(() => `${levels[i]} commits`), ), ), ), div(() => `${levels.reduce((a, b) => a + b, 0)} commits · click a cell`), div( { style: { display: 'flex', gap: '8px', justifyContent: 'center' } }, button('Shuffle', { onClick: () => { levels = levels.map(random); update() }, }), button('Clear', { onClick: () => { levels = levels.map(() => 0); update() }, }), ), )