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.
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
Installation
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.
Related
- A bar chartA bar chart with rounded corners
- A mixed bar chartA horizontal bar chart with a different
- A multiple bar chartA bar chart grouping desktop and mobile
- An interactive bar chartA bar chart of three months of daily page
- A bar chart with negative valuesA bar chart with bars above and below
- A line chart with dotsA line chart with a dot on every data