Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Charts

Free

Interactive Bar Chart

Three months of daily bars with a header that switches between desktop and mobile and shows each total. An interactive bar chart for React.

Framework
Charting library

Preview

Preview width

About

Use it when one chart should cover more than one metric and the reader picks: page views by device, sales by region. Each header button shows its series' total, so the number reads before the bars. To compare the series at once, use grouped bars or two lines.

Ninety-one daily bars fill the card's width, with a few dates along the bottom. Pressing Desktop or Mobile swaps the series and its color, and the tooltip names the full date.

Installation

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

Usage

import { ChartBarInteractive } from "@/components/charts/chart-bar-interactive";
<ChartBarInteractive />

Customize

  • useState holds activeChart. <Bar dataKey={activeChart}> draws that series, and its fill builds var(--color-<key>) from the same key.
  • The header buttons carry data-active, and useMemo sums each series once for the totals.
  • <XAxis minTickGap={32}> drops dates that would collide, and tickFormatter prints them as Apr 1.
  • <ChartTooltipContent nameKey="views" labelFormatter={…} /> names the row Page Views and titles it with the full date.

FAQ

How does the header switch the series?

onClick sets activeChart with useState, and <Bar dataKey={activeChart}> reads it. The active button gets data-active="true", which data-[active=true]:bg-muted/50 shades.

How do I add a third metric?

Add the field to every row, an entry to chartConfig, and the key to the ["desktop", "mobile"] array the header maps over. Then add it to the total that useMemo sums.

How do I show a shorter period?

Filter chartData before the chart reads it, as the Interactive Area Chart does with its 90, 30 and 7 day select. Fewer days give wider bars.

How are the dates formatted?

tickFormatter on <XAxis> prints Apr 1, and minTickGap={32} skips labels that would collide. The tooltip's labelFormatter prints the full date with the year.

Why does the tooltip say Page Views?

<ChartTooltipContent nameKey="views" /> looks the row's name up under views in chartConfig, whose label is Page Views. Remove nameKey to show Desktop or Mobile.

How do I keep the chosen series after a reload?

Keep the active key in the URL's search params or in localStorage, and read it when the component starts. The chart only needs that one key.

API Reference

ChartContainer

PropType
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>

PropType
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>

PropType
configChartConfig

The chart config whose colors it writes as --color-<key> variables.

idstring

The element's id.

ChartTooltip

ChartTooltipContent

<div>

PropType
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 – any

payload – 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.