Horizontal bar chart
Ranked bars with readable labels, the default for comparing items.
Positive and negative changes either side of zero, coloured by business direction.
<DivergingBarChart />Diverging bar chart is free under the MIT license.
$ npx shadcn@latest add https://chartslibraryui.com/r/diverging-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 { DivergingBarChart } from "@/components/charts/diverging-bar-chart";
import { costChange } from "./sample-data";
export function Example() {
return (
<DivergingBarChart
title="Cloud and contractors drove the cost increase"
subtitle="Change in quarterly spend vs prior quarter, USD"
data={costChange}
x="item"
y="change"
label="Change"
favorable="down"
format="currency"
/>
);
}
Rows with a label key and a signed numeric key.
[{ item: "Cloud", change: 17800 }, { item: "Travel", change: -4700 }]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 DivergingBarChartProps extends Omit<BarChartBaseProps, "series" | "colorBySign"> {
y: string;
label?: string;
}
// 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 diverging-bar-chart.tsx and the shared core/ (11 files).
"use client";
import { BarsChart, type BarChartBaseProps } from "./core/bars";
export interface DivergingBarChartProps extends Omit<BarChartBaseProps, "series" | "colorBySign"> {
y: string;
label?: string;
}
/**
* Positive and negative values either side of zero: change by region, variance by line item, net sentiment.
* Colour follows `favorable` ("up" or "down"), not the sign, so a fall in cost reads as good.
*/
export function DivergingBarChart({ y, label, sort = "desc", favorable = "up", ...props }: DivergingBarChartProps) {
return (
<BarsChart
layout="single"
orientation="horizontal"
summaryName="Diverging bar chart"
sort={sort}
favorable={favorable}
{...props}
colorBySign
series={[{ key: y, label: label ?? y }]}
/>
);
}
Ranked bars with readable labels, the default for comparing items.
Actual vs plan per line item: levels and variance side by side, coloured by business direction.
A bridge from one total to another, with computed subtotals that always close.