Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Chart examples · Bar

A bar chart with an active bar for React

A bar chart of visitors by browser with one bar highlighted by a dashed outline.

Framework
UI library

Use it to call out one category, such as the one a walkthrough is talking about. Each browser's bar takes its own color from the data, and a shape function draws the bar at ACTIVE_INDEX with a lighter fill and a dashed outline in its own color.

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.

FAQ

How is the Firefox bar highlighted?

Bar sets shape to renderBar. That function compares props.index with ACTIVE_INDEX, which is 2, the Firefox row. A match renders Rectangle with fillOpacity 0.8, stroke from props.payload.fill, strokeDasharray 4, and strokeDashoffset 4.

How do I highlight a different bar?

Change ACTIVE_INDEX. The rows run Chrome, Safari, Firefox, Edge, Other, so 0 is Chrome. Any other index returns Rectangle with the bar props unchanged. Bar also sets radius to 8 and strokeWidth to 2, which is the outline width on the dashed bar.

Why is each bar a different color?

Each row sets fill to a variable such as var(--color-chrome). config maps chrome through other to --chart-1 through --chart-5 and to labels like Chrome. Bar has dataKey visitors and leaves fill to the row.

How do the ticks get names like Chrome?

XAxis uses dataKey browser and tickFormatter set to formatBrowser, which returns chartConfig[value].label. axisLine and tickLine are false, and tickMargin is 10.

What does the tooltip show on a bar?

ChartTooltipContent sets hideLabel, and cursor is false. The row name comes from the browser key in config. CartesianGrid sets vertical to false.

Does the keyboard reach these bars?

Yes. BarChart sets accessibilityLayer. The same config labels feed the axis ticks and the tooltip, so each browser is named in text as well as drawn in its own color.