Home / Part to Whole / Donut Chart
Donut Chart in HTML, CSS and JavaScript
A donut chart is a pie chart with a hole in the middle. This free template draws one with plain SVG and vanilla JavaScript, no chart library. The example shows workouts logged by members of a fitness app.
What is a donut chart?
A donut chart is a pie chart with a hole in the middle. The ring shows the parts of a whole, and the empty center is a handy place for the total or for details about the part you point at.
Because people read the length of the ring rather than the size of a wedge, many find donut charts a little easier to compare than pies. They also look lighter on dashboards.
At a glance
- Best for
- A few shares with the total shown in the middle
- Data you need
- A name and a count for each part
- Skip it when
- More than 6 parts. The ring gets too busy.
- Made with
- HTML, CSS and vanilla JavaScript. No library.
When to use a donut chart
- Dashboard summaries with a total
- Workout, task or ticket types
- Budget or time split into a few parts
- App screens with little space
When to pick another chart
- Pie Chart: you want the most familiar chart for a general audience
- Semi Donut Chart: you are showing progress toward a goal
What you get in this template
- A ring drawn with SVG arc paths
- The total in the center changes to the share of the part you hover
- Hide any part from the key and the ring rebuilds
- Thick card colored gaps between parts
- Dark theme with bright colors
- Keyboard support and a data table
How the code works
Here is a short version of the idea behind this donut chart. The full file adds the axis, labels, tooltips, sorting and resizing on top of it.
const parts = [['Running', 471, '#4ade80'], ['Cycling', 298, '#38bdf8'], ['Strength', 223, '#f472b6']];
const total = parts.reduce((a, p) => a + p[1], 0);
const cx = 200, cy = 150, r = 120, inner = 75;
let start = 0;
const pt = (rad, a) => (cx + rad * Math.sin(a)) + ',' + (cy - rad * Math.cos(a));
parts.forEach(([name, n, color]) => {
const end = start + n / total * Math.PI * 2, big = end - start > Math.PI ? 1 : 0;
make('path', { fill: color, d: 'M' + pt(r, start) + 'A' + r + ',' + r + ' 0 ' + big + ' 1 ' + pt(r, end) +
'L' + pt(inner, end) + 'A' + inner + ',' + inner + ' 0 ' + big + ' 0 ' + pt(inner, start) + 'Z' });
start = end;
});
drawText(cx, cy + 6, total, 'middle');The example uses four tiny helpers that create SVG shapes. Put them above the code and it runs as is.
const svg = document.querySelector('svg');
function make(tag, attrs) {
const n = document.createElementNS('http://www.w3.org/2000/svg', tag);
for (const k in attrs) n.setAttribute(k, attrs[k]);
svg.appendChild(n);
return n;
}
function drawRect(x, y, width, height, fill) { return make('rect', { x, y, width, height, fill }); }
function drawCircle(cx, cy, r, fill) { return make('circle', { cx, cy, r, fill }); }
function drawLine(x1, y1, x2, y2, stroke, width, dash) {
return make('line', { x1, y1, x2, y2, stroke, 'stroke-width': width || 1, 'stroke-dasharray': dash || 'none' });
}
function drawText(x, y, str, anchor) {
const t = make('text', { x, y, 'text-anchor': anchor || 'start' });
t.textContent = str;
return t;
}How to add it to your website
- Download the file. Click Download HTML file above. You get one file called donut-chart.html with everything inside.
- Change the data. Open the file in any code editor and find the DATA list near the bottom. Replace the names and numbers with your own.
- Change the colors and text. Colors are at the top of the style tag. The title, subtitle and main point are plain HTML near the top of the body.
- Put it online. Upload the file to any host, such as GitHub Pages, Netlify or your own server, or paste it into your site. There is no build step.
Questions people ask
What is the difference between a pie chart and a donut chart?
A donut chart is a pie with the middle cut out. The data is the same, but the empty center can hold a total or a label, and many people find the ring easier to read.
Is it donut chart or doughnut chart?
Both spellings are used and mean the same thing. Donut is more common in American English and in search.
How thick should the ring be?
About a third to a half of the radius. Too thin and the colors are hard to see, too thick and it looks like a pie again.
What should go in the middle of a donut chart?
The total is the most useful choice. You can also show the share of the part the reader is pointing at, like this template does.
More part to whole

021. Pie Chart
Example: how customers paid at a cafe
023. Semi Donut Chart
Example: a fundraising appeal and the money still needed
024. Treemap
Example: bookshop sales by section and genre
025. Sunburst Chart
Example: where a city sends its household waste
026. Waffle Chart
Example: how 100 new customers found a furniture shop
027. Marimekko Chart
Example: e-bike market share by region and brand
028. Funnel Chart
Example: shoppers moving from first visit to purchase in an online shop
029. Icicle Chart
Example: what is filling up a laptop drive