Skip to content
IndexRequest a UIAboutContact

Line Chart with Crosshair

A line chart with a crosshair that snaps to the nearest point and reads out its value.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
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

HTML
<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>

Related patterns

Interactive Bar Chart

↗
Interactive Bar ChartData Visualization

Sparkline Trend Rows

↗
Sparkline Trend RowsData Visualization

Range Slider

↗
Range SliderInputs & Forms