Skip to content

Donut Chart with Legend

SVG donut chart of traffic sources with the total in the centre and an interactive legend that highlights each segment and shows its share, value and percent.

Also known as: doughnut chart, svg donut chart, pie chart with legend, traffic sources chart, ring chart

Donut Chart with Legend live demo

Open in new tab

Pure CSS code (HTML, CSS, JS)

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.items in script.js: set label, detail, value and color for each source; percentages and the table are computed for you.
  • DATA.unit: the word shown in the centre and in the table, e.g. orders or visits.
  • DATA.growth: the change shown in the .dn-badge at the top right.
  • GAP in script.js: the space between segments; use 0 for a closed ring.
  • --dn-stroke on .dn-card: ring thickness (default 22px).
  • --dn-size: diameter of the chart, 13.5rem on phones and 16rem from 720px.
  • --dn-row and --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.

Donut Chart with Legend at a glance

Framework
Pure CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
fonts.googleapis.com
Layout
Responsive, works on mobile
Category
Charts
License
Free for personal and commercial use

Questions about this template

Can I use the Donut Chart with Legend code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load fonts.googleapis.com from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Manrope:wght@500;600;700;800&display=swap">
Donut Chart with Legend – free Pure CSS chart template
Donut Chart with Legend · free Pure CSS chart template

More Charts

Horizontal Bar Chart Ranking – free Bootstrap 6 chart template

Horizontal Bar Chart Ranking

Horizontal bar chart ranking the top 8 products by revenue, units or returns; a select re-sorts the bars and announces the new order.

Pie Chart with Percentage Legend – free Bootstrap 6 chart template

Pie Chart with Percentage Legend

Budget pie chart with a percentage legend table: SVG slices drawn from a data array, and clicking a slice or category row pulls the slice out.

Bar Chart with Axis and Tooltips – free Pure CSS chart template

Bar Chart with Axis and Tooltips

Monthly sales bar chart with a labelled value axis, gridlines, tooltips on hover, focus and tap, and a this year / last year toggle that animates the bars.