Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Charts

Free

Chart Tooltip with Formatter

A tooltip whose rows carry their own unit, 450 kcal not 450, with the date heading and the color dots dropped, on a stacked bar chart in React.

Framework
Charting library

Preview

Preview width

About

Reach for it when a bare number would be ambiguous: calories, minutes, dollars. Each row keeps the series name on the left and puts the value on the right with its unit in muted text, and the date heading and color dots are gone, so the tooltip is two compact lines.

Without dots, the names carry the series. That works with two well-named series; with more, or with similar names, keep the dots and use the Advanced variant.

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

Customize

  • formatter={(value, name) => …} replaces each row's content, including the indicator, so there are no dots.
  • chartConfig[name]?.label turns the data key back into Running or Swimming.
  • hideLabel drops the heading; defaultIndex={1} opens the tooltip on the second day.
  • min-w-[130px] on the row keeps the tooltip from narrowing as the values change.

FAQ

How is the unit added to each value?

formatter={(value, name) => …} on ChartTooltipContent returns the whole row: the chartConfig label for name, then the value and a kcal span in text-muted-foreground.

Where did the color dots go?

formatter replaces the entire row, indicator included. Render a swatch yourself at the start, a size-2.5 square painted with the series' --color- variable for name, as the Advanced variant does.

How do I format the numbers themselves?

Format the number inside the formatter. value.toLocaleString() adds thousands separators, and Intl.NumberFormat with style: "currency" or style: "unit" writes dollars, percents or kilometers for the reader's locale.

Can I keep the date above the rows?

Remove hideLabel from ChartTooltipContent. Add labelFormatter too if the raw 2024-07-16 should read as a date.

Why do the numbers line up?

The value sits in font-mono tabular-nums, so every digit has the same width and the values line up down the column; the unit follows in a lighter weight with items-baseline.

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.