Skip to content

Diverging Bar Chart for React

A React diverging bar chart centers survey answers on a neutral midpoint; the butterfly variant draws a population pyramid. Pass data as readonly ChartRow[]: one row per item (a question, a product, an age group).

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/diverging-bar-chart.json

Customer survey

Use the up and down arrow keys to move between rows, and left and right to read each segment.

  • DivergingBarChart
  • DivergingBarTable
  • divergingLegend
  • divergingLevels
  • defaultDivergingBarChartLabels
  • +3 more

Likert survey

Six statements, 460 answers each. Shares sit inside the segments where they fit (black or white ink, whichever reads better), and the net score follows each row. Switch the quarter and the segments morph. Up and down move between statements, left and right between answers.

Customer survey

Q4, 460 answers per statement

  • Strongly disagree
  • Disagree
  • Neutral
  • Agree
  • Strongly agree

Population pyramid

variant="butterfly" with two levels: men to the left, women to the right of the spine, the age groups in the middle. One scale for both sides, so the halves compare honestly.

Population by age

Mid-year estimate, millions

  • Men
  • Women

Files to copy

15 folders · 30 files · 4 packages
From theme/src/
  • components/charts/{diverging-bar-chart.tsx, histogram.tsx, chart-export-menu.tsx, chart-export.ts, chart-figure.tsx, chart-fit.tsx, chart-icons.tsx, chart-parts.tsx, chart-patterns.tsx, chart-pointer.tsx, chart-tip.tsx, chart-utils.ts, heatmap.tsx}
  • components/charts/chart-parts/
  • components/charts/diverging-bar-chart/
  • components/charts/heatmap/
  • components/charts/histogram/
  • components/charts/kits/
  • components/drawer/parts/sheet.tsx
  • components/kbd/
  • components/menu/
  • components/skeleton/
  • components/status-icons/
  • hooks/{use-controllable.ts, use-direction.ts, use-intersection.ts, use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, csv.ts, form.ts, glide-highlight.tsx, heading.ts, math.ts, popup-a11y.ts, popup-motion.ts, warn-once.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/diverging-bar-chart.jsonRegistry item: diverging-bar-chart. The styles need the Tailwind v4 glass UI setup.
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

Next.js: samples here pass functions (formatters, seriesLegend, a dashboard's load), so put them in a file that starts with "use client".

Props

PropTypeDefaultDescription
data (required)readonly ChartRow[]NoneOne row per item (a question, a product, an age group).
index (required)stringNoneKey of the row name in each data row.
levels (required)readonly DivergingLevel[]NoneLikert: the answer levels, most negative first ("Strongly disagree" … "Strongly agree"); with an odd count the middle one is neutral and straddles the centre line. Butterfly: exactly two levels, drawn to the left and right of the spine (e.g. Men / Women).
variant"likert" | "butterfly""likert""likert": stacked levels diverging from a neutral midpoint. "butterfly": two sides back to back (population pyramid).
neutralstring | nullthe middle level of an odd countLikert: sets the key of the neutral level.
normalizebooleantrue for Likert, false for butterfly (follows mode)Likert: draw shares (each row sums to 100%). Default true. Butterfly: draw each side's share of the grand total. Default false.
showNetbooleantrueLikert: print the net score (positive minus negative) after each row.
showValuesbooleantruePrint values in (Likert) or past (butterfly) the bars where they fit.
negativeColorChartColor8Likert: palette slots of the negative and positive poles (a diverging pair).
positiveColorChartColor7Sets thepalette slot (1 to 8) of the positive arm.
colorsreadonly [ChartColor, ChartColor][7, 5]Butterfly: palette slots of the left and right sides.
heightnumber200Sets the height of the plot in px.
format((n: number) => string)formatCompact in localeFormats a response count for the labels, tooltips and data table: takes the number, returns the text shown.
formatPercent((ratio: number) => string)percentages in localeFormats a share as text (0.42 → "42%").
localestring"en"Locale of the default number formats.
emptyStateReactNodeNoneShown when there is nothing to plot.
labelsPartial<DivergingBarChartLabels>defaultDivergingBarChartLabelsEvery other string, for translation: labels={{ roleDescription: "…" }}; omitted keys keep the English of defaultDivergingBarChartLabels. All 10 keys of DivergingBarChartLabels are listed in the key table under the props table.
aria-label (required)stringNoneNames the chart for assistive tech (required).
classNamestringNoneAdds extra classes to the outer element.
DivergingBarChartLabels keys and English defaults (10)
Pass any key in labels; the rest keep the defaults of defaultDivergingBarChartLabels.
KeyEnglish default
roleDescription(butterfly) => (butterfly ? "butterfly chart" : "diverging bar chart")
instructionsUse the up and down arrow keys to move between rows, and left and right to read each segment.
hintTap a bar to read it
totalNegativeAll negative
totalPositiveAll positive
netNet
totalTotal
shareShare
itemItem
noDataNo data

DivergingBarTable props

PropTypeDefaultDescription
data (required)readonly ChartRow[]NoneOne row per item (a question, a product, an age group).
neutralstring | nullthe middle level of an odd countLikert: sets the key of the neutral level.
format((n: number) => string)formatCompact in localeFormats a response count for the labels, tooltips and data table: takes the number, returns the text shown.
variant"likert" | "butterfly""likert""likert": stacked levels diverging from a neutral midpoint. "butterfly": two sides back to back (population pyramid).
labelsPartial<DivergingBarChartLabels>defaultDivergingBarChartLabelsEvery other string, for translation: labels={{ roleDescription: "…" }}; omitted keys keep the English of defaultDivergingBarChartLabels. All 10 keys of DivergingBarChartLabels are listed in the key table under the props table.
localestring"en"Locale of the default number formats.
index (required)stringNoneKey of the row name in each data row.
formatPercent((ratio: number) => string)percentages in localeFormats a share as text (0.42 → "42%").
levels (required)readonly DivergingLevel[]NoneLikert: the answer levels, most negative first ("Strongly disagree" … "Strongly agree"); with an odd count the middle one is neutral and straddles the centre line. Butterfly: exactly two levels, drawn to the left and right of the spine (e.g. Men / Women).
showNetbooleantrueLikert: print the net score (positive minus negative) after each row.
indexLabelstringNoneSets the header of the row-name column.
captionstringNoneHidden caption that names the table for screen readers.
classNamestringNoneAdds extra classes to the outer element.

Keyboard

KeysAction
TabFocus the chart. It is one tab stop; the arrow keys then read it.
↑/↓Move between rows.
←/→Move between a row's segments.
Home/EndJump to the first / last row.
EscapeClear the reading.

Accessibility

  • The figure takes focus as one stop and is named by its aria-label, the chart title. A one-line hint explains the keys, and a ChartCard table view is linked through aria-details. Values are announced politely, once per move. Shares are printed as text and every Likert row has a net score, so color never carries the reading; each move announces the row and segment.
  • DivergingBarTable is the table view: pair it with ChartCard's table prop (Chart / Table tabs) so every value can be read without color or sight.
  • Reduced motion: the bars appear at their final lengths on either side of zero and data changes jump instead of morphing.

All exports on this page

  • DivergingBarChart — Bars that diverge from a midpoint.
  • DivergingBarTable — Every row's count and share per level (plus the net score of a Likert row).
  • defaultDivergingBarChartLabels — The built-in English strings of DivergingBarChart; spread it into your own labels and change what you need.
  • divergingLegend — Legend items for a diverging chart, in level order (or the two sides of a butterfly).
  • divergingLevels — Each level's side and colour: equal steps per arm, strongest at the extremes, a grey neutral.

When to use the diverging bar chart

  • Use the likert variant as a Likert scale chart for survey results, such as strongly disagree through strongly agree per question, with each row summing to 100 percent and a net score after it.
  • Use the butterfly variant for a population pyramid or an A/B comparison, drawing two groups back to back from a central spine on one shared scale with row names down the middle.
  • Use it for sentiment by product feature or NPS-style breakdowns, where positive and negative answers should pull to opposite sides of a neutral midpoint.
  • Skip it when the categories are not ordered or have no meaningful midpoint, because a Bar chart or Percent bar chart fits unordered parts of a whole better.
  • Pair it with ChartCard and DivergingBarTable for a text view, ChartLegend to explain the answer levels, and a Select to switch the survey wave.

Frequently asked questions

48 charts pages in all: browse them on Charts.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .