Home / Trends Over Time / Stacked Area Chart

Stacked Area Chart in HTML, CSS and JavaScript

A stacked area chart shows several amounts over time, piled on top of each other. This free template draws one with plain SVG and vanilla JavaScript, no chart library. The example shows hours watched each month on a streaming app, by device.

Open live demoDownload HTML fileView code on GitHub
Stacked Area Chart made with HTML and JavaScript showing hours watched each month on a streaming app, by device

What is a stacked area chart?

A stacked area chart shows several amounts over time, piled on top of each other. Each colored layer is one part, and the top edge of the whole stack is the total.

It shows two things at once: how the total changed, and how the mix changed inside it. In this example you can see total viewing peak in winter while phones slowly take a bigger share.

At a glance

Best for
A total over time and the parts that make it up
Data you need
A date and one number per part for each point
Skip it when
Comparing the middle layers exactly. Use lines.
Made with
HTML, CSS and vanilla JavaScript. No library.

When to use a stacked area chart

  • Viewing, visits or sales by device or channel
  • Energy use by source over the year
  • Revenue by product line each month
  • Team hours split by type of work

When to pick another chart

What you get in this template

How the code works

Here is a short version of the idea behind this stacked area chart. The full file adds the axis, labels, tooltips, sorting and resizing on top of it.

const months = 6;
const layers = [
  { color: '#ff5d73', values: [42, 45, 51, 55, 49, 46] },
  { color: '#ffb547', values: [28, 29, 31, 33, 34, 36] }
];
const x = i => 40 + i * 110, y = v => 280 - v / 100 * 260;
let below = Array(months).fill(0);

layers.forEach(l => {
  const top = below.map((b, i) => b + l.values[i]);
  const up = top.map((v, i) => x(i) + ',' + y(v));
  const down = below.map((v, i) => x(i) + ',' + y(v)).reverse();
  make('path', { d: 'M' + up.join('L') + 'L' + down.join('L') + 'Z', fill: l.color });
  below = top;
});

The example uses four tiny helpers that create SVG shapes. Put them above the code and it runs as is.

const svg = document.querySelector('svg');
function make(tag, attrs) {
  const n = document.createElementNS('http://www.w3.org/2000/svg', tag);
  for (const k in attrs) n.setAttribute(k, attrs[k]);
  svg.appendChild(n);
  return n;
}
function drawRect(x, y, width, height, fill) { return make('rect', { x, y, width, height, fill }); }
function drawCircle(cx, cy, r, fill) { return make('circle', { cx, cy, r, fill }); }
function drawLine(x1, y1, x2, y2, stroke, width, dash) {
  return make('line', { x1, y1, x2, y2, stroke, 'stroke-width': width || 1, 'stroke-dasharray': dash || 'none' });
}
function drawText(x, y, str, anchor) {
  const t = make('text', { x, y, 'text-anchor': anchor || 'start' });
  t.textContent = str;
  return t;
}

How to add it to your website

  1. Download the file. Click Download HTML file above. You get one file called stacked-area-chart.html with everything inside.
  2. Change the data. Open the file in any code editor and find the DATA list near the bottom. Replace the names and numbers with your own.
  3. Change the colors and text. Colors are at the top of the style tag. The title, subtitle and main point are plain HTML near the top of the body.
  4. Put it online. Upload the file to any host, such as GitHub Pages, Netlify or your own server, or paste it into your site. There is no build step.

Questions people ask

When should I use a stacked area chart?

Use it when the total matters and you also want to see how the parts change inside it over time, like viewing by device or energy by source.

Which layer should go at the bottom?

Put the largest or the most stable layer at the bottom. It sits on a flat baseline, so it is the only layer people can read accurately.

What is the difference between a stacked area chart and a streamgraph?

A stacked area chart builds up from zero, so you can read the total on the axis. A streamgraph centers the layers on a middle line, which looks more fluid but hides the exact total.

How many layers can a stacked area chart have?

Keep it to five or fewer. Thin layers in the middle of a tall stack become impossible to read.

More trends over time