Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Charts

Free

Chart Tooltip

The tooltip every other variant starts from: the hovered day as a label, then each series with a color dot and its value, in React on Recharts.

Framework
Charting library

Preview

Preview width

About

Start here. Every other tooltip on this page changes one thing about this one: a bold label on top, then a row per series with a square dot in its color, its name, and its value in a monospace column. Use it whenever two or more series share a point and the reader needs the numbers.

Under it is a stack of running and swimming over six days in July, with weekday ticks and no value axis, so the tooltip is the only place the exact values show.

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 { ChartTooltipDefault } from "@/components/charts/chart-tooltip-default";
<ChartTooltipDefault />

Customize

  • <ChartTooltip content={<ChartTooltipContent />} /> with no props draws the default: the label, a dot per series and the values.
  • defaultIndex={1} opens the tooltip on the second day when the chart mounts. Remove it and nothing shows until hover.
  • cursor={false} turns off the band Recharts draws behind the hovered bar.
  • Both <Bar>s share stackId="a". radius={[0, 0, 4, 4]} rounds the bottom of running and [4, 4, 0, 0] the top of swimming.

FAQ

Why does the label show 2024-07-16 and not the weekday?

tickFormatter on <XAxis> only changes the ticks. ChartTooltipContent shows the raw date. Add labelFormatter={(value) => …} to format it, as the Label Formatter variant does.

Why does it list both series when I hover one segment?

Recharts' tooltip is shared: it reports every <Bar> at the hovered index, in the order the bars are declared, so both segments of the day get a row.

Can I move the tooltip with the keyboard?

Yes. accessibilityLayer on <BarChart> makes the chart focusable: Tab into it, then the left and right arrows move the tooltip from day to day.

Why is the tooltip open before I hover?

defaultIndex={1} on <ChartTooltip> shows the second day's tooltip when the chart mounts. Remove it and the tooltip waits for a pointer or a key.

How do I rename the series or change their colors?

Change label and color in chartConfig. The rows read their name from label and their dot from the series color: chartColors(chartConfig) gives the bars var(--color-running) and var(--color-swimming), and the tooltip takes the same.

How do I restyle the tooltip box?

Pass className to ChartTooltipContent; it merges with cn-chart-tooltip, the card your style preset draws in your theme's background, border and shadow, so it follows dark mode.

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.