Skip to content
ChartslibraryUI Get Pro

Stacked bar chart

Totals and their parts, with negatives stacked below zero.

FreeMIT · install in one command<StackedBarChart />

When to use

  • Totals and their composition per period
  • Inflows and outflows (positive and negative parts)
  • Composition across categories (normalize)

When not to use

Good practice

  • Put the series compared most often at the bottom, on the baseline.
  • Use at most five or six segments.
  • Total labels are hidden automatically when stacks mix signs, because a net label above the positive stack misleads.

Installation

Stacked bar chart is free under the MIT license.

$ npx shadcn@latest add https://chartslibraryui.com/r/stacked-bar-chart.json

Installs the chart, its dependencies and the shared core into your components folder.

Then import the theme once: import "@/components/charts/core/charts.css" Installation guide

Usage

import { StackedBarChart } from "@/components/charts/stacked-bar-chart";
import { monthlyExpenses } from "./sample-data";

export function Example() {
  return (
    <StackedBarChart
      title="Payroll is two-thirds of operating spend"
      subtitle="Operating expenses by category, USD"
      data={monthlyExpenses}
      x="month"
      series={[
        { key: "payroll", label: "Payroll" },
        { key: "infrastructure", label: "Infrastructure" },
        { key: "marketing", label: "Marketing" },
        { key: "other", label: "Other" },
      ]}
      format="currency"
    />
  );
}

Data

Rows with a category key and one numeric key per segment. Negative values stack downward.

[{ month: "Jan", newMrr: 34000, expansion: 9100, churned: -11200 }, ...]

Missing values are null. Invalid values make the chart say what’s wrong instead of drawing a misleading picture. How charts handle bad data

Props

Every chart also accepts the shared frame props: title, subtitle, source, height, mode, toolbar. Common API

export interface StackedBarChartProps extends BarChartBaseProps {
  orientation?: "vertical" | "horizontal";
  /** Show each category as 100% (composition, not size). */
  normalize?: boolean;
}

// from core/bars.tsx
export interface BarChartBaseProps extends FrameProps {
  data: Row[];
  /** Key of the category label in each row. */
  x: string;
  series: SeriesInput[];
  format?: FormatInput;
  /** Category values to emphasise (single series). Others are grey. */
  highlight?: string | string[];
  /** Sort categories by value (single series) or total (stacked). */
  sort?: "none" | "asc" | "desc";
  /** Value labels at bar ends. "auto" shows them when they fit. */
  labels?: boolean | "auto";
  /** First category that is an incomplete period; drawn lighter and marked. */
  partialFrom?: string;
  /** Categories that are forecasts; drawn hollow and marked. */
  forecastFrom?: string;
  references?: Reference[];
  /** Colour bars by sign using `favorable` (diverging bars). */
  colorBySign?: boolean;
  favorable?: Favorable;
  /** Show the legend. Default: on for 2+ series. */
  legend?: boolean;
  /** Max bar thickness in px. */
  maxBarSize?: number;
}

Source

Installs stacked-bar-chart.tsx and the shared core/ (11 files).

View stacked-bar-chart.tsx
"use client";

import { BarsChart, type BarChartBaseProps } from "./core/bars";

export interface StackedBarChartProps extends BarChartBaseProps {
  orientation?: "vertical" | "horizontal";
  /** Show each category as 100% (composition, not size). */
  normalize?: boolean;
}

/**
 * Totals and what they are made of. Positive parts stack up from zero and negative parts stack down.
 * Only the bottom segment shares a baseline, so put the series compared most often at the bottom.
 */
export function StackedBarChart({ orientation = "vertical", normalize, ...props }: StackedBarChartProps) {
  return (
    <BarsChart
      layout={normalize ? "stacked100" : "stacked"}
      orientation={orientation}
      summaryName={normalize ? "100% stacked bar chart" : "Stacked bar chart"}
      {...props}
    />
  );
}

Stacked area chart

A total over time and what it's made of, in absolute terms or as 100% shares.

Pro

Variance trend

Variance to plan by period, split into drivers either side of zero with the net marked.