shadcn.io is not affiliated with official shadcn/ui
About
Use it to point at one category in a set: the plan you're selling, this month's leader, the item the page is about. The highlight comes from the data, not the hover, so it holds in a screenshot. For colors without a single focus, the mixed bar chart fits.
Five rounded bars stand in their browsers' colors. The third, Firefox, keeps its color at 80% and gets a 2px dashed outline in the same color. The tooltip shows the visitor count without a title.
Installation
Usage
import { ChartBarActive } from "@/components/charts/chart-bar-active";<ChartBarActive />Customize
shapeon<Bar>draws each bar: a<Rectangle>withfillOpacity={0.8}, a stroke in the row'sfillandstrokeDasharray={4}whenindex === ACTIVE_INDEX, a plain<Rectangle>otherwise.ACTIVE_INDEXis2, the third row. Change it to highlight another bar.- Each row carries
fill: "var(--color-<browser>)", andstrokeWidth={2}on<Bar>sets the outline's width. - The
<XAxis>tickFormatterlooks each key up inchartConfigto print its label.
FAQ
How do I change which bar is active?
Change ACTIVE_INDEX: it's the row's position in chartData, 2 for Firefox. To pick a bar by name, compare props.payload.browser inside shape instead of the index.
How is the dashed outline drawn?
The active <Rectangle> gets stroke={props.payload.fill}, strokeDasharray={4} and strokeDashoffset={4}. Its width comes from strokeWidth={2} on <Bar>.
Can the active bar follow the pointer?
Yes. Pass the active props to activeBar on <Bar> instead of branching in shape: Recharts draws that style on the hovered bar.
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
Mixed Bar Chart
Bar chartsHorizontal bars, each category in a color of its own.
Bar Chart
Bar chartsOne series of rounded vertical bars, a month each.
Donut Chart with Active Sector
Pie chartsA donut with one slice pulled out as active.
Bar Chart with Labels
Bar chartsEach bar's value printed above it.
Interactive Bar Chart
Bar chartsThree months of daily bars, switched between two series.
Multiple Bar Chart
Bar chartsTwo series grouped side by side for each month.