shadcn.io is not affiliated with official shadcn/ui
About
Pick it when the points are named categories and you'd rather name them on the chart than under it. The names sit next to the data, so the eye doesn't travel to an axis and back. For values instead of names, use the Line Chart with Labels; when color should carry the category, the colored dots version.
A natural curve through five browsers, Chrome to Other, with a dot on each point and the browser's name above it in the text color. There's no x axis, the margins leave room for the end labels, and the tooltip shows the visitor count alone.
Installation
Usage
import { ChartLineLabelCustom } from "@/components/charts/chart-line-label-custom";<ChartLineLabelCustom />Customize
<LabelList dataKey="browser" position="top" offset={12} />inside<Line>prints each row's browser above its point.- Its
formattermaps the key tochartConfig[value].label, sochromeprints as Chrome. className="fill-foreground"andfontSize={12}style the labels; there's no<XAxis>, since the labels name the points.margin={{ left: 24, right: 24, top: 24 }}keeps the first, last and highest labels inside the card.<ChartTooltipContent hideLabel indicator="line" nameKey="visitors" />names the row fromchartConfig.visitors.
FAQ
Where do the label names come from?
From dataKey="browser" on <LabelList>, passed through a formatter that returns chartConfig[value]?.label. Without the formatter you'd see the raw keys, chrome and safari.
Can a label show the name and the value?
Yes. Give <LabelList> a content function that returns an SVG <text> with both, or add a second <LabelList dataKey="visitors" position="bottom" /> under each point.
What about long category names?
Shorten them in the formatter, or use content to render a rotated <text>. The margins have to fit the longest label, so test with real names.
Should I bring back the x axis?
Not while every point has a label: the names would appear twice. Bring it back if you label only some points, or if the line has more categories than fit as labels.
API Reference
ChartContainer
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>
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>
configChartConfig
The chart config whose colors it writes as --color-<key> variables.
idstring
The element's id.
ChartTooltip
ChartTooltipContent
<div>
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– anypayload– 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.
Related
Line Chart with Labels
Line chartsEach point's value printed above the line.
Line Chart with Colored Dots
Line chartsA line whose dots each take their category's color.
Bar Chart with Custom Labels
Bar chartsHorizontal bars with the name inside each bar and the value after it.
Pie Chart with Label List
Pie chartsEach slice's name printed inside it.
Radar Chart with Custom Labels
Radar chartsAxis labels that show each month's values above its name.
Line Chart
Line chartsOne series as a smooth line over six months.