Skip to content
ChartslibraryUI Get Pro

Diverging bar chart

Positive and negative changes either side of zero, coloured by business direction.

FreeMIT · install in one command<DivergingBarChart />

When to use

  • Change by region or product
  • Variance by line item
  • Net scores (NPS change, sentiment)

When not to use

Good practice

  • Set favorable="down" for costs, churn or latency.
  • Sort by value so the largest movers sit at the ends.

Installation

Diverging bar chart is free under the MIT license.

$ npx shadcn@latest add https://chartslibraryui.com/r/diverging-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 { 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"
    />
  );
}

Data

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

Props

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;
}

Source

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

View diverging-bar-chart.tsx
"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 }]}
    />
  );
}
Pro

Variance chart

Actual vs plan per line item: levels and variance side by side, coloured by business direction.

Pro

Waterfall chart

A bridge from one total to another, with computed subtotals that always close.