webdevpuneet.com

Stacked Area Chart — SVG Multi-Series Area Graph

Stacked Area Chart · Charts · Plain HTML, CSS & JS · Live preview

CategoryCharts

Copy & paste

Source Code

<div class="sa-card">
  <div class="sa-head"><h3>Traffic by source</h3><span class="sa-total" id="saTotal"></span></div>
  <div class="sa-wrap">
    <svg class="sa-svg" id="saSvg" viewBox="0 0 320 180" preserveAspectRatio="none" aria-label="Stacked area chart"></svg>
    <div class="sa-tip" id="saTip" hidden></div>
  </div>
  <div class="sa-legend" id="saLegend"></div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Correct stacking
Cumulative lower/upper boundaries per series.
Total-based scale
Y-axis fits the tallest stacked column.
Responsive SVG
viewBox + preserveAspectRatio=none stretches to any width.
Gridlines
Reference levels for reading values.
Column hit areas
Invisible columns select the nearest time point reliably.
Shared tooltip
Lists every series value plus the stacked total.
Data-driven legend
Legend and tooltip derive from the series array.
No library
Pure HTML/CSS/JS/SVG — no chart dependency.

About this UI Snippet

Stacked Area Chart — Cumulative Multi-Series SVG Areas with Hover Tooltip

Screenshot of the Stacked Area Chart snippet rendered live

A stacked area chart shows how several series add up to a total over time, with each band sitting on top of the one below — ideal for traffic by source, revenue by product, or usage by tier. This snippet renders one from a data array in pure SVG, with gridlines, a hover guide, and a per-series tooltip, in plain HTML, CSS, and vanilla JavaScript with no charting library.

Stacking, done correctly

The core is the cumulative stack: for each series, the band's lower boundary is the running total of all series beneath it, and its upper boundary is that total plus the series' own values. The script walks the series keeping a lower array, computes upper, and builds a closed area path that traces the top edge left-to-right then the bottom edge back. Series are drawn in order so each sits cleanly on its predecessor, and the y-scale is based on the maximum stacked total so the tallest column just fits.

Scalable SVG with responsive width

The chart uses a fixed viewBox with preserveAspectRatio="none", so it stretches to its container's width while the math stays in tidy 0–320 coordinates. Horizontal gridlines give the eye reference levels. Everything is vector, so it's crisp at any size and weighs nothing.

Column hover with a shared guide

Hovering reveals a dashed vertical guide and a tooltip listing every series' value at that time point plus the stacked total — the read you actually want from a stacked chart ("what made up Friday?"). Rather than per-shape hit-testing, the chart lays invisible rectangle "columns" spanning the midpoints between points, so the nearest time index is selected reliably anywhere in that column, even over thin bands.

Tooltip positioning

The tooltip is an HTML element positioned over the SVG by converting chart coordinates to pixels using the SVG's rendered size, so it stays anchored to the hovered column at any width. Color chips in the tooltip tie each value back to its band.

Data-driven and portable

Series are an array of { name, color, data }, so adding a series or swapping the numbers needs no code changes — the stack, scale, legend, and tooltip all derive from the data. It's a compact, dependency-free reference for the stacked-area pattern that you'd otherwise pull in a chart library for.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA four-series stacked area chart renders with a legend.
  2. 2
    Use your dataEdit the SERIES array of { name, color, data } and the LABELS.
  3. 3
    Hover the chartA guide and tooltip show each series and the total at that point.
  4. 4
    Read the stackBand heights show each source's contribution to the total.
  5. 5
    RestyleChange colors, gridline count, or chart height.
  6. 6
    ResizeIt stretches to its container via preserveAspectRatio.

Real-world uses

Common Use Cases

Traffic by source
Break visits down by source over time beside an area chart, so total traffic and each channel's share are visible together.
Revenue by product
Show how product lines add up to total revenue, with each band stacked cumulatively on the one below so the top edge is always the true total.
Usage by plan tier
Visualise plan mix next to a quota usage meter, with the y-axis fitted to the tallest stacked column.
Dashboards with a trend line
Pair with a multi-line chart for trends, using the hover guide and per-series tooltip to read each source's exact value at a point in time.
Resource allocation
Stack categories that sum to a fixed capacity, so shifts in allocation are visible while the total remains the visual constraint.

Got questions?

Frequently Asked Questions

The script keeps a running lower-boundary array, starting at zero. For each series it computes the upper boundary as lower plus that series' values, draws a closed area between the two boundaries, then sets lower to upper for the next series. This makes each band sit exactly on top of the ones below, and the y-scale uses the maximum stacked total so the tallest column fits.

Hit-testing the actual area shapes is unreliable — thin bands are hard to hover, and you want the same time index whether the cursor is over the top or bottom of the stack. Laying transparent rectangles that span the midpoints between points means hovering anywhere in a column selects that time index, which is how production charts handle tooltips.

The SVG uses a fixed viewBox (0 0 320 180) with preserveAspectRatio="none", so it scales to its container width while the drawing math stays in simple coordinates. The HTML tooltip is positioned by converting chart coordinates to pixels using the SVG's measured size, so it stays anchored to the right column at any width.

Edit the SERIES array — each entry is { name, color, data }, with data an array of values aligned to the LABELS. The stacking, scale, legend, and tooltip all derive from this array, so adding a series or changing numbers needs no other code. Keep every series' data the same length as the labels.

Compute the stacked paths from your series in a memo/computed and render them as SVG path elements. Track the hovered index in state for the guide and tooltip, driven by pointer handlers on the column rects. Libraries like Recharts or visx can replace the math, but the data shape stays the same. Tailwind users swap the classes for utilities.