Interactive Bar Chart
A bar chart that grows in, dims the other bars on hover and shows the exact value.
AI prompt
Build a 300x170px bar chart for the days Mon to Sun with values 32, 48, 41, 66, 58, 27 and 19 visits. Use flex columns, a baseline line and day labels in 11px secondary text. Each bar is a button element with an aria-label like "Thursday: 66 visits", rounded top corners and a mid-grey fill (#b9b9b3). On load animate each bar from scaleY(0) with transform-origin bottom to full height over 600ms cubic-bezier(.2,.7,.2,1) with a 50ms stagger. On hover and focus-visible set the bar to #171717, reduce the opacity of the other bars to 0.45 over 150ms and show a small tooltip above the bar with the value. Taps toggle the tooltip on touch devices. Scale bar heights to the maximum value. Keep a 2px focus ring. Under prefers-reduced-motion skip the grow animation.About this pattern
Definition
A chart of seven bars whose height encodes a value. Hovering or focusing one bar highlights it, dims the rest and shows its exact number in a tooltip.
Purpose & UX principles
- Bars make comparison quick; the tooltip supplies the exact value so the chart does not need clutter.
- Each bar is focusable with an
aria-labelsuch as "Wednesday: 48 visits", so keyboard and screen-reader users get the same data. - Dimming the others focuses attention without hiding context.
Interaction states
- Rest, hover or focus (bar dark, others dimmed, tooltip), selected by tap, entering (growing).
Motion specification
- On load each bar grows from the baseline over 600ms cubic-bezier(.2,.7,.2,1) with a 50ms stagger.
- Highlight and dim: 150ms ease.
- Reduced motion: bars appear at full height.
Responsive behaviour
- The chart fills its container and bars share the width equally.
- On touch, tapping a bar shows its tooltip until the next tap.
Code
<div class="ch" id="ch" role="group" aria-label="Weekly visits"></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}
.ch{position:relative;display:flex;align-items:flex-end;gap:8px;width:min(300px,100%);height:170px;padding:26px 6px 22px;border-bottom:1px solid var(--line)}
.col{position:relative;flex:1;height:100%;display:flex;flex-direction:column;justify-content:flex-end;align-items:stretch}
.bar{display:block;border:0;padding:0;width:100%;background:#b9b9b3;border-radius:6px 6px 0 0;cursor:pointer;transform-origin:bottom;animation:grow .6s cubic-bezier(.2,.7,.2,1) backwards;animation-delay:calc(var(--i) * 50ms);transition:background .15s,opacity .15s}
.bar:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.ch:has(.bar:hover) .bar:not(:hover),.ch:has(.bar:focus-visible) .bar:not(:focus-visible),.ch.tap .bar:not(.on){opacity:.45}
.bar:hover,.bar:focus-visible,.bar.on{background:#171717}
.day{position:absolute;bottom:-20px;left:0;right:0;text-align:center;font-size:11px;color:var(--muted)}
.tip{position:absolute;bottom:calc(var(--h) + 6px);left:50%;transform:translateX(-50%);padding:2px 7px;border-radius:6px;background:#171717;color:#fff;font-size:11px;font-weight:600;white-space:nowrap;pointer-events:none;opacity:0;transition:opacity .15s}
.col:hover .tip,.col:has(.bar:focus-visible) .tip,.col:has(.bar.on) .tip{opacity:1}
@keyframes grow{from{transform:scaleY(0)}}
@media (prefers-reduced-motion:reduce){.bar{animation:none;transition:none}.tip{transition:none}}const d=[['Mon',32],['Tue',48],['Wed',41],['Thu',66],['Fri',58],['Sat',27],['Sun',19]],names={Mon:'Monday',Tue:'Tuesday',Wed:'Wednesday',Thu:'Thursday',Fri:'Friday',Sat:'Saturday',Sun:'Sunday'};
const ch=document.getElementById('ch'),max=Math.max(...d.map(x=>x[1]));
d.forEach(([k,v],i)=>{const c=document.createElement('div');c.className='col';const h=Math.round(v/max*100);c.style.setProperty('--h',h+'%');c.style.setProperty('--i',i);
c.innerHTML='<span class="tip" aria-hidden="true">'+v+'</span><button class="bar" style="height:'+h+'%;--i:'+i+'" aria-label="'+names[k]+': '+v+' visits"></button><span class="day" aria-hidden="true">'+k+'</span>';ch.append(c);});
ch.addEventListener('click',e=>{const b=e.target.closest('.bar');if(!b||matchMedia('(hover:hover)').matches)return;const was=b.classList.contains('on');ch.querySelectorAll('.bar.on').forEach(x=>x.classList.remove('on'));ch.classList.toggle('tap',!was);if(!was)b.classList.add('on');});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>Interactive Bar Chart — 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}}
.ch{position:relative;display:flex;align-items:flex-end;gap:8px;width:min(300px,100%);height:170px;padding:26px 6px 22px;border-bottom:1px solid var(--line)}
.col{position:relative;flex:1;height:100%;display:flex;flex-direction:column;justify-content:flex-end;align-items:stretch}
.bar{display:block;border:0;padding:0;width:100%;background:#b9b9b3;border-radius:6px 6px 0 0;cursor:pointer;transform-origin:bottom;animation:grow .6s cubic-bezier(.2,.7,.2,1) backwards;animation-delay:calc(var(--i) * 50ms);transition:background .15s,opacity .15s}
.bar:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.ch:has(.bar:hover) .bar:not(:hover),.ch:has(.bar:focus-visible) .bar:not(:focus-visible),.ch.tap .bar:not(.on){opacity:.45}
.bar:hover,.bar:focus-visible,.bar.on{background:#171717}
.day{position:absolute;bottom:-20px;left:0;right:0;text-align:center;font-size:11px;color:var(--muted)}
.tip{position:absolute;bottom:calc(var(--h) + 6px);left:50%;transform:translateX(-50%);padding:2px 7px;border-radius:6px;background:#171717;color:#fff;font-size:11px;font-weight:600;white-space:nowrap;pointer-events:none;opacity:0;transition:opacity .15s}
.col:hover .tip,.col:has(.bar:focus-visible) .tip,.col:has(.bar.on) .tip{opacity:1}
@keyframes grow{from{transform:scaleY(0)}}
@media (prefers-reduced-motion:reduce){.bar{animation:none;transition:none}.tip{transition:none}}
</style></head>
<body><div class="stage">
<div class="ch" id="ch" role="group" aria-label="Weekly visits"></div>
</div>
<script>
const d=[['Mon',32],['Tue',48],['Wed',41],['Thu',66],['Fri',58],['Sat',27],['Sun',19]],names={Mon:'Monday',Tue:'Tuesday',Wed:'Wednesday',Thu:'Thursday',Fri:'Friday',Sat:'Saturday',Sun:'Sunday'};
const ch=document.getElementById('ch'),max=Math.max(...d.map(x=>x[1]));
d.forEach(([k,v],i)=>{const c=document.createElement('div');c.className='col';const h=Math.round(v/max*100);c.style.setProperty('--h',h+'%');c.style.setProperty('--i',i);
c.innerHTML='<span class="tip" aria-hidden="true">'+v+'</span><button class="bar" style="height:'+h+'%;--i:'+i+'" aria-label="'+names[k]+': '+v+' visits"></button><span class="day" aria-hidden="true">'+k+'</span>';ch.append(c);});
ch.addEventListener('click',e=>{const b=e.target.closest('.bar');if(!b||matchMedia('(hover:hover)').matches)return;const was=b.classList.contains('on');ch.querySelectorAll('.bar.on').forEach(x=>x.classList.remove('on'));ch.classList.toggle('tap',!was);if(!was)b.classList.add('on');});
</script>
</body></html>