Skip to content
OpenChart

Grouped and stacked bars

Split each bar or column into series, placed side by side to compare series or stacked end to end to read totals and parts.

Grouped and stacked bars answer different questions about the same data. Grouping places the series side by side, so the reader compares one series against another. Stacking puts the series end to end, so the reader sees each total and how it divides.

Both add a color channel to a bar or column chart. When only the split matters and not the totals, a 100% stacked chart is the better form.

Grouped columns

Bars stack by default. stack: null on the value channel places the series side by side instead. Solar’s lead over wind and hydro is easy to read here because every series starts at the same baseline.

20192020202120222023050100150200250300350Capacity added (GW)98581212790201679315222752234610724SolarWindHydroSolar additions more than tripled from 2019 to2023Global renewable capacity additions by source, 2019-2023 (gigawatts)20192020202120222023050100150200250300350Capacity added (GW)98581212790201679315222752234610724SolarWindHydroSolar additions more than tripled from 2019 to2023Global renewable capacity additions by source, 2019-2023 (gigawatts)
Renewable capacity added by source, grouped. Source: International Energy Agency.
{
  "mark": "bar",
  "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": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Solar additions more than tripled from 2019 to 2023",
    "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": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Solar additions more than tripled from 2019 to 2023",
    "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": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Solar additions more than tripled from 2019 to 2023",
    "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": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Solar additions more than tripled from 2019 to 2023",
    "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": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Solar additions more than tripled from 2019 to 2023",
    "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);

Stacked columns

stack: "zero" stacks the series from zero, so each column’s height is the total for that year. The cost is that only the bottom series shares a baseline, so the upper series are harder to compare across years.

201920202021202220230100200300400500Capacity added (GW)SolarWindHydroTotal additions more than doubled in fouryearsGlobal renewable capacity additions by source, 2019-2023 (gigawatts)201920202021202220230100200300400500Capacity added (GW)SolarWindHydroTotal additions more than doubled in fouryearsGlobal renewable capacity additions by source, 2019-2023 (gigawatts)
Renewable capacity added by source, stacked. Source: International Energy Agency.
{
  "mark": "bar",
  "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": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Total additions more than doubled in four years",
    "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": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Total additions more than doubled in four years",
    "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": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Total additions more than doubled in four years",
    "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": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Total additions more than doubled in four years",
    "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": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "capacity",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Capacity added (GW)"
      }
    },
    "color": {
      "field": "type",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Total additions more than doubled in four years",
    "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);

Stacked bars with labels

On a horizontal stack, labels.density: "all" prints a value on every segment. Here the five parts of each income bracket sum to 100, so the housing segment is comparable across brackets.

020406080100Share of spending (%)Under $30K$30K - $50K$50K - $80K$80K - $120KOver $120K40181512153516181021321419926301217833271014643HousingFoodTransportHealthcareOtherLower-income households spend a larger shareon housingShare of annual household expenditure by category and income bracket, 2022 (%)020406080100Share of spending (%)Under $30K$30K - $50K$50K - $80K$80K - $120KOver $120K40181512153516181021321419926301217833271014643HousingFoodTransportHealthcareOtherLower-income households spend a larger shareon housingShare of annual household expenditure by category and income bracket, 2022 (%)
Household spending, stacked with labels. Source: Bureau of Labor Statistics, Consumer Expenditure Survey.
{
  "mark": "bar",
  "data": [
    {
      "bracket": "Under $30K",
      "pct": 40,
      "category": "Housing"
    },
    {
      "bracket": "Under $30K",
      "pct": 18,
      "category": "Food"
    },
    {
      "bracket": "Under $30K",
      "pct": 15,
      "category": "Transport"
    },
    {
      "bracket": "Under $30K",
      "pct": 12,
      "category": "Healthcare"
    },
    {
      "bracket": "Under $30K",
      "pct": 15,
      "category": "Other"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 35,
      "category": "Housing"
    },
    // … 19 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of spending (%)"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Lower-income households spend a larger share on housing",
    "subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 25 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of spending (%)"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Lower-income households spend a larger share on housing",
    "subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
  }
};

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 = [/* 25 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of spending (%)"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Lower-income households spend a larger share on housing",
    "subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
  }
};
</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 = [/* 25 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of spending (%)"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Lower-income households spend a larger share on housing",
    "subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
  }
};
</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 = [/* 25 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of spending (%)"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "all"
  },
  "chrome": {
    "title": "Lower-income households spend a larger share on housing",
    "subtitle": "Share of annual household expenditure by category and income bracket, 2022 (%)"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Normalized stacks

stack: "normalize" scales every bar to the same full length, so the chart shows each part’s share instead of its size. Set the axis format to ".0%" to print the ticks as percentages. Use this when the totals differ or do not matter.

0%20%40%60%80%100%Share of spendingUnder $30K$30K - $50K$50K - $80K$80K - $120KOver $120KHousingFoodTransportHealthcareOtherHousing and food shrink as a share of spendingas income risesHousehold expenditure normalized to 100% per income bracket, 20220%20%40%60%80%100%Share of spendingUnder $30K$30K - $50K$50K - $80K$80K - $120KOver $120KHousingFoodTransportHealthcareOtherHousing and food shrink as a share of spendingas income risesHousehold expenditure normalized to 100% per income bracket, 2022
Household spending, normalized. Source: Bureau of Labor Statistics, Consumer Expenditure Survey.
{
  "mark": "bar",
  "data": [
    {
      "bracket": "Under $30K",
      "pct": 40,
      "category": "Housing"
    },
    {
      "bracket": "Under $30K",
      "pct": 18,
      "category": "Food"
    },
    {
      "bracket": "Under $30K",
      "pct": 15,
      "category": "Transport"
    },
    {
      "bracket": "Under $30K",
      "pct": 12,
      "category": "Healthcare"
    },
    {
      "bracket": "Under $30K",
      "pct": 15,
      "category": "Other"
    },
    {
      "bracket": "$30K - $50K",
      "pct": 35,
      "category": "Housing"
    },
    // … 19 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "normalize",
      "axis": {
        "title": "Share of spending",
        "format": ".0%"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Housing and food shrink as a share of spending as income rises",
    "subtitle": "Household expenditure normalized to 100% per income bracket, 2022"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 25 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "normalize",
      "axis": {
        "title": "Share of spending",
        "format": ".0%"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Housing and food shrink as a share of spending as income rises",
    "subtitle": "Household expenditure normalized to 100% per income bracket, 2022"
  }
};

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 = [/* 25 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "normalize",
      "axis": {
        "title": "Share of spending",
        "format": ".0%"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Housing and food shrink as a share of spending as income rises",
    "subtitle": "Household expenditure normalized to 100% per income bracket, 2022"
  }
};
</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 = [/* 25 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "normalize",
      "axis": {
        "title": "Share of spending",
        "format": ".0%"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Housing and food shrink as a share of spending as income rises",
    "subtitle": "Household expenditure normalized to 100% per income bracket, 2022"
  }
};
</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 = [/* 25 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "pct",
      "type": "quantitative",
      "stack": "normalize",
      "axis": {
        "title": "Share of spending",
        "format": ".0%"
      }
    },
    "y": {
      "field": "bracket",
      "type": "nominal"
    },
    "color": {
      "field": "category",
      "type": "nominal"
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Housing and food shrink as a share of spending as income rises",
    "subtitle": "Household expenditure normalized to 100% per income bracket, 2022"
  }
};

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
encoding.color Splits each category into series by the field’s values.
encoding.y.stack On a column chart, null groups the series side by side, "zero" stacks them from zero and "normalize" stacks them to full length.
encoding.x.stack The same setting on a horizontal bar chart, where the value is on x.
encoding.*.axis title names the axis and format sets how its ticks print.
labels.density "all" labels every segment and "none" turns the labels off.