Skip to content
ChartslibraryUI Get Pro

Grouped bar chart

Two to four measures side by side per category.

FreeMIT · install in one command<GroupedBarChart />

When to use

  • This year vs last year by quarter
  • Two or three segments compared per category

When not to use

Good practice

  • Put the comparison (last year) in grey and the current period in the accent.
  • Keep the series order consistent across charts.

Installation

Grouped bar chart is free under the MIT license.

$ npx shadcn@latest add https://chartslibraryui.com/r/grouped-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 { GroupedBarChart } from "@/components/charts/grouped-bar-chart";
import { quarterlyRevenue } from "./sample-data";

export function Example() {
  return (
    <GroupedBarChart
      title="Every quarter beat the same quarter last year"
      subtitle="Revenue, USD"
      data={quarterlyRevenue}
      x="quarter"
      series={[
        { key: "lastYear", label: "Last year", color: "var(--clui-context)" },
        { key: "thisYear", label: "This year" },
      ]}
      format="currency"
    />
  );
}

Data

Rows with a category key and one numeric key per series.

[{ quarter: "Q1", lastYear: 2.9e6, thisYear: 3.4e6 }, ...]

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 GroupedBarChartProps extends BarChartBaseProps {
  orientation?: "vertical" | "horizontal";
}

// 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 grouped-bar-chart.tsx and the shared core/ (11 files).

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

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

export interface GroupedBarChartProps extends BarChartBaseProps {
  orientation?: "vertical" | "horizontal";
}

/**
 * Two to four measures side by side per category, e.g. this year vs last year. Past four series the groups get
 * too wide to read; use small multiples instead.
 */
export function GroupedBarChart({ orientation = "vertical", ...props }: GroupedBarChartProps) {
  return <BarsChart layout="grouped" orientation={orientation} summaryName="Grouped bar chart" {...props} />;
}

Bar chart

Columns for one measure across periods or categories, always from zero.

Pro

Dumbbell chart

Before and after per item with the change printed and coloured by direction.