Skip to content
SevenOne

Search documentation

Search pages and components

ChartPro

Bar, line, area, donut and radial charts that use your theme. Series colors come from chart-1 to chart-5; every text uses the text colors at 16px. Tooltip, legend and a data table come ready. Built on Recharts.

Installation

This component is part of SevenOne Pro.

The Pro license includes the code of every Pro component, the complete Figma file and the Angular version.

Usage

import { Bar, BarChart, CartesianGrid, XAxis } from "recharts"
import {
  ChartContainer,
  ChartLegend,
  ChartLegendContent,
  ChartTooltip,
  ChartTooltipContent,
  type ChartConfig,
} from "@/components/ui/chart"

const chartConfig = {
  desktop: { label: "Desktop", color: "var(--chart-1)" },
  mobile: { label: "Mobile", color: "var(--chart-2)" },
} satisfies ChartConfig

<ChartContainer config={chartConfig} className="min-h-50 w-full">
  <BarChart accessibilityLayer data={chartData} aria-label="Visitors by month, bar chart">
    <CartesianGrid vertical={false} />
    <XAxis dataKey="month" tickLine={false} axisLine={false} />
    <ChartTooltip content={<ChartTooltipContent />} />
    <ChartLegend content={<ChartLegendContent />} />
    <Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
    <Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
  </BarChart>
</ChartContainer>

Examples

Bar chart

Hover a bar, or focus the chart and use the arrow keys, to see the tooltip.

Visitors from January to June. Desktop peaked in February (305).
const chartData = [
  { month: "January", desktop: 186, mobile: 80 },
  { month: "February", desktop: 305, mobile: 200 },
  { month: "March", desktop: 237, mobile: 120 },
  { month: "April", desktop: 73, mobile: 190 },
  { month: "May", desktop: 209, mobile: 130 },
  { month: "June", desktop: 214, mobile: 140 },
]

const chartConfig = {
  desktop: { label: "Desktop", color: "var(--chart-1)" },
  mobile: { label: "Mobile", color: "var(--chart-2)" },
} satisfies ChartConfig

<figure>
  <ChartContainer config={chartConfig} className="min-h-50 w-full">
    <BarChart accessibilityLayer data={chartData} aria-label="Visitors by month, bar chart">
      <CartesianGrid vertical={false} />
      <XAxis dataKey="month" tickLine={false} tickMargin={10} axisLine={false} tickFormatter={(v) => v.slice(0, 3)} />
      <ChartTooltip content={<ChartTooltipContent />} />
      <ChartLegend content={<ChartLegendContent />} />
      <Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
      <Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
    </BarChart>
  </ChartContainer>
  <figcaption className="mt-3 text-center text-sm text-muted-foreground">
    Visitors from January to June. Desktop peaked in February (305).
  </figcaption>
</figure>

Stacked bars

A 2px gap in the background color separates the segments.

Total visitors per month. February had the most (505).
<ChartContainer config={chartConfig} className="min-h-50 w-full">
  <BarChart accessibilityLayer data={chartData} aria-label="Visitors by month, stacked bar chart">
    <CartesianGrid vertical={false} />
    <XAxis dataKey="month" tickLine={false} tickMargin={10} axisLine={false} tickFormatter={(v) => v.slice(0, 3)} />
    <ChartTooltip content={<ChartTooltipContent indicator="line" />} />
    <ChartLegend content={<ChartLegendContent />} />
    <Bar dataKey="desktop" stackId="a" fill="var(--color-desktop)" stroke="var(--background)" strokeWidth={2} radius={[0, 0, 4, 4]} />
    <Bar dataKey="mobile" stackId="a" fill="var(--color-mobile)" stroke="var(--background)" strokeWidth={2} radius={[4, 4, 0, 0]} />
  </BarChart>
</ChartContainer>

Horizontal bars

One series needs no legend: the caption names it.

