StealThis .dev

Stacked Bar Chart

A stacked bar chart comparing year-over-year data by month. Features animated bars, legend, responsive layout, and configurable datasets with a clean corporate dashboard aesthetic.

Open in Lab
vanilla-js svg
Targets: JS HTML

Code

Features

  • Stacked bars — two series per month rendered as vertically stacked SVG rects
  • Animated bars — scale-in animation from bottom on mount
  • Legend — top-right legend showing both year series
  • Y-axis formatting — monetary values with $ prefix and dot separators
  • Responsive — ResizeObserver redraws on container resize
  • Hover tooltip — shows month, year, and formatted value on hover
  • Light theme — clean corporate dashboard style with green palette

How it works

  1. Monthly data is defined as an array with two values (2024 and 2025) per month
  2. For each month two SVG <rect> elements are stacked — the bottom series renders first, and the top series sits on top
  3. Y-axis ticks are computed from the max stacked total and formatted as currency
  4. transform: scaleY() animates each bar from 0 to 1 with staggered delays
  5. A ResizeObserver triggers a full redraw when the container changes size