Donut Chart with Linked Legend
A donut whose segments and legend highlight each other, with the value shown in the centre.
AI prompt
Create a donut chart for four categories: Direct 42%, Search 31%, Social 18% and Email 9%, drawn with SVG circles of radius 40 using stroke-dasharray and stroke-dashoffset, stroke width 14, rotated to start at the top, using four distinct greys plus one dark (#171717, #6f6f6f, #a3a3a3, #d4d4d4) with a 2px gap between arcs. The centre shows the total "12.4k visits" at rest and the name and percentage of the highlighted segment otherwise. A legend of four buttons (swatch, name, value) sits beside the chart. Hovering or focusing a legend entry or an arc highlights that segment (stroke width 18) and dims the others to 0.35 over 160ms; clicking pins it. Legend buttons have aria-pressed. On load the ring draws clockwise over 800ms ease-out. Under prefers-reduced-motion skip the draw and transitions.About this pattern
Definition
A ring split into proportional arcs. Hovering or focusing an arc or its legend entry highlights that part, dims the others and shows its name and share in the centre.
Purpose & UX principles
- Shows parts of a whole when there are few categories (up to five).
- The legend is the accessible control: each entry is a button, and the numbers are always visible in it, so colour is never the only cue.
- The centre label removes the need for a tooltip.
Interaction states
- Rest (total shown), highlighted segment, focus-visible on legend, pinned segment on click.
Motion specification
- Highlight: the active arc thickens from 14 to 18px and the others dim in 160ms.
- The ring draws clockwise on load, 800ms ease-out.
- Reduced motion: no draw, instant highlight.
Responsive behaviour
- The SVG scales with its container; the legend sits beside the ring on wide layouts and below on narrow ones.
Code
<div class="wrap">
<div class="ring"><svg viewBox="0 0 100 100" id="svg" aria-hidden="true"></svg><div class="ctr" id="ctr" aria-live="polite"><b>12.4k</b><small>visits</small></div></div>
<ul class="lg" id="lg"></ul>
</div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.wrap{display:flex;align-items:center;gap:18px;flex-wrap:wrap;justify-content:center}
.ring{position:relative;width:132px;height:132px}
svg{width:100%;height:100%;transform:rotate(-90deg)}
.seg{fill:none;stroke-width:14;transition:stroke-width .16s,opacity .16s,stroke-dasharray .8s ease-out;cursor:pointer}
.ctr{position:absolute;inset:0;display:grid;place-content:center;text-align:center;pointer-events:none}
.ctr b{font-size:19px;font-weight:600;line-height:1.1}
.ctr small{font-size:11px;color:var(--muted)}
.lg{list-style:none;display:grid;gap:4px}
.lg button{display:flex;align-items:center;gap:8px;width:136px;padding:6px 8px;border:0;border-radius:8px;background:none;font:inherit;font-size:12.5px;cursor:pointer;text-align:left;transition:background .15s}
.lg button:hover,.lg button[aria-pressed=true]{background:var(--surface)}
.lg button:focus-visible{outline:2px solid var(--accent)}
.lg i{width:10px;height:10px;border-radius:3px;flex:none}
.lg span{flex:1}.lg em{font-style:normal;color:var(--muted)}
.dim .seg:not(.act){opacity:.35}
.seg.act{stroke-width:18}
@media (prefers-reduced-motion:reduce){.seg{transition:none}}const D=[['Direct',42,'#171717'],['Search',31,'#6f6f6f'],['Social',18,'#a3a3a3'],['Email',9,'#d4d4d4']],C=2*Math.PI*40,svg=document.getElementById('svg'),lg=document.getElementById('lg'),ctr=document.getElementById('ctr'),wrap=svg.closest('.wrap');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;let off=0,pin=-1;const segs=[],btns=[];
D.forEach(([n,v,c],i)=>{const len=C*v/100,s=(svg.insertAdjacentHTML('beforeend','<circle class="seg" cx="50" cy="50" r="40" stroke="'+c+'"></circle>'),svg.lastElementChild);
s.setAttribute('stroke-dashoffset',-off);s.setAttribute('stroke-dasharray',reduce?(len-2)+' '+(C-len+2):'0 '+C);segs.push(s);
if(!reduce)requestAnimationFrame(()=>requestAnimationFrame(()=>s.setAttribute('stroke-dasharray',(len-2)+' '+(C-len+2))));off+=len;
const li=document.createElement('li');li.innerHTML='<button type="button" aria-pressed="false"><i style="background:'+c+'"></i><span>'+n+'</span><em>'+v+'%</em></button>';lg.append(li);btns.push(li.firstChild);
const on=()=>hl(i),offf=()=>hl(pin);[li.firstChild,s].forEach(el=>{el.addEventListener('pointerenter',on);el.addEventListener('pointerleave',offf);});
li.firstChild.addEventListener('focus',on);li.firstChild.addEventListener('blur',offf);
const pinIt=()=>{pin=pin===i?-1:i;btns.forEach((b,k)=>b.setAttribute('aria-pressed',String(k===pin)));hl(pin);};li.firstChild.addEventListener('click',pinIt);s.addEventListener('click',pinIt);});
function hl(i){segs.forEach((s,k)=>s.classList.toggle('act',k===i));wrap.classList.toggle('dim',i>=0);ctr.innerHTML=i<0?'<b>12.4k</b><small>visits</small>':'<b>'+D[i][1]+'%</b><small>'+D[i][0]+'</small>';}One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Donut Chart with Linked Legend — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.wrap{display:flex;align-items:center;gap:18px;flex-wrap:wrap;justify-content:center}
.ring{position:relative;width:132px;height:132px}
svg{width:100%;height:100%;transform:rotate(-90deg)}
.seg{fill:none;stroke-width:14;transition:stroke-width .16s,opacity .16s,stroke-dasharray .8s ease-out;cursor:pointer}
.ctr{position:absolute;inset:0;display:grid;place-content:center;text-align:center;pointer-events:none}
.ctr b{font-size:19px;font-weight:600;line-height:1.1}
.ctr small{font-size:11px;color:var(--muted)}
.lg{list-style:none;display:grid;gap:4px}
.lg button{display:flex;align-items:center;gap:8px;width:136px;padding:6px 8px;border:0;border-radius:8px;background:none;font:inherit;font-size:12.5px;cursor:pointer;text-align:left;transition:background .15s}
.lg button:hover,.lg button[aria-pressed=true]{background:var(--surface)}
.lg button:focus-visible{outline:2px solid var(--accent)}
.lg i{width:10px;height:10px;border-radius:3px;flex:none}
.lg span{flex:1}.lg em{font-style:normal;color:var(--muted)}
.dim .seg:not(.act){opacity:.35}
.seg.act{stroke-width:18}
@media (prefers-reduced-motion:reduce){.seg{transition:none}}
</style></head>
<body><div class="stage">
<div class="wrap">
<div class="ring"><svg viewBox="0 0 100 100" id="svg" aria-hidden="true"></svg><div class="ctr" id="ctr" aria-live="polite"><b>12.4k</b><small>visits</small></div></div>
<ul class="lg" id="lg"></ul>
</div>
</div>
<script>
const D=[['Direct',42,'#171717'],['Search',31,'#6f6f6f'],['Social',18,'#a3a3a3'],['Email',9,'#d4d4d4']],C=2*Math.PI*40,svg=document.getElementById('svg'),lg=document.getElementById('lg'),ctr=document.getElementById('ctr'),wrap=svg.closest('.wrap');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;let off=0,pin=-1;const segs=[],btns=[];
D.forEach(([n,v,c],i)=>{const len=C*v/100,s=(svg.insertAdjacentHTML('beforeend','<circle class="seg" cx="50" cy="50" r="40" stroke="'+c+'"></circle>'),svg.lastElementChild);
s.setAttribute('stroke-dashoffset',-off);s.setAttribute('stroke-dasharray',reduce?(len-2)+' '+(C-len+2):'0 '+C);segs.push(s);
if(!reduce)requestAnimationFrame(()=>requestAnimationFrame(()=>s.setAttribute('stroke-dasharray',(len-2)+' '+(C-len+2))));off+=len;
const li=document.createElement('li');li.innerHTML='<button type="button" aria-pressed="false"><i style="background:'+c+'"></i><span>'+n+'</span><em>'+v+'%</em></button>';lg.append(li);btns.push(li.firstChild);
const on=()=>hl(i),offf=()=>hl(pin);[li.firstChild,s].forEach(el=>{el.addEventListener('pointerenter',on);el.addEventListener('pointerleave',offf);});
li.firstChild.addEventListener('focus',on);li.firstChild.addEventListener('blur',offf);
const pinIt=()=>{pin=pin===i?-1:i;btns.forEach((b,k)=>b.setAttribute('aria-pressed',String(k===pin)));hl(pin);};li.firstChild.addEventListener('click',pinIt);s.addEventListener('click',pinIt);});
function hl(i){segs.forEach((s,k)=>s.classList.toggle('act',k===i));wrap.classList.toggle('dim',i>=0);ctr.innerHTML=i<0?'<b>12.4k</b><small>visits</small>':'<b>'+D[i][1]+'%</b><small>'+D[i][0]+'</small>';}
</script>
</body></html>