Desktop visitors per month. April was the lowest (73).
<ChartContainer config={chartConfig} className="min-h-50 w-full">
  <BarChart accessibilityLayer data={chartData} layout="vertical" margin={{ left: -8 }} aria-label="Desktop visitors by month, bar chart">
    <CartesianGrid horizontal={false} />
    <XAxis type="number" dataKey="desktop" hide />
    <YAxis dataKey="month" type="category" tickLine={false} tickMargin={10} axisLine={false} tickFormatter={(v) => v.slice(0, 3)} />
    <ChartTooltip content={<ChartTooltipContent hideLabel />} />
    <Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
  </BarChart>
</ChartContainer>

Line

2px lines, 8px markers with a 2px ring. The second series is dashed, so it never depends on color.

Mobile passed desktop only in April (190 vs 73).
<ChartContainer config={chartConfig} className="min-h-50 w-full">
  <LineChart accessibilityLayer data={chartData} margin={{ left: 24, right: 24, top: 8 }} aria-label="Visitors by month, line chart">
    <CartesianGrid vertical={false} />
    <XAxis dataKey="month" tickLine={false} tickMargin={10} axisLine={false} tickFormatter={(v) => v.slice(0, 3)} />
    <ChartTooltip cursor={false} content={<ChartTooltipContent indicator="line" />} />
    <ChartLegend content={<ChartLegendContent />} />
    <Line
      dataKey="desktop"
      type="monotone"
      stroke="var(--color-desktop)"
      strokeWidth={2}
      dot={{ r: 5, strokeWidth: 2, fill: "var(--color-desktop)", stroke: "var(--background)" }}
    />
    <Line
      dataKey="mobile"
      type="monotone"
      stroke="var(--color-mobile)"
      strokeWidth={2}
      strokeDasharray="6 4"
      dot={{ r: 5, strokeWidth: 2, fill: "var(--color-mobile)", stroke: "var(--background)" }}
    />
  </LineChart>
</ChartContainer>

Area

Stacked visitors from January to June.
<ChartContainer config={chartConfig} className="min-h-50 w-full">
  <AreaChart accessibilityLayer data={chartData} margin={{ left: 24, right: 24, top: 8 }} aria-label="Visitors by month, area chart">
    <CartesianGrid vertical={false} />
    <XAxis dataKey="month" tickLine={false} tickMargin={10} axisLine={false} tickFormatter={(v) => v.slice(0, 3)} />
    <ChartTooltip cursor={false} content={<ChartTooltipContent indicator="dot" />} />
    <ChartLegend content={<ChartLegendContent />} />
    <Area dataKey="mobile" type="natural" fill="var(--color-mobile)" fillOpacity={0.4} stroke="var(--color-mobile)" strokeWidth={2} stackId="a" />
    <Area dataKey="desktop" type="natural" fill="var(--color-desktop)" fillOpacity={0.4} stroke="var(--color-desktop)" strokeWidth={2} stackId="a" />
  </AreaChart>
</ChartContainer>

Donut with total

Up to 5 slices, always with the legend on.

Chrome is the top browser: 275 of 925 visitors.
const chartData = [
  { browser: "chrome", visitors: 275, fill: "var(--color-chrome)" },
  { browser: "safari", visitors: 200, fill: "var(--color-safari)" },
  { browser: "firefox", visitors: 187, fill: "var(--color-firefox)" },
  { browser: "edge", visitors: 173, fill: "var(--color-edge)" },
  { browser: "other", visitors: 90, fill: "var(--color-other)" },
]

const chartConfig = {
  visitors: { label: "Visitors" },
  chrome: { label: "Chrome", color: "var(--chart-1)" },
  safari: { label: "Safari", color: "var(--chart-2)" },
  firefox: { label: "Firefox", color: "var(--chart-3)" },
  edge: { label: "Edge", color: "var(--chart-4)" },
  other: { label: "Other", color: "var(--chart-5)" },
} satisfies ChartConfig

