Line Chart with Crosshair
A line chart with a crosshair that snaps to the nearest point and reads out its value.
AI prompt
Draw a 300x150px SVG line chart of 12 monthly values (Jan to Dec: 22, 28, 26, 35, 41, 38, 47, 52, 49, 58, 63, 71) with a 2px dark line, light horizontal gridlines at three levels and month labels for Jan, Apr, Jul and Oct. On load animate the line drawing using stroke-dasharray and stroke-dashoffset over 900ms ease-out. On pointermove (and while dragging on touch with touch-action pan-y), find the nearest data index by x position and show a vertical guide line, a 5px dot on the line and a readout above the chart with "Month: value". When the pointer leaves, show the latest value. The SVG is focusable (tabindex 0, role group, aria-label "Monthly sales"): ArrowLeft and ArrowRight move the active index, Home and End jump to the ends, and the readout lives in an aria-live polite region. Under prefers-reduced-motion skip the draw animation.About this pattern
Definition
A line drawn through monthly values. As the pointer moves, a vertical guide and a dot snap to the nearest data point and a readout shows its month and value.
Purpose & UX principles
- Lets people read exact values from a continuous shape without gridline clutter.
- Snapping to real points avoids showing interpolated numbers that do not exist.
- Left and Right arrow keys move the same crosshair, and the readout is announced politely.
Interaction states
- No selection (readout shows the latest value), hover or drag (crosshair active), keyboard focus (ring on the chart).
Motion specification
- The line draws itself on load with stroke-dashoffset over 900ms ease-out.
- The crosshair follows the pointer directly; the dot moves with a 60ms transition.
- Reduced motion: the line is drawn immediately.
Responsive behaviour
- The SVG scales with its container; points are mapped by percentage of the width.
- On touch, dragging across the chart moves the crosshair.
Code
<div class="box">
<p class="read" id="read" aria-live="polite"></p>
<svg id="svg" viewBox="0 0 300 130" tabindex="0" role="group" aria-label="Monthly sales line chart. Use left and right arrow keys."></svg>
</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%)}
.read{font-size:13px;font-weight:600;margin-bottom:6px;min-height:20px}
.read span{color:var(--muted);font-weight:400}
svg{display:block;width:100%;height:auto;touch-action:pan-y;cursor:crosshair;border-radius:8px}
svg:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
.grid{stroke:var(--line);stroke-width:1}
.ln{fill:none;stroke:#171717;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:var(--len);stroke-dashoffset:0;animation:dr .9s ease-out}
@keyframes dr{from{stroke-dashoffset:var(--len)}}
.g{stroke:#171717;stroke-width:1;stroke-dasharray:3 3;opacity:0;transition:opacity .15s}
.dot{fill:#171717;stroke:#fff;stroke-width:2;opacity:0;transition:opacity .15s,cx .06s,cy .06s}
.on .g,.on .dot{opacity:1}
.lb{font:10px sans-serif;fill:var(--muted);text-anchor:middle}
@media (prefers-reduced-motion:reduce){.ln{animation:none}.dot{transition:opacity .01s}}const M=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],V=[22,28,26,35,41,38,47,52,49,58,63,71];
const svg=document.getElementById('svg'),read=document.getElementById('read'),W=300,H=130,px=14,pt=10,pb=22,mx=80;
const X=i=>px+i*(W-2*px)/11,Y=v=>H-pb-(v/mx)*(H-pb-pt);
let h='';[0,40,80].forEach(v=>{h+='<line class="grid" x1="'+px+'" x2="'+(W-px)+'" y1="'+Y(v)+'" y2="'+Y(v)+'"/>';});
[0,3,6,9].forEach(i=>{h+='<text class="lb" x="'+X(i)+'" y="'+(H-6)+'">'+M[i]+'</text>';});
const pts=V.map((v,i)=>X(i).toFixed(1)+','+Y(v).toFixed(1)).join(' ');
h+='<polyline class="ln" id="ln" points="'+pts+'"/><line class="g" id="g" y1="'+pt+'" y2="'+(H-pb)+'"/><circle class="dot" id="dot" r="5"/>';
svg.innerHTML=h;const ln=document.getElementById('ln');ln.style.setProperty('--len',Math.ceil(ln.getTotalLength()));
const g=document.getElementById('g'),dot=document.getElementById('dot');let idx=11,active=false,spoke=false;
function show(i,on){idx=i;svg.classList.toggle('on',on);g.setAttribute('x1',X(i));g.setAttribute('x2',X(i));dot.setAttribute('cx',X(i));dot.setAttribute('cy',Y(V[i]));read.innerHTML=M[i]+': <span>'+V[i]+'k</span>';}
show(11,false);
const near=e=>{const r=svg.getBoundingClientRect(),x=(e.clientX-r.left)/r.width*W;return Math.max(0,Math.min(11,Math.round((x-px)/((W-2*px)/11))));};
svg.addEventListener('pointermove',e=>show(near(e),true));svg.addEventListener('pointerdown',e=>show(near(e),true));
svg.addEventListener('pointerleave',()=>{if(document.activeElement!==svg)show(11,false);});
svg.addEventListener('keydown',e=>{const m={ArrowLeft:Math.max(0,idx-1),ArrowRight:Math.min(11,idx+1),Home:0,End:11};if(e.key in m){e.preventDefault();show(m[e.key],true);}});
svg.addEventListener('blur',()=>show(11,false));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>Line Chart with Crosshair — 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%)}
.read{font-size:13px;font-weight:600;margin-bottom:6px;min-height:20px}
.read span{color:var(--muted);font-weight:400}
svg{display:block;width:100%;height:auto;touch-action:pan-y;cursor:crosshair;border-radius:8px}
svg:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
.grid{stroke:var(--line);stroke-width:1}
.ln{fill:none;stroke:#171717;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:var(--len);stroke-dashoffset:0;animation:dr .9s ease-out}
@keyframes dr{from{stroke-dashoffset:var(--len)}}
.g{stroke:#171717;stroke-width:1;stroke-dasharray:3 3;opacity:0;transition:opacity .15s}
.dot{fill:#171717;stroke:#fff;stroke-width:2;opacity:0;transition:opacity .15s,cx .06s,cy .06s}
.on .g,.on .dot{opacity:1}
.lb{font:10px sans-serif;fill:var(--muted);text-anchor:middle}
@media (prefers-reduced-motion:reduce){.ln{animation:none}.dot{transition:opacity .01s}}
</style></head>
<body><div class="stage">
<div class="box">
<p class="read" id="read" aria-live="polite"></p>
<svg id="svg" viewBox="0 0 300 130" tabindex="0" role="group" aria-label="Monthly sales line chart. Use left and right arrow keys."></svg>
</div>
</div>
<script>
const M=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],V=[22,28,26,35,41,38,47,52,49,58,63,71];
const svg=document.getElementById('svg'),read=document.getElementById('read'),W=300,H=130,px=14,pt=10,pb=22,mx=80;
const X=i=>px+i*(W-2*px)/11,Y=v=>H-pb-(v/mx)*(H-pb-pt);
let h='';[0,40,80].forEach(v=>{h+='<line class="grid" x1="'+px+'" x2="'+(W-px)+'" y1="'+Y(v)+'" y2="'+Y(v)+'"/>';});
[0,3,6,9].forEach(i=>{h+='<text class="lb" x="'+X(i)+'" y="'+(H-6)+'">'+M[i]+'</text>';});
const pts=V.map((v,i)=>X(i).toFixed(1)+','+Y(v).toFixed(1)).join(' ');
h+='<polyline class="ln" id="ln" points="'+pts+'"/><line class="g" id="g" y1="'+pt+'" y2="'+(H-pb)+'"/><circle class="dot" id="dot" r="5"/>';
svg.innerHTML=h;const ln=document.getElementById('ln');ln.style.setProperty('--len',Math.ceil(ln.getTotalLength()));
const g=document.getElementById('g'),dot=document.getElementById('dot');let idx=11,active=false,spoke=false;
function show(i,on){idx=i;svg.classList.toggle('on',on);g.setAttribute('x1',X(i));g.setAttribute('x2',X(i));dot.setAttribute('cx',X(i));dot.setAttribute('cy',Y(V[i]));read.innerHTML=M[i]+': <span>'+V[i]+'k</span>';}
show(11,false);
const near=e=>{const r=svg.getBoundingClientRect(),x=(e.clientX-r.left)/r.width*W;return Math.max(0,Math.min(11,Math.round((x-px)/((W-2*px)/11))));};
svg.addEventListener('pointermove',e=>show(near(e),true));svg.addEventListener('pointerdown',e=>show(near(e),true));
svg.addEventListener('pointerleave',()=>{if(document.activeElement!==svg)show(11,false);});
svg.addEventListener('keydown',e=>{const m={ArrowLeft:Math.max(0,idx-1),ArrowRight:Math.min(11,idx+1),Home:0,End:11};if(e.key in m){e.preventDefault();show(m[e.key],true);}});
svg.addEventListener('blur',()=>show(11,false));
</script>
</body></html>