Donut chart
Donut Chart
Section titled “Donut Chart”A parametric donut chart with five adjustable segments, rendered using
d3-shape’s pie and arc generators and coloured with a chroma-js scale.
Each slice shows its percentage label inside the arc.
How it works
Section titled “How it works”pie() converts raw values into startAngle / endAngle objects that
describe each slice. arc() turns those objects into SVG path strings,
centred on (0, 0) by convention — a <g transform="translate(cx, cy)">
wrapper repositions the whole chart.
chroma.scale(...).colors(n) produces n evenly-spaced colours along a
gradient, one per segment. font.widthOfTextAtSize is not available here;
instead each label is placed at the arc centroid using arc().centroid().
Parameters
Section titled “Parameters”| Parameter | Effect |
|---|---|
| Segments A–E | Relative value of each slice |
| Hole size | Inner radius as a % of outer; 0 = filled pie |
| Background | Canvas fill colour |
"use strict";
const { pie, arc } = require('d3-shape');const chroma = require('chroma-js');
const LABELS = ['A', 'B', 'C', 'D', 'E'];
const getParameterDefinitions = () => [ { name: 'v1', type: 'number', widget: 'slider', caption: 'Segment A', default: 35, min: 1, max: 100 }, { name: 'v2', type: 'number', widget: 'slider', caption: 'Segment B', default: 25, min: 1, max: 100 }, { name: 'v3', type: 'number', widget: 'slider', caption: 'Segment C', default: 20, min: 1, max: 100 }, { name: 'v4', type: 'number', widget: 'slider', caption: 'Segment D', default: 12, min: 1, max: 100 }, { name: 'v5', type: 'number', widget: 'slider', caption: 'Segment E', default: 8, min: 1, max: 100 }, { name: 'holeSize', type: 'number', widget: 'slider', caption: 'Hole size %', default: 48, min: 0, max: 85 }, { name: 'bg', type: 'color', caption: 'Background', default: '#1a1a2e' },];
function main({ v1, v2, v3, v4, v5, holeSize, bg }) { var size = 500; var cx = size / 2, cy = size / 2; var outerR = size * 0.36; var innerR = outerR * (holeSize / 100);
var values = [v1, v2, v3, v4, v5]; var total = values.reduce(function(s, v) { return s + v; }, 0); var slices = pie().value(function(d) { return d; }).padAngle(0.018)(values); var arcGen = arc().innerRadius(innerR).outerRadius(outerR); var labelArc = arc().innerRadius(outerR * 0.65).outerRadius(outerR * 0.65); var colors = chroma.scale(['#e94560', '#533483', '#0f3460', '#0a9396', '#94d2bd']) .mode('lab').colors(values.length);
// Arc slices var paths = slices.map(function(s, i) { return '<path d="' + arcGen(s) + '" fill="' + colors[i] + '" />'; });
// Percentage labels at arc centroids var textElems = slices.map(function(s, i) { var c = labelArc.centroid(s); var pct = Math.round(values[i] / total * 100); if (pct < 5) return ''; // skip label if slice is too small return '<text x="' + c[0].toFixed(1) + '" y="' + c[1].toFixed(1) + '"' + ' text-anchor="middle" dominant-baseline="central"' + ' font-family="sans-serif" font-size="13" font-weight="bold" fill="#fff">' + pct + '%</text>'; });
// Legend var legendX = size * 0.74, legendY = cy - (values.length * 22) / 2; var legendItems = values.map(function(v, i) { var y = legendY + i * 24; return '<rect x="' + legendX + '" y="' + (y - 7) + '" width="14" height="14"' + ' rx="3" fill="' + colors[i] + '"/>' + '<text x="' + (legendX + 20) + '" y="' + y + '"' + ' font-family="sans-serif" font-size="13" fill="#ccc"' + ' dominant-baseline="central">Segment ' + LABELS[i] + '</text>'; });
return '<svg xmlns="http://www.w3.org/2000/svg"' + ' width="' + size + '" height="' + size + '"' + ' viewBox="0 0 ' + size + ' ' + size + '">' + '<rect width="' + size + '" height="' + size + '" fill="' + bg + '"/>' + '<g transform="translate(' + (cx * 0.72).toFixed(1) + ',' + cy + ')">' + paths.join('') + textElems.join('') + '</g>' + legendItems.join('') + '</svg>';}
module.exports = { main, getParameterDefinitions };