An SVG donut chart with an interactive legend, built with pure CSS and vanilla JavaScript. This doughnut chart example breaks website traffic down by source, shows the total in the centre and lists every source with its value and percent, for analytics dashboards, marketing reports and admin panels.
How it works
The root section.dn-card contains the chart (.dn-figure) and the legend (ul.dn-legend). The chart is a single inline SVG with a grey .dn-track ring and a g.dn-segs group that the script fills with one circle.dn-seg per source. Each segment is the same circle with a different stroke-dasharray (its share of the circumference) and stroke-dashoffset (where it starts), so no path math is needed. .dn-center sits on top of the ring and shows the total.
All values come from the DATA array at the top of script.js. On DOMContentLoaded the script sums the values, draws the segments with a small gap between them, builds a button.dn-row for every source with a colour swatch, value, percent and a thin .dn-meter bar, and fills the hidden table.
Hovering a legend row or a segment, or focusing a row with the keyboard, adds .is-focused to the card and .is-active to that segment: the other segments fade, the active one gets thicker and the centre switches to the source name, its percent and its session count. Clicking or tapping a row pins the highlight (aria-pressed="true"); clicking it again or pressing Escape clears it.
Customization
DATA.itemsinscript.js: setlabel,detail,valueandcolorfor each source; percentages and the table are computed for you.DATA.unit: the word shown in the centre and in the table, e.g.ordersorvisits.DATA.growth: the change shown in the.dn-badgeat the top right.GAPinscript.js: the space between segments; use0for a closed ring.--dn-strokeon.dn-card: ring thickness (default22px).--dn-size: diameter of the chart,13.5remon phones and16remfrom 720px.--dn-rowand--dn-radius: the soft background of the legend rows and the card corner radius.
Use cases
- Website analytics: traffic sources, devices or countries as shares of all sessions.
- Marketing reports: budget split per channel with exact values next to the chart.
- SaaS dashboards: plan mix or revenue by product in a compact card.
- Survey results: answer distribution with a clear legend.
Accessibility
The SVG is decorative (aria-hidden); the information lives in the legend buttons, whose text includes the source, value and percent. Each row is a toggle button with aria-pressed, has a visible :focus-visible outline and works with Tab, Enter, Space and Escape. A polite live region announces when a source is highlighted or cleared, and a visually hidden table.dn-sr repeats every number. Colours are never the only cue, and transitions are disabled with prefers-reduced-motion: reduce.
Usage notes
Several charts can share a page; each card keeps its own state.