Примеры
Практические примеры. Живые демо.
Выберите пример, и он откроется на весь экран: превью и исходный код рядом.
Выберите пример, и он откроется на весь экран: превью и исходный код рядом.
Один круг на сектор. 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() } }), ), )