Stacked area

A stacked area adds several series into one shape across an ordered x. The top of the shape is the total. Each band is one series. The type is stacked-area.

When to use it

Use a stacked area when the series are parts of a total and the x has an order, usually time. Spend by team is that chart: product, design, and support add up, and the total rises. Use percent when the question is the mix, not the total. Channel mix and Classic and e-bike share are that reading.

Do not use a stacked area for one series. A line is enough, and a fill under it is still a line. Do not use it when the series should be compared as separate lines. Stacking hides that. Do not use it when the baseline should move to keep the layers smooth. That is a streamgraph.

Stacked area, filled line, and streamgraph

A filled line is not this type. There is no type: "area". Area is the note for type: "line" with mark.area set to true. That fills each series down to the baseline. The series are not stacked, so they can overlap.

filled line
{
  "type": "line",
  "mark": { "area": true }
}

A stacked area stacks by default. mark.stack omitted means stack. The bands sit on each other and the top edge is the sum.

A streamgraph is also stacked, but the baseline moves. The shape is centered instead of anchored to zero. Use that when the change in the layers matters more than the total.

Data

Each row is one x and one number per series. encoding.x is the category or the time. encoding.y is a list of channels. Spend by team lists product, design, and support. Plan mix over time lists free, pro, and team. Classic and e-bike share lists two fields, classic and ebike.

The numbers are amounts. ChartKit adds them at each x. You do not pass shares unless you set mark.stack to percent, which turns each x into 100% from the amounts you did pass. a11y.alt is required. One sentence, 10 to 500 characters, with the point of the chart.

A spec that renders

This is the smallest stacked area. version, type, and a11y.alt are required. Three months, free and pro. The curve and the stack come from the defaults, so mark can be omitted. The curve is monotone. The stack is stack, not a share of 100%.

spec.json
{
  "version": "1",
  "type": "stacked-area",
  "data": [
    {
      "month": "Jan",
      "free": 40,
      "pro": 20
    },
    {
      "month": "Feb",
      "free": 42,
      "pro": 28
    },
    {
      "month": "Mar",
      "free": 45,
      "pro": 36
    }
  ],
  "encoding": {
    "x": "month",
    "y": [
      {
        "field": "free",
        "title": "Free"
      },
      {
        "field": "pro",
        "title": "Pro"
      }
    ]
  },
  "a11y": {
    "alt": "Pro accounts grew faster than free accounts from January to March."
  }
}

Install core and the adapter for your framework. The menu in the sidebar picks the snippet. spec is the JSON above.

Import@chartkitdev/core/charts/stacked-areaBundle@chartkitdev/core/allFamilyLibrary
React
npm install @chartkitdev/core
npm install @chartkitdev/react

import { Chart } from "@chartkitdev/react";

export function Panel() {
  return <Chart spec={spec} />;
}
Vanilla
npm install @chartkitdev/core
npm install @chartkitdev/vanilla

import { mount } from "@chartkitdev/vanilla";

const chart = mount(document.querySelector("#chart"), spec);
chart.update(spec);
Vue
npm install @chartkitdev/core
npm install @chartkitdev/vue

<script setup>
import { Chart } from "@chartkitdev/vue";
</script>

<template>
  <Chart :spec="spec" />
</template>
Svelte
npm install @chartkitdev/core
npm install @chartkitdev/svelte

<script>
  import { Chart } from "@chartkitdev/svelte";
</script>

<Chart {spec} />
Astro
npm install @chartkitdev/core
npm install @chartkitdev/astro

---
import { Chart } from "@chartkitdev/astro";
---

<Chart spec={spec} />
Solid
npm install @chartkitdev/core
npm install @chartkitdev/solid

import { Chart } from "@chartkitdev/solid";

export function Panel() {
  return <Chart spec={spec} />;
}
Qwik
npm install @chartkitdev/core
npm install @chartkitdev/qwik

import { component$ } from "@builder.io/qwik";
import { Chart } from "@chartkitdev/qwik";

export const Panel = component$(() => {
  return <Chart spec={spec} />;
});
Angular
npm install @chartkitdev/core
npm install @chartkitdev/angular

import { Component } from "@angular/core";
import { Chart } from "@chartkitdev/angular";

@Component({
  standalone: true,
  imports: [Chart],
  template: `<ck-chart [spec]="spec" />`,
})
export class Panel {
  spec = spec;
}
Custom element
npm install @chartkitdev/core
npm install @chartkitdev/element

import "@chartkitdev/element";

const chart = document.querySelector("chart-kit");
chart.spec = spec;

Each option below is a key on the spec. The demo changes that key and updates the chart and the snippet together.

Examples

The rail switches among existing stacked area specs. Spend by team and Plan mix over time add the series. Channel mix and Classic and e-bike share are percent stacks. Classic and e-bike share also names the tooltip.

Curve

mark.curve is how the edges join. monotone is the default. It stays inside the data, so a spike does not overshoot. linear draws straight segments. step holds the value and then jumps, which is the right curve for a total that changes and then sits still.

mark.curve
{
  "mark": { "curve": "linear" }
}
mark.curve step
{
  "mark": { "curve": "step" }
}

step-before and step-after place the jump on the other side of the point. Set those in the playground.

Stack

mark.stack is stack or percent. Omit it and the chart stacks the amounts. The top of the shape is the total. percent normalizes each x to 100%, so a growing total does not change the height. Channel mix and Classic and e-bike share set that.

mark.stack
{
  "mark": { "stack": "stack" }
}
mark.stack percent
{
  "mark": { "stack": "percent" }
}

none draws the series unstacked. That is the filled-line reading, and the page for it is area, with type: "line". This control does not offer it.

Legend

A stacked area with several series shows a legend. Omit legend, or set true, and it sits on top. false hides it. legend.position is top, right, bottom, left, or inside. Plan mix over time hides it. Classic and e-bike share puts it on the bottom.

legend.position
{
  "legend": { "position": "top" }
}

Tooltip

tooltip is on for a stacked area when the key is omitted. Hovering a position shows each series. tooltip: false turns the card off. tooltip: true turns it on.

tooltip false
{
  "tooltip": false
}
tooltip true
{
  "tooltip": true
}

tooltip.fields chooses the rows, and tooltip.position places the card. Classic and e-bike share sets an index card with a crosshair. Change those in the playground. Turning the card off and on again puts that object back.

The rest of the spec

mark.gradient fades the fill toward the baseline. It is on unless you set false. mark.fillOpacity sets the fill. mark.connectNulls draws through missing values. axes.y.format can be percent on a share chart. Theme presets live on the playground, with the full option list.