Stacked area
Show how a total divides into parts over time with stacked areas, then normalize each period to 100% or center the stack as a streamgraph.
A stacked area chart adds series into a running total, so the top edge is the total and each band is one part. It answers how a whole is made up over time, and the reader can compare the total across periods but only judge a band’s size by its thickness.
Use a line chart when the reader needs to compare individual series, since only the bottom band sits on a flat baseline. Use 100% stacked bars when the periods are few and shares matter more than totals.
A basic stacked area
Put a nominal color channel on an area mark and set stack: "zero" on the y channel to stack the series from a zero baseline. Multi-series areas stack by default, so the setting makes the intent explicit. The x channel is ordinal here because the years are labels, not a time scale.
{
"mark": "area",
"data": [
{
"year": "2015",
"energy": 33.1,
"source": "Oil"
},
{
"year": "2016",
"energy": 33.4,
"source": "Oil"
},
{
"year": "2017",
"energy": 33.7,
"source": "Oil"
},
{
"year": "2018",
"energy": 33.7,
"source": "Oil"
},
{
"year": "2019",
"energy": 33.1,
"source": "Oil"
},
{
"year": "2020",
"energy": 31.4,
"source": "Oil"
},
// … 34 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "energy",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of primary energy (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "Fossil fuels still supplied over 80% of primary energy in 2022",
"subtitle": "Global primary energy mix by source, 2015-2022 (% of total)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "energy",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of primary energy (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "Fossil fuels still supplied over 80% of primary energy in 2022",
"subtitle": "Global primary energy mix by source, 2015-2022 (% of total)"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "energy",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of primary energy (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "Fossil fuels still supplied over 80% of primary energy in 2022",
"subtitle": "Global primary energy mix by source, 2015-2022 (% of total)"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "energy",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of primary energy (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "Fossil fuels still supplied over 80% of primary energy in 2022",
"subtitle": "Global primary energy mix by source, 2015-2022 (% of total)"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 40 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "energy",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of primary energy (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "Fossil fuels still supplied over 80% of primary energy in 2022",
"subtitle": "Global primary energy mix by source, 2015-2022 (% of total)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Normalized to 100%
stack: "normalize" rescales each period to a fraction of its total, so every column of the chart is the same height and the bands show shares. The axis format ".0%" prints the fractions as percentages. The total is no longer visible, so state it elsewhere if it matters. This demo uses capacity added each year, where the total grows from 168 to 477 gigawatts.
{
"mark": "area",
"data": [
{
"year": "2019",
"capacity": 98,
"type": "Solar"
},
{
"year": "2019",
"capacity": 58,
"type": "Wind"
},
{
"year": "2019",
"capacity": 12,
"type": "Hydro"
},
{
"year": "2020",
"capacity": 127,
"type": "Solar"
},
{
"year": "2020",
"capacity": 90,
"type": "Wind"
},
{
"year": "2020",
"capacity": 20,
"type": "Hydro"
},
// … 9 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": "normalize",
"axis": {
"title": "Share of capacity added",
"format": ".0%",
"grid": true
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "Normalizing turns growing totals into shares",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (share of gigawatts)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": "normalize",
"axis": {
"title": "Share of capacity added",
"format": ".0%",
"grid": true
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "Normalizing turns growing totals into shares",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (share of gigawatts)"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": "normalize",
"axis": {
"title": "Share of capacity added",
"format": ".0%",
"grid": true
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "Normalizing turns growing totals into shares",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (share of gigawatts)"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": "normalize",
"axis": {
"title": "Share of capacity added",
"format": ".0%",
"grid": true
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "Normalizing turns growing totals into shares",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (share of gigawatts)"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": "normalize",
"axis": {
"title": "Share of capacity added",
"format": ".0%",
"grid": true
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "Normalizing turns growing totals into shares",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (share of gigawatts)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Streamgraph
stack: "center" centers the stack around zero instead of resting it on a baseline. Each band keeps its thickness, so the chart still reads as composition. Here the total grows each year, so the outline widens from left to right around the middle.
{
"mark": "area",
"data": [
{
"year": "2019",
"capacity": 98,
"type": "Solar"
},
{
"year": "2019",
"capacity": 58,
"type": "Wind"
},
{
"year": "2019",
"capacity": 12,
"type": "Hydro"
},
{
"year": "2020",
"capacity": 127,
"type": "Solar"
},
{
"year": "2020",
"capacity": 90,
"type": "Wind"
},
{
"year": "2020",
"capacity": 20,
"type": "Hydro"
},
// … 9 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": "center",
"axis": {
"title": "Capacity added (GW)",
"format": ".0f"
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "A streamgraph centers a growing total around zero",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": "center",
"axis": {
"title": "Capacity added (GW)",
"format": ".0f"
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "A streamgraph centers a growing total around zero",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": "center",
"axis": {
"title": "Capacity added (GW)",
"format": ".0f"
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "A streamgraph centers a growing total around zero",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": "center",
"axis": {
"title": "Capacity added (GW)",
"format": ".0f"
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "A streamgraph centers a growing total around zero",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 15 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "capacity",
"type": "quantitative",
"stack": "center",
"axis": {
"title": "Capacity added (GW)",
"format": ".0f"
}
},
"color": {
"field": "type",
"type": "nominal"
}
},
"legend": {
"position": "top"
},
"chrome": {
"title": "A streamgraph centers a growing total around zero",
"subtitle": "Global renewable capacity additions by source, 2019-2023 (gigawatts)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Fields that matter
| Field | What it does |
|---|---|
mark |
"area". Several series on one area mark stack by default. |
encoding.y.stack |
"zero" stacks from zero, "normalize" scales each period to a fraction of its total, "center" centers the stack, and null overlaps the series. |
encoding.color |
One band per value of this field. |
encoding.x |
The period. "ordinal" for named years, "temporal" for dates. |
legend.position |
Where the legend sits. "top" puts it above the plot. |
Related
- Area for a single series or overlapping series.
- Stacked 100% for normalized bars and columns.
- Grouped and stacked bars for the same stacking on bars.
- Line for comparing individual series.
- Marks reference lists the channels and options each mark type takes.