shadcn.io is not affiliated with official shadcn/ui
About
Use it for values that cross zero: profit and loss, change from last month, a net score. The baseline splits up from down and the color says it twice. When nothing goes below zero, the plain bar chart is enough.
Six square bars rise from or drop below zero. Positive months use the first chart color and the two negative months the second, and each bar carries its month's name at its outer end. There are no axes, only grid lines.
Installation
Usage
import { ChartBarNegative } from "@/components/charts/chart-bar-negative";<ChartBarNegative />Customize
- Each row gets a
<Cell>inside<Bar>whosefilldepends on the sign.Cellis deprecated in Recharts 3; ashapefunction on<Bar>does the same. <LabelList dataKey="month" position="top" />writes the month at each bar's outer end.<CartesianGrid vertical={false} />is the only guide: there's no<XAxis>or<YAxis>.<ChartTooltipContent hideIndicator hideLabel />leaves the value and its label.
FAQ
How are the bars colored by sign?
A <Cell> per row sets fill from item.visitors > 0. Recharts 3 deprecates Cell, so in new code put the same check in a shape function on <Bar>.
How do I center zero in the chart?
Add a <YAxis hide domain={[-max, max]} />, with max the largest absolute value. Zero then sits in the middle.
API Reference
ChartContainer
configChartConfig
Each series by its data key: its label, an icon, and a color (or a * theme color per light and dark), read by the tooltip and legend and set * as --color-<key>.
initialDimension?{ height: number; width: number; }
The size it renders at before it's measured, so the first paint isn't * empty.
- Type
{ height: number; width: number; }- Default
INITIAL_DIMENSION
className?string
Merged with the part's classes.
ChartLegend
ChartLegendContent
<div>
hideIcon?boolean
Hides each series' icon (from config), leaving its color swatch.
- Default
false
nameKey?string
The key in each legend item's data to look its label up by in config. * Defaults to the item's dataKey.
className?string
Merged with the part's classes.
ChartStyle
<style>
configChartConfig
The chart config whose colors it writes as --color-<key> variables.
idstring
The element's id.
ChartTooltip
ChartTooltipContent
<div>
hideIndicator?boolean
Hides each item's color indicator.
- Default
false
hideLabel?boolean
Hides the tooltip's label, the hovered point's.
- Default
false
indicator?"dashed" | "dot" | "line"
The color indicator's shape: a dot, a line or dashed.
- Type
"dashed" | "dot" | "line"- Default
"dot"
labelFormatter?(label: any, payload: TooltipPayload) → React.ReactNode
Formats the tooltip's label; its value is untyped, as in recharts 3.8.
- Type
(label: any, payload: TooltipPayload) → React.ReactNode- Parameters
label– anypayload– TooltipPayload- Returns
React.ReactNode
labelKey?string
The key in the data to read the label from, looked up in config.
nameKey?string
The key in each item's data to look its name up by in config.
className?string
Merged with the part's classes.
Related
Bar Chart
Bar chartsOne series of rounded vertical bars, a month each.
Bar Chart with Labels
Bar chartsEach bar's value printed above it.
Bar Chart with Active Bar
Bar chartsOne bar drawn as active, outlined and dashed, the rest plain.
Line Chart with Labels
Line chartsEach point's value printed above the line.
Step Area Chart
Area chartsA stepped area for values that change at fixed points.
Multiple Bar Chart
Bar chartsTwo series grouped side by side for each month.