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.
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
- Multi Line Chart: you need to compare each part exactly
- Streamgraph: you have many parts and care more about the flow than exact numbers
- Stacked Bar Chart: you only have a few points in time
What you get in this template
- Four smooth layers stacked with a running total
- A crosshair that lists every device with its share and the total
- Click a device in the key to hide it and restack the rest
- Dark theme with bright, easy to tell apart colors
- Layers reveal from left to right on load
- Arrow key support and a data table with totals
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
- Download the file. Click Download HTML file above. You get one file called stacked-area-chart.html with everything inside.
- 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.
- 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.
- 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

011. Line Chart
Example: new members each month for a yoga studio app
012. Multi Line Chart
Example: average home prices in three neighborhoods over ten years
013. Area Chart
Example: solar power made each day by a family home
015. Streamgraph
Example: weekly listening hours by genre on a radio app
016. Step Line Chart
Example: the interest rate on a savings account from 2021 to 2026
017. Sparkline
Example: daily sales for six products in a shop dashboard
018. Candlestick Chart
Example: daily share prices and volume for a made up robotics company
019. Slope Chart
Example: guest ratings for eight hotels in 2025 and 2026