Skip to content

shadcn.io is an independent project built on shadcn/ui

Diverging Bars with a Legend

Four answers with no neutral, custom fills, and a legend that lights the hovered answer in every plan. This is the React version, drawn with TanStack Charts.

Charting library: TanStack Charts

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
Use email insteadNo credit card

Usage

import { ChartDivergingLegend } from "@/components/charts/chart-diverging-legend";
<ChartDivergingLegend />

API Reference

ChartLegendHoverProvider

PropType
hoveredIndexnumber | null
Type
number | null
onHoverChange(index: number | null) => void
Type
(index: number | null) => void
childrenReactNode

DivergingBarChart

PropType
datareadonly Record<string, unknown>[]

One row per bar: its yDataKey names it, each DivergingSegment's dataKey is a count or value.

Type
readonly Record<string, unknown>[]
yDataKeystring

The key of each row's name.

center?string

The dataKey of the neutral segment, split across the centre line. Without one the centre falls between the first and second halves of the segments (an odd count splits its middle one).

normalize?boolean

Show each bar as shares of its row (every bar the same length) rather than raw values.

Default
true
barSize?number

The bars' thickness as a share of their row.

Default
0.62
radius?number

Corner radius of the bars' ends, in px.

Default
4
formatValue?(value: number) => string

Formats a value in the tooltip (the raw value; shares are shown beside it).

Type
(value: number) => string
onHoverChange?(index: number | null) => void

Called with the hovered segment's series index, and null when the pointer leaves (pair it with a ChartLegend).

Type
(index: number | null) => void
aspectRatio?string | number

Width over height: a number or a string such as "16 / 9".

Type
string | number
Default
"16 / 9"
height?number

A fixed height in px instead of an aspect ratio.

animate?boolean

Play the entrance: the segments grow out from the centre line, row by row.

Default
true
status?ChartStatus

"loading" draws a shimmering skeleton (muted bars split at the centre line) in place of the chart and ignores the pointer; switching to "ready" plays the entrance. Switching back to "loading" fades the chart out first.

Default
"ready"
loadingLabel?string

Text shown centered over the plot, with a shimmer, while status is "loading". Nothing is shown when it is unset or empty.

ariaLabel?string
ariaDescription?string
className?string

Extra class name for the chart's root element.

style?CSSProperties

Inline style for the chart's root element.

children?ReactNode

Carriers: ChartTooltip and the family's own.

DivergingSegment

PropType
dataKeystring

The key of its value in each row.

label?string

Its name in the tooltip and legend. Defaults to the dataKey.

fill?string

Its colour. Defaults to a diverging scheme from the theme: --chart-negative tints before the centre, a muted neutral at it, --chart-positive tints after it, strongest at the ends.

XAxis

PropType
numTicks?number | ResponsiveChartLabelCount

Target number of tick labels along the x axis, or one per chart width: { base, sm, md, lg, xl } takes the count of the widest breakpoint the chart reaches (640, 768, 1024 and 1280 px).

Type
number | ResponsiveChartLabelCount
Default
5
labels?readonly string[]

The tick text of each data row, in row order: it replaces the date formatting (pair it with ChartTooltip dateLabels).

tickerHalfWidth?number

Half-width in px of the zone around the date pill inside which tick labels are hidden. Line, area and composed charts widen it to the pill's half-width plus the widest tick label's, so no label sits half under the pill.

Default
50
tickMode?"domain" | "data"

Where ticks fall: "data" places them on evenly spaced data rows, "domain" places them at evenly spaced time values across the x domain.

Type
"domain" | "data"
Default
"data"
formatValue?(value: Readonly<Date>) => string

Formats a tick date into its label. Defaults to a short month and day such as "Jan 5".

Type
(value: Readonly<Date>) => string

YAxis

PropType
yAxisId?string | number

Id of the y scale this axis labels. Must match the yAxisId of the series that use it.

Type
string | number
Default
"left"
orientation?"left" | "right"

Side of the plot the axis sits on. The primary axis is on the left; additional axes default to the right.

Type
"left" | "right"
Default
"left"
numTicks?number

Target number of tick labels. The primary axis also sets the number of horizontal grid lines.

Default
5
formatLargeNumbers?boolean

Abbreviate values of 1000 and above as thousands, with one decimal where needed, for example 2k or 1.5k.

Default
true
formatValue?(value: number) => string

Formats a tick value into its label. Overrides formatLargeNumbers.

Type
(value: number) => string

ChartTooltip

PropType
showDatePill?boolean

Show the date pill that rides along the bottom of the plot.

Default
true
showCrosshair?boolean

Show the vertical crosshair line. Bar charts leave it off unless this is true (the dots and the category pill mark the bar).

Default
true
showDots?boolean

Show a dot on every series at the hovered position.

Default
true
dotVariant?"dot" | "ring"

Dot style: a filled "dot" or a transparent "ring" square. Read by BarChart only.

Type
"dot" | "ring"
Default
"dot"
dotSize?number

Dot radius in px.

Default
5
dotRadiusFraction?number

Corner radius of the "ring" variant as a fraction of its side (0 is square, 0.5 is a circle). Read by BarChart only.

Default
0.25
dotScale?number

Multiplier applied to the dot or ring size. Read by BarChart and LiveLineChart.

Default
1
dotStrokeWidth?number

Stroke width of the dots in px.

Default
2
indicatorColor?string | ((point: Record<string, unknown>) => string)

Color of the crosshair. Only a string is applied; the function form that bklit-ui accepts is ignored and the default is used.

