Skip to content
OpenChart

Stacked bar chart vs grouped bar chart

Stacked bars show the total, and grouped bars let you compare the parts. This post also covers how to order the segments of a stack.

201820192020202120222023202401020304050GigawattsSolarBatteriesWindNatural gasThe four fuels added 49.8 GW in 2024, the most of any year shownUS generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)201820192020202120222023202401020304050GigawattsSolarBatteriesWindNatural gasThe four fuels added 49.8 GW in 2024, the most of any year shownUS generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)
New US generating capacity by fuel, stacked. Source: US Energy Information Administration, Form EIA-860M preliminary monthly generator inventory.

Made with OpenChart. How to build this column chart

US solar generators that came online in 2024 add up to 31.2 gigawatts, 13 times the new natural gas capacity. Whether a chart makes that comparison easy depends on whether it puts each year’s fuels end to end in a stack or side by side in a group. A stack shows the total and how the bottom segment moves. A group shows how the parts compare with each other.

Stacked bars

The chart below stacks four fuels for each year, in gigawatts (GW) of nameplate capacity, the maximum output a generator is rated for. The column height is the four fuels’ total, which reached 49.8 GW in 2024. Solar sits at the bottom, so its growth from 5.0 GW in 2018 to 31.2 GW in 2024 is easy to follow.

201820192020202120222023202401020304050GigawattsSolarBatteriesWindNatural gasThe four fuels added 49.8 GW in 2024, the most of any year shownUS generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)201820192020202120222023202401020304050GigawattsSolarBatteriesWindNatural gasThe four fuels added 49.8 GW in 2024, the most of any year shownUS generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)
New US generating capacity by fuel, stacked. Source: US Energy Information Administration, Form EIA-860M preliminary monthly generator inventory.

Made with OpenChart. How to build this column chart

{
  "mark": "bar",
  "data": [
    {
      "year": "2018",
      "fuel": "Solar",
      "gigawatts": 5
    },
    {
      "year": "2018",
      "fuel": "Batteries",
      "gigawatts": 0.2
    },
    {
      "year": "2018",
      "fuel": "Wind",
      "gigawatts": 7.1
    },
    {
      "year": "2018",
      "fuel": "Natural gas",
      "gigawatts": 21.7
    },
    {
      "year": "2019",
      "fuel": "Solar",
      "gigawatts": 5.9
    },
    {
      "year": "2019",
      "fuel": "Batteries",
      "gigawatts": 0.2
    },
    // … 22 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "gigawatts",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Gigawatts",
        "format": ".0f"
      }
    },
    "color": {
      "field": "fuel",
      "type": "nominal",
      "scale": {
        "domain": [
          "Solar",
          "Batteries",
          "Wind",
          "Natural gas"
        ],
        "range": [
          "#e07b39",
          "#7c3aed",
          "#0e7490",
          "#6b7280"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "The four fuels added 49.8 GW in 2024, the most of any year shown",
    "subtitle": "US generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2018",
              "2020",
              "2022",
              "2024"
            ]
          }
        }
      }
    }
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

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

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "gigawatts",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Gigawatts",
        "format": ".0f"
      }
    },
    "color": {
      "field": "fuel",
      "type": "nominal",
      "scale": {
        "domain": [
          "Solar",
          "Batteries",
          "Wind",
          "Natural gas"
        ],
        "range": [
          "#e07b39",
          "#7c3aed",
          "#0e7490",
          "#6b7280"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "The four fuels added 49.8 GW in 2024, the most of any year shown",
    "subtitle": "US generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2018",
              "2020",
              "2022",
              "2024"
            ]
          }
        }
      }
    }
  }
};

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

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "gigawatts",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Gigawatts",
        "format": ".0f"
      }
    },
    "color": {
      "field": "fuel",
      "type": "nominal",
      "scale": {
        "domain": [
          "Solar",
          "Batteries",
          "Wind",
          "Natural gas"
        ],
        "range": [
          "#e07b39",
          "#7c3aed",
          "#0e7490",
          "#6b7280"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "The four fuels added 49.8 GW in 2024, the most of any year shown",
    "subtitle": "US generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2018",
              "2020",
              "2022",
              "2024"
            ]
          }
        }
      }
    }
  }
};
</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 = [/* 28 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "gigawatts",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Gigawatts",
        "format": ".0f"
      }
    },
    "color": {
      "field": "fuel",
      "type": "nominal",
      "scale": {
        "domain": [
          "Solar",
          "Batteries",
          "Wind",
          "Natural gas"
        ],
        "range": [
          "#e07b39",
          "#7c3aed",
          "#0e7490",
          "#6b7280"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "The four fuels added 49.8 GW in 2024, the most of any year shown",
    "subtitle": "US generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2018",
              "2020",
              "2022",
              "2024"
            ]
          }
        }
      }
    }
  }
};
</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 = [/* 28 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "gigawatts",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Gigawatts",
        "format": ".0f"
      }
    },
    "color": {
      "field": "fuel",
      "type": "nominal",
      "scale": {
        "domain": [
          "Solar",
          "Batteries",
          "Wind",
          "Natural gas"
        ],
        "range": [
          "#e07b39",
          "#7c3aed",
          "#0e7490",
          "#6b7280"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "The four fuels added 49.8 GW in 2024, the most of any year shown",
    "subtitle": "US generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2018",
              "2020",
              "2022",
              "2024"
            ]
          }
        }
      }
    }
  }
};

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

The data counts only generators that still operate in one snapshot of the EIA’s inventory. A plant that opened in 2018 and has since retired is missing, so older years are understated.

Grouped bars

Wind is hard to read in that stack. It sits in the middle, so both its bottom edge and its top edge move from year to year, and the reader has to subtract to see its size. Grouping gives every fuel the same zero baseline.

201820192020202120222023202405101520253035GigawattsSolarBatteriesWindNatural gasSolar added 31.2 GW in 2024, 13 times as much as natural gasUS generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)201820192020202120222023202405101520253035GigawattsSolarBatteriesWindNatural gasSolar added 31.2 GW in 2024, 13 times as much as natural gasUS generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)
Live example. Source: US Energy Information Administration, Form EIA-860M preliminary monthly generator inventory.

Made with OpenChart. How to build this column chart

{
  "mark": "bar",
  "data": [
    {
      "year": "2018",
      "fuel": "Solar",
      "gigawatts": 5
    },
    {
      "year": "2018",
      "fuel": "Batteries",
      "gigawatts": 0.2
    },
    {
      "year": "2018",
      "fuel": "Wind",
      "gigawatts": 7.1
    },
    {
      "year": "2018",
      "fuel": "Natural gas",
      "gigawatts": 21.7
    },
    {
      "year": "2019",
      "fuel": "Solar",
      "gigawatts": 5.9
    },
    {
      "year": "2019",
      "fuel": "Batteries",
      "gigawatts": 0.2
    },
    // … 22 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "gigawatts",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Gigawatts",
        "format": ".0f"
      }
    },
    "color": {
      "field": "fuel",
      "type": "nominal",
      "scale": {
        "domain": [
          "Solar",
          "Batteries",
          "Wind",
          "Natural gas"
        ],
        "range": [
          "#e07b39",
          "#7c3aed",
          "#0e7490",
          "#6b7280"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "Solar added 31.2 GW in 2024, 13 times as much as natural gas",
    "subtitle": "US generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2018",
              "2020",
              "2022",
              "2024"
            ]
          }
        }
      }
    }
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

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

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "gigawatts",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Gigawatts",
        "format": ".0f"
      }
    },
    "color": {
      "field": "fuel",
      "type": "nominal",
      "scale": {
        "domain": [
          "Solar",
          "Batteries",
          "Wind",
          "Natural gas"
        ],
        "range": [
          "#e07b39",
          "#7c3aed",
          "#0e7490",
          "#6b7280"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "Solar added 31.2 GW in 2024, 13 times as much as natural gas",
    "subtitle": "US generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2018",
              "2020",
              "2022",
              "2024"
            ]
          }
        }
      }
    }
  }
};

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

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "gigawatts",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Gigawatts",
        "format": ".0f"
      }
    },
    "color": {
      "field": "fuel",
      "type": "nominal",
      "scale": {
        "domain": [
          "Solar",
          "Batteries",
          "Wind",
          "Natural gas"
        ],
        "range": [
          "#e07b39",
          "#7c3aed",
          "#0e7490",
          "#6b7280"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "Solar added 31.2 GW in 2024, 13 times as much as natural gas",
    "subtitle": "US generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2018",
              "2020",
              "2022",
              "2024"
            ]
          }
        }
      }
    }
  }
};
</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 = [/* 28 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "gigawatts",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Gigawatts",
        "format": ".0f"
      }
    },
    "color": {
      "field": "fuel",
      "type": "nominal",
      "scale": {
        "domain": [
          "Solar",
          "Batteries",
          "Wind",
          "Natural gas"
        ],
        "range": [
          "#e07b39",
          "#7c3aed",
          "#0e7490",
          "#6b7280"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "Solar added 31.2 GW in 2024, 13 times as much as natural gas",
    "subtitle": "US generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2018",
              "2020",
              "2022",
              "2024"
            ]
          }
        }
      }
    }
  }
};
</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 = [/* 28 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "gigawatts",
      "type": "quantitative",
      "stack": null,
      "axis": {
        "title": "Gigawatts",
        "format": ".0f"
      }
    },
    "color": {
      "field": "fuel",
      "type": "nominal",
      "scale": {
        "domain": [
          "Solar",
          "Batteries",
          "Wind",
          "Natural gas"
        ],
        "range": [
          "#e07b39",
          "#7c3aed",
          "#0e7490",
          "#6b7280"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "chrome": {
    "title": "Solar added 31.2 GW in 2024, 13 times as much as natural gas",
    "subtitle": "US generators still operating, by the year they came online and by fuel, 2018-2024 (gigawatts)"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2018",
              "2020",
              "2022",
              "2024"
            ]
          }
        }
      }
    }
  }
};

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

Now the comparisons are direct: in 2024 batteries (11.1 GW) added more than twice as much as wind (5.2 GW). The total is gone, though, and a reader who wants it has to add four bars. Four fuels across seven years is 28 bars, about as many as a grouped chart can hold before the bars get too thin to read.

Order of the segments

Put the series the reader cares about at the bottom, where it starts at zero, and keep its order and color the same in every column. Past four or five segments the middle ones get hard to read, and a grouped chart or one panel per series works better.

Choose that order on purpose. Data often arrives sorted by name, and a stack drawn in that order puts whichever series sorts first at the bottom, whether or not the reader cares about it.

Totals over time

Stacking is the right choice when the total is the story. Counted homelessness in the 50 states and DC fell to 544,084 people in 2016 and then rose 41% to 767,856 in 2024, and the stack shows that total while still splitting it into sheltered and unsheltered people. Unsheltered people, the top segment, rose from 172,035 to 271,350 over the same years, and that change is harder to see than the total.

2008201020122014201620182020202220240100k200k300k400k500k600k700k800kPeople counted2021: unshelteredcount skippedShelteredUnshelteredThe homeless count fell to 544,084 in 2016 and rose to 767,856 in2024People counted as homeless on one night in January, 50 states and DC, 2007-20242008201020122014201620182020202220240100k200k300k400k500k600k700k800kPeople counted2021: unshelteredcount skippedShelteredUnshelteredThe homeless count fell to 544,084 in 2016 and rose to 767,856 in2024People counted as homeless on one night in January, 50 states and DC, 2007-2024
Live example. Source: US Department of Housing and Urban Development, Annual Homeless Assessment Report (point-in-time counts).

Made with OpenChart. How to build this column chart

{
  "mark": "bar",
  "data": [
    {
      "year": "2007",
      "status": "Sheltered",
      "people": 389858
    },
    {
      "year": "2007",
      "status": "Unsheltered",
      "people": 251807
    },
    {
      "year": "2008",
      "status": "Sheltered",
      "people": 385333
    },
    {
      "year": "2008",
      "status": "Unsheltered",
      "people": 250112
    },
    {
      "year": "2009",
      "status": "Sheltered",
      "people": 401725
    },
    {
      "year": "2009",
      "status": "Unsheltered",
      "people": 222873
    },
    // … 30 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0,
        "values": [
          "2008",
          "2010",
          "2012",
          "2014",
          "2016",
          "2018",
          "2020",
          "2022",
          "2024"
        ]
      }
    },
    "y": {
      "field": "people",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "People counted",
        "format": "~s"
      }
    },
    "color": {
      "field": "status",
      "type": "nominal",
      "scale": {
        "domain": [
          "Sheltered",
          "Unsheltered"
        ],
        "range": [
          "#0e7490",
          "#e07b39"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "annotations": [
    {
      "type": "text",
      "x": "2021",
      "y": 400000,
      "text": "2021: unsheltered\ncount skipped",
      "anchor": "top",
      "offset": {
        "dx": -50,
        "dy": -62
      },
      "connector": true
    }
  ],
  "chrome": {
    "title": "The homeless count fell to 544,084 in 2016 and rose to 767,856 in 2024",
    "subtitle": "People counted as homeless on one night in January, 50 states and DC, 2007-2024"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2008",
              "2012",
              "2016",
              "2020",
              "2024"
            ]
          }
        }
      }
    }
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

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

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0,
        "values": [
          "2008",
          "2010",
          "2012",
          "2014",
          "2016",
          "2018",
          "2020",
          "2022",
          "2024"
        ]
      }
    },
    "y": {
      "field": "people",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "People counted",
        "format": "~s"
      }
    },
    "color": {
      "field": "status",
      "type": "nominal",
      "scale": {
        "domain": [
          "Sheltered",
          "Unsheltered"
        ],
        "range": [
          "#0e7490",
          "#e07b39"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "annotations": [
    {
      "type": "text",
      "x": "2021",
      "y": 400000,
      "text": "2021: unsheltered\ncount skipped",
      "anchor": "top",
      "offset": {
        "dx": -50,
        "dy": -62
      },
      "connector": true
    }
  ],
  "chrome": {
    "title": "The homeless count fell to 544,084 in 2016 and rose to 767,856 in 2024",
    "subtitle": "People counted as homeless on one night in January, 50 states and DC, 2007-2024"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2008",
              "2012",
              "2016",
              "2020",
              "2024"
            ]
          }
        }
      }
    }
  }
};

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

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0,
        "values": [
          "2008",
          "2010",
          "2012",
          "2014",
          "2016",
          "2018",
          "2020",
          "2022",
          "2024"
        ]
      }
    },
    "y": {
      "field": "people",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "People counted",
        "format": "~s"
      }
    },
    "color": {
      "field": "status",
      "type": "nominal",
      "scale": {
        "domain": [
          "Sheltered",
          "Unsheltered"
        ],
        "range": [
          "#0e7490",
          "#e07b39"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "annotations": [
    {
      "type": "text",
      "x": "2021",
      "y": 400000,
      "text": "2021: unsheltered\ncount skipped",
      "anchor": "top",
      "offset": {
        "dx": -50,
        "dy": -62
      },
      "connector": true
    }
  ],
  "chrome": {
    "title": "The homeless count fell to 544,084 in 2016 and rose to 767,856 in 2024",
    "subtitle": "People counted as homeless on one night in January, 50 states and DC, 2007-2024"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2008",
              "2012",
              "2016",
              "2020",
              "2024"
            ]
          }
        }
      }
    }
  }
};
</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 = [/* 36 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0,
        "values": [
          "2008",
          "2010",
          "2012",
          "2014",
          "2016",
          "2018",
          "2020",
          "2022",
          "2024"
        ]
      }
    },
    "y": {
      "field": "people",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "People counted",
        "format": "~s"
      }
    },
    "color": {
      "field": "status",
      "type": "nominal",
      "scale": {
        "domain": [
          "Sheltered",
          "Unsheltered"
        ],
        "range": [
          "#0e7490",
          "#e07b39"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "annotations": [
    {
      "type": "text",
      "x": "2021",
      "y": 400000,
      "text": "2021: unsheltered\ncount skipped",
      "anchor": "top",
      "offset": {
        "dx": -50,
        "dy": -62
      },
      "connector": true
    }
  ],
  "chrome": {
    "title": "The homeless count fell to 544,084 in 2016 and rose to 767,856 in 2024",
    "subtitle": "People counted as homeless on one night in January, 50 states and DC, 2007-2024"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2008",
              "2012",
              "2016",
              "2020",
              "2024"
            ]
          }
        }
      }
    }
  }
};
</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 = [/* 36 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal",
      "axis": {
        "labelAngle": 0,
        "values": [
          "2008",
          "2010",
          "2012",
          "2014",
          "2016",
          "2018",
          "2020",
          "2022",
          "2024"
        ]
      }
    },
    "y": {
      "field": "people",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "People counted",
        "format": "~s"
      }
    },
    "color": {
      "field": "status",
      "type": "nominal",
      "scale": {
        "domain": [
          "Sheltered",
          "Unsheltered"
        ],
        "range": [
          "#0e7490",
          "#e07b39"
        ]
      }
    }
  },
  "labels": {
    "density": "none"
  },
  "legend": {
    "position": "top"
  },
  "annotations": [
    {
      "type": "text",
      "x": "2021",
      "y": 400000,
      "text": "2021: unsheltered\ncount skipped",
      "anchor": "top",
      "offset": {
        "dx": -50,
        "dy": -62
      },
      "connector": true
    }
  ],
  "chrome": {
    "title": "The homeless count fell to 544,084 in 2016 and rose to 767,856 in 2024",
    "subtitle": "People counted as homeless on one night in January, 50 states and DC, 2007-2024"
  },
  "overrides": {
    "compact": {
      "encoding": {
        "x": {
          "axis": {
            "labelAngle": 0,
            "values": [
              "2008",
              "2012",
              "2016",
              "2020",
              "2024"
            ]
          }
        }
      }
    }
  }
};

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

The 2021 column is the exception. Many communities skipped the unsheltered count that year, so the unsheltered segment drops from 222,061 to 54,391 and the total drops with it, as the annotation says. The 2016 low leaves 2021 out.

If shares matter more than totals, normalize each column with a 100% stacked bar. For many periods, a stacked area draws the same stack as a continuous shape.

The spec

Both capacity charts use mark: "bar" with the series in color. The only difference is the value channel: stack: "zero" on y stacks the series from a zero baseline and stack: null places them side by side. Segments stack in the order of the data rows, and the rows list solar first, so solar is the bottom segment. The domain and range on the color scale pin each fuel to a color.

labelAngle: 0 keeps the year labels level, and values on the x axis picks which years get a label, with an overrides.compact block labeling fewer of them on a phone. The homelessness chart adds one text annotation on the 2021 column, with x: "2021" placing it on that category.

The grouped and stacked bars page shows how to print a value inside each segment.