StealThis .dev

Pie / Donut Chart

An animated SVG pie and donut chart with legend, interactive slice highlighting on hover, and smooth arc transitions. Toggle between pie and donut mode with a button click.

Open in Lab
vanilla-js svg react tailwind vue svelte
Targets: TS JS HTML React Vue Svelte

Code

Features

  • Pie & donut — toggle between solid pie and hollow donut
  • Animated arcs — slices animate in with a stroke-dasharray reveal
  • Hover highlight — slice scales and lifts on hover
  • Legend — color-coded legend with label and percentage
  • Center label — donut mode shows total value in center

How it works

  1. Data values are converted to radian angles summed across slices
  2. SVG <path> arcs are computed with polarToCartesian() helper
  3. On hover, the active slice gets a transform: scale(1.05) via CSS
  4. Slice click dispatches a custom sliceSelect event with the data payload