Bubble Scatter with Labels
A bubble chart with position for two values and bubble area for a third; hover or focus shows details.
AI prompt
Clean bubble chart of nine countries with GDP per person on the horizontal axis and life expectancy on the vertical axis, translucent blue circles whose area shows population, the largest two labelled directly, one bubble highlighted with a thin dark outline and a small label card, light grey grid lines and axis titles, a size legend with three sample circles, flat vector style, calm and readable. Negative: 3D, gradients, rainbow colours, text errors, clutter. Reduced motion: static chart.About this pattern
Definition
A bubble chart adds a third number to a scatter plot by changing the size of each dot. Size must follow area, not radius, or big values look far too large. Labels appear on hover and focus, and the biggest bubbles can be labelled directly so the chart can be read without interaction.
Purpose & UX principles
- Map the third value to area: radius equals the square root of the value times a constant.
- Draw big bubbles first so small ones stay visible on top, and use some transparency.
- Label axes with units and add light grid lines.
- Make each bubble focusable with an aria-label that reads all three values.
Interaction states
- Two datasets; hover, focus or tap bubbles.
Motion specification
- Bubbles grow into place over 400ms when data changes.
- Reduced motion: no growth.
Responsive behaviour
- Overlapping bubbles hide data; use transparency and consider jitter or filters.
- Provide a table or text summary of the values.
Code
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Data"><button data-p="0" aria-pressed="true">Countries</button><button data-p="1" aria-pressed="false">Products</button></div><svg class="ch" id="ch" viewBox="0 0 330 160" role="group" aria-label="Bubble chart"></svg><p class="out" id="out" role="status">Hover or focus a bubble.</p></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}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.ch{width:100%;height:auto;border:1px solid var(--line);border-radius:12px;background:#fff}.ch circle.bb{fill:#2f6bff;fill-opacity:.38;stroke:#2f6bff;stroke-width:1;cursor:pointer;transition:r .4s,cx .4s,cy .4s}.ch circle.bb:hover,.ch circle.bb:focus{fill-opacity:.7;stroke:#171717;stroke-width:1.6;outline:none}.ch text{font:9px system-ui,sans-serif;fill:#666}.ch .t{font-weight:700;fill:#171717}.out{font-size:12px;color:var(--muted);min-height:16px;margin:0}@media (prefers-reduced-motion:reduce){.ch circle.bb{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const D=[{x:'Income ($k)',y:'Life expectancy',z:'Population (m)',xr:[0,80],yr:[55,85],p:[['USA',65,79,330],['India',2.5,70,1400],['China',12,78,1410],['Brazil',9,75,214],['Nigeria',2,54,210],['Japan',40,85,125],['Germany',50,81,83],['Egypt',3.8,72,104],['Chile',15,80,19]]},{x:'Price ($)',y:'Rating',z:'Units sold (k)',xr:[0,100],yr:[3,5],p:[['Basic',12,3.6,950],['Plus',29,4.1,610],['Pro',58,4.5,240],['Max',90,4.7,60],['Lite',6,3.2,820],['Duo',44,4.0,400],['Studio',75,4.4,120]]}],ch=document.getElementById('ch'),out=document.getElementById('out'),bs=[...document.querySelectorAll('#pre button')];let p=0;
function up(){const d=D[p],W=330,H=160,L=34,R=8,T=8,B=26,mx=Math.max(...d.p.map(a=>a[3])),sx=v=>L+(v-d.xr[0])/(d.xr[1]-d.xr[0])*(W-L-R),sy=v=>H-B-(v-d.yr[0])/(d.yr[1]-d.yr[0])*(H-B-T),rr=z=>3+Math.sqrt(z/mx)*17;let h='';
for(let i=0;i<=4;i++){const xv=d.xr[0]+(d.xr[1]-d.xr[0])*i/4,yv=d.yr[0]+(d.yr[1]-d.yr[0])*i/4;h+='<line x1="'+sx(xv)+'" x2="'+sx(xv)+'" y1="'+T+'" y2="'+(H-B)+'" stroke="#eee"/><line x1="'+L+'" x2="'+(W-R)+'" y1="'+sy(yv)+'" y2="'+sy(yv)+'" stroke="#eee"/><text x="'+sx(xv)+'" y="'+(H-B+10)+'" text-anchor="middle">'+Math.round(xv)+'</text><text x="'+(L-4)+'" y="'+(sy(yv)+3)+'" text-anchor="end">'+(yv%1?yv.toFixed(1):yv)+'</text>';}
h+='<text x="'+(W/2)+'" y="'+(H-3)+'" text-anchor="middle" class="t">'+d.x+'</text><text transform="translate(8 '+(H/2)+') rotate(-90)" text-anchor="middle" class="t">'+d.y+'</text>';
[...d.p].sort((a,b)=>b[3]-a[3]).forEach((a,i)=>{h+='<circle class="bb" tabindex="0" cx="'+sx(a[1])+'" cy="'+sy(a[2])+'" r="'+rr(a[3])+'" aria-label="'+a[0]+': '+d.x+' '+a[1]+', '+d.y+' '+a[2]+', '+d.z+' '+a[3]+'" data-n="'+a[0]+'"/>';if(i<2)h+='<text x="'+sx(a[1])+'" y="'+(sy(a[2])+3)+'" text-anchor="middle" class="t" pointer-events="none">'+a[0]+'</text>';});
ch.innerHTML=h;ch.querySelectorAll('.bb').forEach(c=>{const f=()=>{out.textContent=c.getAttribute('aria-label')+'.';};c.addEventListener('mouseenter',f);c.addEventListener('focus',f);c.addEventListener('click',f);});out.textContent='Hover or focus a bubble. Bubble area shows '+d.z+'.';bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));up();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>Bubble Scatter with Labels — 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}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.ch{width:100%;height:auto;border:1px solid var(--line);border-radius:12px;background:#fff}.ch circle.bb{fill:#2f6bff;fill-opacity:.38;stroke:#2f6bff;stroke-width:1;cursor:pointer;transition:r .4s,cx .4s,cy .4s}.ch circle.bb:hover,.ch circle.bb:focus{fill-opacity:.7;stroke:#171717;stroke-width:1.6;outline:none}.ch text{font:9px system-ui,sans-serif;fill:#666}.ch .t{font-weight:700;fill:#171717}.out{font-size:12px;color:var(--muted);min-height:16px;margin:0}@media (prefers-reduced-motion:reduce){.ch circle.bb{transition:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Data"><button data-p="0" aria-pressed="true">Countries</button><button data-p="1" aria-pressed="false">Products</button></div><svg class="ch" id="ch" viewBox="0 0 330 160" role="group" aria-label="Bubble chart"></svg><p class="out" id="out" role="status">Hover or focus a bubble.</p></div>
</div>
<script>
const D=[{x:'Income ($k)',y:'Life expectancy',z:'Population (m)',xr:[0,80],yr:[55,85],p:[['USA',65,79,330],['India',2.5,70,1400],['China',12,78,1410],['Brazil',9,75,214],['Nigeria',2,54,210],['Japan',40,85,125],['Germany',50,81,83],['Egypt',3.8,72,104],['Chile',15,80,19]]},{x:'Price ($)',y:'Rating',z:'Units sold (k)',xr:[0,100],yr:[3,5],p:[['Basic',12,3.6,950],['Plus',29,4.1,610],['Pro',58,4.5,240],['Max',90,4.7,60],['Lite',6,3.2,820],['Duo',44,4.0,400],['Studio',75,4.4,120]]}],ch=document.getElementById('ch'),out=document.getElementById('out'),bs=[...document.querySelectorAll('#pre button')];let p=0;
function up(){const d=D[p],W=330,H=160,L=34,R=8,T=8,B=26,mx=Math.max(...d.p.map(a=>a[3])),sx=v=>L+(v-d.xr[0])/(d.xr[1]-d.xr[0])*(W-L-R),sy=v=>H-B-(v-d.yr[0])/(d.yr[1]-d.yr[0])*(H-B-T),rr=z=>3+Math.sqrt(z/mx)*17;let h='';
for(let i=0;i<=4;i++){const xv=d.xr[0]+(d.xr[1]-d.xr[0])*i/4,yv=d.yr[0]+(d.yr[1]-d.yr[0])*i/4;h+='<line x1="'+sx(xv)+'" x2="'+sx(xv)+'" y1="'+T+'" y2="'+(H-B)+'" stroke="#eee"/><line x1="'+L+'" x2="'+(W-R)+'" y1="'+sy(yv)+'" y2="'+sy(yv)+'" stroke="#eee"/><text x="'+sx(xv)+'" y="'+(H-B+10)+'" text-anchor="middle">'+Math.round(xv)+'</text><text x="'+(L-4)+'" y="'+(sy(yv)+3)+'" text-anchor="end">'+(yv%1?yv.toFixed(1):yv)+'</text>';}
h+='<text x="'+(W/2)+'" y="'+(H-3)+'" text-anchor="middle" class="t">'+d.x+'</text><text transform="translate(8 '+(H/2)+') rotate(-90)" text-anchor="middle" class="t">'+d.y+'</text>';
[...d.p].sort((a,b)=>b[3]-a[3]).forEach((a,i)=>{h+='<circle class="bb" tabindex="0" cx="'+sx(a[1])+'" cy="'+sy(a[2])+'" r="'+rr(a[3])+'" aria-label="'+a[0]+': '+d.x+' '+a[1]+', '+d.y+' '+a[2]+', '+d.z+' '+a[3]+'" data-n="'+a[0]+'"/>';if(i<2)h+='<text x="'+sx(a[1])+'" y="'+(sy(a[2])+3)+'" text-anchor="middle" class="t" pointer-events="none">'+a[0]+'</text>';});
ch.innerHTML=h;ch.querySelectorAll('.bb').forEach(c=>{const f=()=>{out.textContent=c.getAttribute('aria-label')+'.';};c.addEventListener('mouseenter',f);c.addEventListener('focus',f);c.addEventListener('click',f);});out.textContent='Hover or focus a bubble. Bubble area shows '+d.z+'.';bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));up();
</script>
</body></html>