<ChartContainer config={chartConfig} className="mx-auto aspect-square max-h-75">
  <PieChart accessibilityLayer aria-label="Visitors by browser, donut chart">
    <ChartTooltip cursor={false} content={<ChartTooltipContent hideLabel nameKey="browser" />} />
    <Pie data={chartData} dataKey="visitors" nameKey="browser" innerRadius="60%" strokeWidth={4}>
      <Label
        content={({ viewBox }) =>
          viewBox && "cx" in viewBox && "cy" in viewBox ? (
            <text x={viewBox.cx} y={viewBox.cy} textAnchor="middle" dominantBaseline="middle">
              <tspan x={viewBox.cx} y={viewBox.cy} className="fill-foreground text-3xl font-semibold">
                925
              </tspan>
              <tspan x={viewBox.cx} y={(viewBox.cy || 0) + 28} className="fill-muted-foreground">
                Visitors
              </tspan>
            </text>
          ) : null
        }
      />
    </Pie>
    <ChartLegend content={<ChartLegendContent nameKey="browser" />} />
  </PieChart>
</ChartContainer>

Radial progress

72 GB of 100 GB used.
const chartConfig = {
  used: { label: "Storage used", color: "var(--chart-1)" },
} satisfies ChartConfig

<ChartContainer config={chartConfig} className="mx-auto aspect-square max-h-62">
  <RadialBarChart
    data={[{ name: "used", value: 72, fill: "var(--color-used)" }]}
    startAngle={90}
    endAngle={-270}
    innerRadius="72%"
    outerRadius="100%"
    accessibilityLayer={false}
  >
    <PolarAngleAxis type="number" domain={[0, 100]} tick={false} axisLine={false} />
    <RadialBar dataKey="value" background cornerRadius={8} />
    <text x="50%" y="50%" textAnchor="middle" dominantBaseline="middle">
      <tspan x="50%" dy="-0.2em" className="fill-foreground text-3xl font-semibold">72%</tspan>
      <tspan x="50%" dy="1.6em" className="fill-muted-foreground">Storage used</tspan>
    </text>
  </RadialBarChart>
</ChartContainer>

With data table

The same numbers as a real table, one click away.

Visitors from January to June.
const [showTable, setShowTable] = React.useState(false)

<figure>
  <Button variant="outline" aria-expanded={showTable} aria-controls="visitors-table" onClick={() => setShowTable((v) => !v)}>
    {showTable ? "Hide table" : "Show table"}
  </Button>
  <ChartContainer config={chartConfig} className="min-h-50 w-full">
    <BarChart accessibilityLayer data={chartData} aria-label="Visitors by month, bar chart">{/* … */}</BarChart>
  </ChartContainer>
  <figcaption>Visitors from January to June.</figcaption>
  <div id="visitors-table" hidden={!showTable}>
    <ChartDataTable
      data={chartData}
      config={chartConfig}
      labelKey="month"
      labelHeader="Month"
      caption="Visitors from January to June"
    />
  </div>
</figure>

Accessibility

  • Never show a chart alone: add a caption with the takeaway and a data table (ChartDataTable) or a text summary (WCAG 1.1.1, 1.3.1).
  • accessibilityLayer makes the chart focusable; the arrow keys move the tooltip from point to point, with a visible 2px focus ring. Name it with aria-label ("Visitors by month, bar chart") (WCAG 2.1.1, 2.4.7, 4.1.2).
  • A chart with nothing to explore (a single value, like the radial progress) turns accessibilityLayer off, so it is not an empty tab stop.
  • A series is never identified by color alone: the legend and the tooltip name every series, and lines can be dashed. chart-2 is 2.4:1 against the light background, so it always comes with labels (WCAG 1.4.1, 1.4.11).
  • Tooltip, legend and axis texts use foreground and muted-foreground at 16px, never the series color (WCAG 1.4.3, 1.4.6).
  • Pies and donuts: up to 5 slices, legend on. More categories go to a bar chart.