Grouped bar chart
Two to four measures side by side per category.
Totals and their parts, with negatives stacked below zero.
<StackedBarChart />Stacked bar chart is free under the MIT license.
$ npx shadcn@latest add https://chartslibraryui.com/r/stacked-bar-chart.jsonInstalls 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
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"
/>
);
}
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
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;
}Installs stacked-bar-chart.tsx and the shared core/ (11 files).
"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}
/>
);
}
Two to four measures side by side per category.
A total over time and what it's made of, in absolute terms or as 100% shares.
Variance to plan by period, split into drivers either side of zero with the net marked.