Type
string | ((point: Record<string, unknown>) => string)
Default
"var(--chart-crosshair)"
content?(props: { point: Record<string, unknown>; index: number; pinned?: boolean; dismiss?: () => void }) => ReactNode

Replaces the default tooltip panel body. Receives the hovered data row and its index; with pinnable, also whether the panel is pinned and a dismiss that unpins it (for a close button in a pinned panel's detail).

Type
(props: { point: Record<string, unknown>; index: number; pinned?: boolean; dismiss?: () => void }) => ReactNode
pinnable?boolean

A click (or Enter, or Space) on the chart pins the tooltip where it is: the panel takes the pointer, so its content can grow into detail and hold controls, until another click, Escape or dismiss lets it go.

Default
false
rows?(point: Record<string, unknown>) => { readonly color: string; readonly label: string; readonly value: string | number }[]

Replaces the default rows (one per series) with your own list of { color, label, value }.

Type
(point: Record<string, unknown>) => { readonly color: string; readonly label: string; readonly value: string | number }[]
dotColor?string | ((point: Record<string, unknown>, line: ChartLineConfig) => string)

Fill of the tooltip dots. A function receives the hovered row and the series config, and is called again each time the hovered row changes. Defaults to each series' color; a ProfitLossLine's dot takes the color of the hovered value's sign.

Type
string | ((point: Record<string, unknown>, line: ChartLineConfig) => string)
children?ReactNode

Extra content rendered below the rows in the default panel, for example marker details. Without it, a chart with ChartMarkers lists the markers on the hovered date there.

className?string

Extra class name for the tooltip panel.

springConfig?SpringConfig

Spring for the floating panel when matchCrosshair is on. Defaults to the chart's tooltip spring.

matchCrosshair?boolean

Make the floating panel follow the pointer with the same spring as the crosshair instead of damping.

Default
false
damping?number

Spring damping for the floating panel when matchCrosshair is off, on bklit's 0 to 100 scale: lower is bouncier, higher slower, 0 moves the panel instantly. The default, 20, is the snappy hover spring (stiffness 3600, damping 120); every other value is bklit's spring for it played six times faster.

Default
20
indicatorDasharray?string

Dash pattern of the crosshair. Omit for a solid line.

indicatorFadeEdges?IndicatorFadeEdges

Vertical fade of the crosshair: both ends, the top, the bottom, or none (solid).

Default
"both"
indicatorFadeLength?number

Size of the crosshair's fade, as a percentage of its height (2 to 40).

Default
10
indicatorEndColor?string

Bottom colour of the crosshair: it ramps from indicatorColor at the top to this at the bottom (composes with the fade).

indicatorEnd?"plot" | "point"

How far down the crosshair reaches: the whole plot, or down to the hovered point of the first series.

Type
"plot" | "point"
Default
"plot"
dotHaloBlur?number

Blur in px on the dots' halo, for a soft glow.

Default
0
topOffset?number

Extra gap in px between the top of the plot and the tooltip panel.

Default
0
dateLabels?readonly string[]

The date pill's text for each data row, in row order (pair it with XAxis labels).

boxSpringConfig?SpringConfig

Spring for the floating panel. Takes priority over matchCrosshair and damping.

panelStyle?CSSProperties

Accepted for bklit-ui compatibility. It has no effect in bklit-tanstack-charts. Style the panel with className or the --chart-tooltip-* CSS variables instead.

backgroundColor?string

Accepted for bklit-ui compatibility. It has no effect in bklit-tanstack-charts. Set --chart-tooltip-background instead.

indicatorWidth?IndicatorWidth

Thickness of the crosshair: a width in px, or "line" (1px), "thin" (2px), "medium" (4px) or "thick" (8px).

Default
"line"
columnWidth?number

Accepted for bklit-ui compatibility. It has no effect in bklit-tanstack-charts.

ChartLegend

PropType
itemsLegendItem[]

The items to list.

hoveredIndex?number | null

Hovered item index (null for none). Pair it with onHover to share hover with a chart.

Type
number | null
Default
null
onHover?(index: number | null) => void

Called with the item index when the pointer enters a row, and null when it leaves.

Type
(index: number | null) => void
showProgress?boolean

Show a progress bar under each item that has a maxValue.

Default
false
showMarker?boolean

Show the colored dot before each label.

Default
true
showValue?boolean

Show each item's value.

Default
true
showPercentage?boolean

Show each item's percentage of maxValue, under its progress bar: it takes effect only with showProgress and an item maxValue, as in bklit-ui (chart-legend.tsx:115, where the plain row has no percentage). For a percentage without a bar, compose Legend with LegendValue showPercentage.

Default
the value of `showProgress`
formatValue?(value: number) => string

Formats each value.

Type
(value: number) => string
Default
en-US number format, for example "1,234"
title?string

Heading shown above the items.

className?string

Class names added to the legend container.

titleClassName?string

Class names added to the title, on top of the default title style.

itemClassName?string

Class names added to each row.

labelClassName?string

Class names that replace the default label text style.

valueClassName?string

Class names that replace the default value text style.

renderItem?(props: Readonly<{ readonly item: LegendItem; readonly index: number; readonly isHovered: boolean; readonly isFaded: boolean; readonly percentage: number }>) => ReactNode

Renders a row yourself. Receives the item, its index, hover flags and its percentage of maxValue. When set, the show* options are ignored.

Type
(props: Readonly<{ readonly item: LegendItem; readonly index: number; readonly isHovered: boolean; readonly isFaded: boolean; readonly percentage: number }>) => ReactNode