예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
점들로 d를 만들어 내는 경로 하나에, 그라데이션 영역과 점마다 원 하나를 더했습니다.
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() } }), ), )