Exemples
Du concret. Des démos en direct.
Choisissez un exemple pour l'ouvrir en plein écran : l'aperçu en direct et son code source, côte à côte.
Choisissez un exemple pour l'ouvrir en plein écran : l'aperçu en direct et son code source, côte à côte.
Sélectionnez une plage sur la vue d'ensemble pour zoomer le graphique du dessus. Un clipPath qui suit la sélection colore la zone choisie.
import 'nuclo' const W = 320 const N = 120 // A random walk, seeded so it is the same on every load. let seed = 7 const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647 const data: number[] = [] for (let i = 0, v = 50; i < N; i++) { v = Math.max(5, Math.min(95, v + (random() - 0.5) * 16)) data.push(Math.round(v * 10) / 10) } let from = 30 let to = 70 let grab: { move: boolean; at: number } | null = null const slice = () => data.slice(from, to + 1) // The overview: every day in the strip at the bottom. const ox = (i: number) => (i / (N - 1)) * W const overview = data .map((v, i) => `${i ? 'L' : 'M'}${ox(i).toFixed(1)},${(196 - v * 0.42).toFixed(1)}`) .join(' ') // The focus: the brushed days across the full width, with the // y scale fitted to their own min and max. function focus() { const days = slice() const min = Math.min(...days) const max = Math.max(...days) return days .map((v, j) => { const x = (j / Math.max(1, to - from)) * W const y = 112 - ((v - min) / Math.max(1, max - min)) * 100 return `${j ? 'L' : 'M'}${x.toFixed(1)},${y.toFixed(1)}` }) .join(' ') } const index = (e: PointerEvent) => { const box = (e.currentTarget as Element).getBoundingClientRect() const f = Math.max(0, Math.min(1, (e.clientX - box.left) / box.width)) return Math.round(f * (N - 1)) } // A clipPath whose rect follows the brush paints the brushed // part of the overview in color. A transparent rect on top takes // the pointer: inside the brush a drag moves it, outside it // draws a new one. export const BrushZoom = () => div( svgSvg( { viewBox: '0 0 320 200', width: '320', height: '200' }, defsSvg( linearGradientSvg( { id: 'fade', x1: '0', y1: '0', x2: '0', y2: '1' }, stopSvg({ offset: '0', 'stop-color': '#ff3f00', 'stop-opacity': 0.3 }), stopSvg({ offset: '1', 'stop-color': '#ff3f00', 'stop-opacity': 0 }), ), clipPathSvg( { id: 'brush' }, rectSvg({ y: 136, height: 64, x: () => ox(from), width: () => ox(to) - ox(from), }), ), ), ...[12, 62, 112].map((y) => lineSvg({ x1: 0, x2: W, y1: y + 0.5, y2: y + 0.5, stroke: '#ddd', 'stroke-dasharray': '2 4' }), ), pathSvg({ d: () => `${focus()} L${W},120 L0,120 Z`, fill: 'url(#fade)' }), pathSvg({ d: focus, fill: 'none', stroke: '#ff3f00', 'stroke-width': 2, 'stroke-linejoin': 'round', }), textSvg({ x: 0, y: 132, 'font-size': 11, fill: '#999' }, () => `day ${from}`), textSvg( { x: W, y: 132, 'text-anchor': 'end', 'font-size': 11, fill: '#999' }, () => `day ${to}`, ), pathSvg({ d: overview, fill: 'none', stroke: '#ddd', 'stroke-width': 1.5 }), pathSvg({ d: overview, fill: 'none', stroke: '#ff3f00', 'stroke-width': 1.5, 'clip-path': 'url(#brush)', }), rectSvg({ y: 140, height: 58, rx: 4, fill: '#ff3f00', 'fill-opacity': 0.1, stroke: '#ff3f00', x: () => ox(from), width: () => ox(to) - ox(from), style: { pointerEvents: 'none' }, }), rectSvg({ x: 0, y: 136, width: W, height: 64, fill: 'transparent', style: { cursor: 'crosshair', touchAction: 'none' }, onPointerDown: (e) => { e.currentTarget.setPointerCapture(e.pointerId) const i = index(e) grab = i >= from && i <= to ? { move: true, at: i - from } : { move: false, at: i } if (!grab.move) from = to = i update() }, onPointerMove: (e) => { if (!grab) return const i = index(e) if (grab.move) { const len = to - from from = Math.max(0, Math.min(N - 1 - len, i - grab.at)) to = from + len } else { from = Math.min(grab.at, i) to = Math.max(grab.at, i) } update() }, onPointerUp: () => { grab = null if (to - from < 4) { to = Math.min(N - 1, from + 4); from = to - 4 } update() }, }), ), div(() => { const days = slice() const avg = days.reduce((a, b) => a + b, 0) / days.length return `${to - from + 1} days · avg ${avg.toFixed(1)} · max ${Math.max(...days)}` }), div( { style: { display: 'flex', gap: '8px' } }, button('All', { onClick: () => { from = 0; to = N - 1; update() } }), button('Last 30', { onClick: () => { from = N - 30; to = N - 1; update() } }), ), )