Stacked Bars with Series Toggle
Stacked columns where legend buttons switch series on and off and the chart rescales smoothly.
AI prompt
Build a stacked column chart, 300x170px, with five quarters (Q1 to Q5) and three series (Product, Services, Support) using three shades (#171717, #8c8c87, #d4d4d4). Compute each column as a flex column of segments whose heights are the series value divided by the largest visible column total, so hiding a series rescales the whole chart; animate segment heights over 400ms cubic-bezier(.2,.7,.2,1). Below the chart place three legend toggle buttons (swatch plus name) with aria-pressed; pressing one hides or shows that series, and the last visible series cannot be turned off (its button gets aria-disabled and a short note). Each column has an aria-label listing the visible values, e.g. "Q3: Product 40, Services 22". Hovering a segment shows its value in a small readout line under the chart. Under prefers-reduced-motion remove transitions.About this pattern
Definition
Columns divided into stacked segments, one per series. Legend buttons show or hide a series, and the remaining segments animate to the new proportions.
Purpose & UX principles
- Lets people isolate one series to compare it across columns.
- Toggle buttons use
aria-pressedand always keep at least one series on, so the chart is never empty. - Segment values are in the accessible name of each column, so the data is available without the visual.
Interaction states
- All series on, some off (button looks inactive), only one series left (its button is locked).
Motion specification
- Heights and offsets animate 400ms cubic-bezier(.2,.7,.2,1) when a series toggles or the scale changes.
- Reduced motion: instant update.
Responsive behaviour
- Columns share the width; the legend wraps under the chart on narrow widths.
Code
<div class="box">
<div class="ch" id="ch" role="group" aria-label="Revenue by quarter"></div>
<p class="read" id="read" aria-live="polite">Hover a segment</p>
<div class="lg" id="lg"></div>
</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}
.box{width:min(300px,100%)}
.ch{display:flex;align-items:flex-end;gap:10px;height:130px;border-bottom:1px solid var(--line)}
.col{flex:1;height:100%;display:flex;flex-direction:column-reverse;position:relative;padding-bottom:0}
.sg{width:100%;transition:height .4s cubic-bezier(.2,.7,.2,1);cursor:default}
.sg:first-child{border-radius:0}
.sg:hover{filter:brightness(.9)}
.qn{position:absolute;bottom:-18px;left:0;right:0;text-align:center;font-size:11px;color:var(--muted)}
.read{margin-top:24px;font-size:12px;color:var(--muted);min-height:16px}
.lg{display:flex;gap:6px;flex-wrap:wrap;margin-top:8px}
.lg button{display:inline-flex;align-items:center;gap:6px;height:28px;padding:0 10px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12px;cursor:pointer;transition:opacity .15s,border-color .15s}
.lg button[aria-pressed=false]{opacity:.45}
.lg button:hover{border-color:var(--accent)}
.lg button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.lg i{width:9px;height:9px;border-radius:2px;display:inline-block}
@media (prefers-reduced-motion:reduce){.sg{transition:none}}const S=[['Product','#171717',[40,46,40,58,64]],['Services','#8c8c87',[22,26,22,30,28]],['Support','#d4d4d4',[14,12,16,15,18]]],Q=['Q1','Q2','Q3','Q4','Q5'];
const ch=document.getElementById('ch'),lg=document.getElementById('lg'),read=document.getElementById('read');const on=S.map(()=>true);
const cols=Q.map((q,qi)=>{const c=document.createElement('div');c.className='col';c.innerHTML='<span class="qn" aria-hidden="true">'+q+'</span>';
const segs=S.map(([n,col,v],si)=>{const s=document.createElement('div');s.className='sg';s.style.background=col;s.addEventListener('pointerenter',()=>{read.textContent=q+' · '+n+': '+v[qi];});c.append(s);return s;});ch.append(c);return {c,segs};});
function render(){const tot=Q.map((_,qi)=>S.reduce((a,s,si)=>a+(on[si]?s[2][qi]:0),0)),mx=Math.max(...tot);
cols.forEach(({c,segs},qi)=>{segs.forEach((s,si)=>{s.style.height=on[si]?(S[si][2][qi]/mx*100)+'%':'0%';});
c.setAttribute('aria-label',Q[qi]+': '+S.filter((_,si)=>on[si]).map(s=>s[0]+' '+s[2][qi]).join(', '));});
[...lg.children].forEach((b,si)=>{b.setAttribute('aria-pressed',String(on[si]));b.setAttribute('aria-disabled',String(on[si]&&on.filter(Boolean).length===1));});}
S.forEach(([n,col],si)=>{const b=document.createElement('button');b.type='button';b.innerHTML='<i style="background:'+col+'"></i>'+n;
b.addEventListener('click',()=>{if(on[si]&&on.filter(Boolean).length===1){read.textContent='At least one series stays visible.';return;}on[si]=!on[si];read.textContent='';render();});lg.append(b);});
render();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>Stacked Bars with Series Toggle — 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}}
.box{width:min(300px,100%)}
.ch{display:flex;align-items:flex-end;gap:10px;height:130px;border-bottom:1px solid var(--line)}
.col{flex:1;height:100%;display:flex;flex-direction:column-reverse;position:relative;padding-bottom:0}
.sg{width:100%;transition:height .4s cubic-bezier(.2,.7,.2,1);cursor:default}
.sg:first-child{border-radius:0}
.sg:hover{filter:brightness(.9)}
.qn{position:absolute;bottom:-18px;left:0;right:0;text-align:center;font-size:11px;color:var(--muted)}
.read{margin-top:24px;font-size:12px;color:var(--muted);min-height:16px}
.lg{display:flex;gap:6px;flex-wrap:wrap;margin-top:8px}
.lg button{display:inline-flex;align-items:center;gap:6px;height:28px;padding:0 10px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12px;cursor:pointer;transition:opacity .15s,border-color .15s}
.lg button[aria-pressed=false]{opacity:.45}
.lg button:hover{border-color:var(--accent)}
.lg button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.lg i{width:9px;height:9px;border-radius:2px;display:inline-block}
@media (prefers-reduced-motion:reduce){.sg{transition:none}}
</style></head>
<body><div class="stage">
<div class="box">
<div class="ch" id="ch" role="group" aria-label="Revenue by quarter"></div>
<p class="read" id="read" aria-live="polite">Hover a segment</p>
<div class="lg" id="lg"></div>
</div>
</div>
<script>
const S=[['Product','#171717',[40,46,40,58,64]],['Services','#8c8c87',[22,26,22,30,28]],['Support','#d4d4d4',[14,12,16,15,18]]],Q=['Q1','Q2','Q3','Q4','Q5'];
const ch=document.getElementById('ch'),lg=document.getElementById('lg'),read=document.getElementById('read');const on=S.map(()=>true);
const cols=Q.map((q,qi)=>{const c=document.createElement('div');c.className='col';c.innerHTML='<span class="qn" aria-hidden="true">'+q+'</span>';
const segs=S.map(([n,col,v],si)=>{const s=document.createElement('div');s.className='sg';s.style.background=col;s.addEventListener('pointerenter',()=>{read.textContent=q+' · '+n+': '+v[qi];});c.append(s);return s;});ch.append(c);return {c,segs};});
function render(){const tot=Q.map((_,qi)=>S.reduce((a,s,si)=>a+(on[si]?s[2][qi]:0),0)),mx=Math.max(...tot);
cols.forEach(({c,segs},qi)=>{segs.forEach((s,si)=>{s.style.height=on[si]?(S[si][2][qi]/mx*100)+'%':'0%';});
c.setAttribute('aria-label',Q[qi]+': '+S.filter((_,si)=>on[si]).map(s=>s[0]+' '+s[2][qi]).join(', '));});
[...lg.children].forEach((b,si)=>{b.setAttribute('aria-pressed',String(on[si]));b.setAttribute('aria-disabled',String(on[si]&&on.filter(Boolean).length===1));});}
S.forEach(([n,col],si)=>{const b=document.createElement('button');b.type='button';b.innerHTML='<i style="background:'+col+'"></i>'+n;
b.addEventListener('click',()=>{if(on[si]&&on.filter(Boolean).length===1){read.textContent='At least one series stays visible.';return;}on[si]=!on[si];read.textContent='';render();});lg.append(b);});
render();
</script>
</body></html>