예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
조각 하나에 원 하나. stroke-dasharray는 고리에서 조각이 차지할 몫을, stroke-dashoffset은 시작 위치를 정해 줍니다.
import 'nuclo' const names = ['Core', 'Router', 'Docs'] const colors = ['#ff3f00', '#2563eb', '#ff8a00'] const start = [52, 18, 30] let parts = [...start] const C = 2 * Math.PI * 54 // the ring's circumference const total = () => parts.reduce((a, b) => a + b, 0) const share = (i: number) => parts[i] / total() const before = (i: number) => parts.slice(0, i).reduce((a, b) => a + b, 0) / total() const ring = { cx: 80, cy: 80, r: 54, fill: 'none', 'stroke-width': 18 } // One circle per slice: the dash array is its share of the ring, // the dash offset moves it past the slices before it. export const DonutChart = () => div( svgSvg( { viewBox: '0 0 160 160', width: '160', height: '160' }, circleSvg(ring, { stroke: '#eee' }), ...parts.map((_, i) => circleSvg(ring, { stroke: colors[i], transform: 'rotate(-90 80 80)', style: { transition: 'stroke-dasharray 0.5s, stroke-dashoffset 0.5s', }, 'stroke-dasharray': () => `${share(i) * C} ${C}`, 'stroke-dashoffset': () => -before(i) * C, }), ), textSvg( { x: 80, y: 80, 'text-anchor': 'middle', 'dominant-baseline': 'middle', 'font-size': 22, 'font-weight': 700, }, () => `${Math.round(share(0) * 100)}%`, ), textSvg( { x: 80, y: 102, 'text-anchor': 'middle', 'font-size': 11 }, names[0], ), ), div( { style: { display: 'flex', gap: '8px', justifyContent: 'center' } }, ...names.map((name, i) => button(`+ ${name}`, { onClick: () => { parts[i] += 10; update() } }), ), button('Reset', { onClick: () => { parts = [...start]; update() } }), ), )