Component guideEditable source

Chart

Theme-aware chart containers, tooltips, and legends for Recharts.

Original component docs
On this page5 sectionsBack to top

Use ChartContainer around a Recharts chart to supply series colors and labels. The wrapper is not a charting engine or a data source. The chart gallery contains eight independently installable analytical recipes with controls, calculated summaries, and exact data tables. All sample data is illustrative.

The preview below is the Release adoption recipe. Install that recipe for the complete screen; installing Chart alone supplies the primitives.

Live preview
Loading preview...

#Installation

Complete the base setup. For manual installation:

npx shadcn@latest add https://neobrutal-ui.andongmin.com/r/chart.json

#Usage

"use client";
import { Bar, BarChart, XAxis } from "recharts";
import {
  ChartContainer,
  ChartTooltip,
  ChartTooltipContent,
  type ChartConfig,
} from "@/components/ui/chart";

const data = [
  { month: "Jan", visits: 120 },
  { month: "Feb", visits: 180 },
];
const config = { visits: { label: "Visits", color: "var(--chart-1)" } } satisfies ChartConfig;

export default function VisitsChart() {
  return (
    <figure className="min-w-0">
      <figcaption>Visits increased from 120 in January to 180 in February.</figcaption>
      <ChartContainer config={config} className="h-64 w-full min-w-0 aspect-auto">
        <BarChart accessibilityLayer data={data}>
          <XAxis dataKey="month" />
          <ChartTooltip content={<ChartTooltipContent />} />
          <Bar dataKey="visits" fill="var(--color-visits)" />
        </BarChart>
      </ChartContainer>
    </figure>
  );
}

#Responsive layout

Give the surrounding layout an explicit flexible column, such as grid grid-cols-1, rather than an implicit auto column. A wide table in an automatic column can stretch every neighboring chart beyond a narrow viewport. For multiple columns, use tracks with a zero minimum, such as minmax(0, 1fr), and min-w-0 on nested grid or flex items.

Keep wide data tables in their own keyboard-accessible scroll area. Do not hide page overflow or shrink all text to fit; the plot and its description should reflow while exact table values remain reachable.

#API reference

Prop / partPurposeDefault
ChartContainer.configSeries labels and colors checked with ChartConfigRequired
ChartContainer.childrenA Recharts chartRequired
classNameChart height and responsive widthDefault aspect ratio
ChartTooltip / ChartTooltipContentTooltip primitive and styled content—
ChartLegend / ChartLegendContentLegend primitive and styled content—
ChartTooltipContent.indicatordot, line, or dasheddot
ChartTooltipContent.formatterRecharts callback returning a [formattedValue, formattedName] tuple or complete row contentDefault series name and value
ChartTooltipContent.htmlProps / ChartLegendContent.htmlPropsNative attributes, styles, events, and ref for the content's outer divUnset
ChartContainer.initialDimensionInitial responsive width and height before measurement, in pixels{ width: 320, height: 200 }
ChartTooltipContent.hideLabel / hideIndicatorHide the tooltip heading or series indicatorsfalse
ChartLegendContent.hideIconHide series icons in the legendfalse
ChartLegendContent.verticalAlignPosition legend spacing above or below the chartbottom

Return [formattedValue, formattedName] to keep the styled indicator, name, and value layout. Both parts can contain JSX; zero and empty names are preserved. A JSX or other non-array return replaces the complete row content. Returning null or undefined omits that row. The callback receives the original payload entry, index, and complete payload:

<ChartTooltip
  content={
    <ChartTooltipContent
      formatter={(value) => [`${Number(value).toLocaleString()} visits`, "Visits"]}
    />
  }
/>;

Pass DOM attributes through htmlProps when customizing tooltip or legend content. Keep className on the content component for appearance. Recharts supplies its own content props, so separating HTML props keeps chart configuration off the DOM:

import {
  ChartLegend,
  ChartLegendContent,
  ChartTooltip,
  ChartTooltipContent,
} from "@/components/ui/chart";

<ChartTooltip
  content={
    <ChartTooltipContent
      className="max-w-xs"
      htmlProps={{ id: "visits-tooltip", "aria-label": "Monthly visits" }}
    />
  }
/>;

<ChartLegend
  content={<ChartLegendContent htmlProps={{ "aria-label": "Visit series" }} />}
/>;

#Accessibility

Set a definite chart height so responsive measurement can work. Give each series a meaningful label and do not rely on color alone. Enable Recharts accessibility support and provide a textual summary or data table for important values. Tooltips alone are not an accessible substitute for the underlying data.