Components
Charts

Charts

Simple, responsive data visualization powered by Recharts, ideal for creating clean and interactive charts that adjust seamlessly to any screen size. Designed for flexibility, it allows for easy customization and integration across various applications.

Bar Chart - Interactive

Showing total visitors for the last 3 months

Charts Library/ Examples

Browse the complete Charts Library.

Component

Built with ease-of-use in mind, our chart component leverages Recharts under the hood for simple, responsive data visualization. We do not wrap Recharts, meaning you're not locked into an abstraction. This allows you to follow the official Recharts upgrade path when new versions are released. Instead, the Chart component is abstracted to let you seamlessly use Recharts' native components and integrate custom elements, such as ChartTooltip, only when needed—offering flexibility without unnecessary complexity.

import { Bar, BarChart } from "recharts"
 
import { ChartContainer, ChartTooltipContent } from "@/components/ui/charts"
 
export function MyChart()
  return (
    <ChartContainer>
      <BarChart data={data}>
        <Bar dataKey="value" />
        <ChartTooltip content={<ChartTooltipContent />} />
      </BarChart>
    </ChartContainer>
  )
}

Building from Scratch

Building charts is easy and can be done in one of two ways: either by developing one from scratch, ideal for those needing custom or advanced functionality, or by leveraging one of the many pre-built charts available in the library, which can be quickly customized to fit specific data needs.

Start by defining your data

The following sample data represents the number of desktop and mobile users for each month.

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 },
]

Define your chart config

The chart config stores all configuration settings for the chart. This is where you define human-readable strings, such as labels, icons, and color tokens for theming, ensuring that your chart is reusable and easily customizable.

import { type ChartConfig } from "@/components/ui/chart"
 
const chartConfig = {
  desktop: {
    label: "Desktop",
    color: "#2563eb",
  },
  mobile: {
    label: "Mobile",
    color: "#60a5fa",
  },
} satisfies ChartConfig

Build your chart

You can now build your chart using Recharts components.

"use client"
 
import { Bar, BarChart } from "recharts"
 
import { ChartConfig, ChartContainer } from "@/components/ui/chart"
 
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: "#2563eb",
  },
  mobile: {
    label: "Mobile",
    color: "#60a5fa",
  },
} satisfies ChartConfig
 
export function Component() {
  return (
    <ChartContainer config={chartConfig} className="min-h-[200px] w-full">
      <BarChart accessibilityLayer data={chartData}>
        <Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
        <Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
      </BarChart>
    </ChartContainer>
  )
}

Add a Grid

To add a grid to the chart, simply include the CartesianGrid component in your chart configuration. This will provide a structured layout, making the data easier to interpret by adding horizontal and vertical lines across the chart.

Import the CartesianGrid component.

import { Bar, BarChart, CartesianGrid } from "recharts"

Add the CartesianGrid component to your chart.

<ChartContainer config={chartConfig} className="min-h-[200px] w-full">
  <BarChart accessibilityLayer data={chartData}>
    <CartesianGrid vertical={false} />
    <Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
    <Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
  </BarChart>
</ChartContainer>

Add an Axis

To add an x-axis to the chart, use the XAxis component. This defines the horizontal axis of the chart, displaying the relevant data points.

Import the XAxis component.

import { Bar, BarChart, CartesianGrid, XAxis } from "recharts"

Add the XAxis component to your chart.

<ChartContainer config={chartConfig} className="h-[200px] w-full">
  <BarChart accessibilityLayer data={chartData}>
    <CartesianGrid vertical={false} />
    <XAxis
      dataKey="month"
      tickLine={false}
      tickMargin={10}
      axisLine={false}
      tickFormatter={(value) => value.slice(0, 3)}
    />
    <Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
    <Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
  </BarChart>
</ChartContainer>

Add Tooltip

So far, we’ve only utilized components from Recharts. These components look great right out of the box, thanks to built-in customization within the Chart component, allowing for visually appealing and functional charts with minimal configuration. But we can expand upon this native functionality with additional custom components. To add a tooltip, we’ll use the custom ChartTooltip and ChartTooltipContent components from the Chart component. These allow for more detailed and tailored tooltip designs, enhancing user interaction and data presentation.

Import the ChartTooltip and ChartTooltipContent components.

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

Add the components to your chart.

<ChartContainer config={chartConfig} className="h-[200px] w-full">
  <BarChart accessibilityLayer data={chartData}>
    <CartesianGrid vertical={false} />
    <XAxis
      dataKey="month"
      tickLine={false}
      tickMargin={10}
      axisLine={false}
      tickFormatter={(value) => value.slice(0, 3)}
    />
    <ChartTooltip content={<ChartTooltipContent />} />
    <Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
    <Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
  </BarChart>
</ChartContainer>

Hover over the chart to view the tooltips. With just two components, we've seamlessly integrated a visually appealing and functional tooltip, enhancing both interactivity and data presentation.

Add Legend

We'll apply the same approach for the legend. By using the ChartLegend and ChartLegendContent components from Chart, we can easily add a customized, professional-looking legend to the chart.

Import the ChartLegend and ChartLegendContent components.

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

Add the components to your chart.

<ChartContainer config={chartConfig} className="h-[200px] w-full">
  <BarChart accessibilityLayer data={chartData}>
    <CartesianGrid vertical={false} />
    <XAxis
      dataKey="month"
      tickLine={false}
      tickMargin={10}
      axisLine={false}
      tickFormatter={(value) => value.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>

Congratulations! You've successfully built your first chart. Now, what's next? Start by exploring the Chart Config, where you can manage key settings like labels, icons, and color tokens for theming. After that, you can dive into Themes and Colors to further customize the look and feel of your chart, enhance the Tooltip for better interactivity, or adjust the Legend to provide clear and informative data representations.

Chart Config

The Chart Config is where you define the labels, icons, and colors for a chart. It is intentionally decoupled from the chart data, allowing you to share configurations and color tokens across multiple charts. This setup also works independently, making it ideal for scenarios where your data or color tokens are stored remotely or in a different format.

import { Monitor } from "lucide-react"
 
import { type ChartConfig } from "@/components/ui/chart"
 
const chartConfig = {
  desktop: {
    label: "Desktop",
    icon: Monitor,
    // A color like 'hsl(220, 98%, 61%)' or 'var(--color-name)'
    color: "#2563eb",
    // OR a theme object with 'light' and 'dark' keys
    theme: {
      light: "#2563eb",
      dark: "#dc2626",
    },
  },
} satisfies ChartConfig

Theming

Charts come with built-in support for theming. You can use CSS variables (recommended) or specify color values in any format, such as hex, HSL, or OKLCH, providing flexibility in how you customize the appearance of your charts.

CSS Variables

Define your colors in your css file

globals.css
@layer base {
  :root {
    --background: 0 0% 100%;
    --foreground: 240 10% 3.9%;
    // ...
    --chart-1: 12 76% 61%;
    --chart-2: 173 58% 39%;
  }
 
  .dark: {
    --background: 240 10% 3.9%;
    --foreground: 0 0% 100%;
    // ...
    --chart-1: 220 70% 50%;
    --chart-2: 160 60% 45%;
  }
}

Add the color to your chartConfig

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

To use the theme colors in your chart, reference the colors using the format var(--color-KEY), where KEY corresponds to the specific color token defined in your CSS variables. This allows for consistent theming across your charts.

In Components
<Bar dataKey="desktop" fill="var(--color-desktop)" />
In Chart Data
const chartData = [
  { browser: "chrome", visitors: 275, fill: "var(--color-chrome)" },
  { browser: "safari", visitors: 200, fill: "var(--color-safari)" },
]

With Tailwinds

<LabelList className="fill-[--color-desktop]" />

Tooltip

A chart tooltip contains a label, name, indicator, and value. You can customize your tooltip by using a combination of these elements to display relevant data in a clear and concise manner.

Label
Page Views
Desktop
186
Mobile
80
Name
Chrome
1,286
Firefox
1,000
Page Views
Desktop
12,486
Indicator
Chrome
1,286

You can turn on/off any of these using the hideLabel, hideIndicator props and customize the indicator style using the indicator prop.

Use labelKey and nameKey to use a custom key for the tooltip label and name.

Chart comes with the <ChartTooltip> and <ChartTooltipContent> components. You can use these two components to add custom tooltips to your chart.

import { ChartTooltip, ChartTooltipContent } from "@/components/ui/chart"
<ChartTooltip content={<ChartTooltipContent />} />

Props

Use the following props to customize the tooltip.

PropTypeDescription
labelKeystringThe config or data key to use for the label.
nameKeystringThe config or data key to use for the name.
indicatordot line or dashedThe indicator style for the tooltip.
hideLabelbooleanWhether to hide the label.
hideIndicatorbooleanWhether to hide the indicator.

Colors

Colors are automatically referenced from the chart config.

Custom

To use a custom key for tooltip label and names, use the labelKey and nameKey props.

const chartData = [
  { browser: "chrome", visitors: 187, fill: "var(--color-chrome)" },
  { browser: "safari", visitors: 200, fill: "var(--color-safari)" },
]
 
const chartConfig = {
  visitors: {
    label: "Total Visitors",
  },
  chrome: {
    label: "Chrome",
    color: "hsl(var(--chart-1))",
  },
  safari: {
    label: "Safari",
    color: "hsl(var(--chart-2))",
  },
} satisfies ChartConfig
<ChartTooltip
  content={<ChartTooltipContent labelKey="visitors" nameKey="browser" />}
/>

This will use Total Visitors for label and Chrome and Safari for the tooltip names.

Legend

You can use the custom <ChartLegend> and <ChartLegendContent> components to add a legend to your chart.

import { ChartLegend, ChartLegendContent } from "@/components/ui/chart"
<ChartLegend content={<ChartLegendContent />} />

Colors

Colors are automatically referenced from the chart config.

Custom

To use a custom key for legend names, use the nameKey prop.

const chartData = [
  { browser: "chrome", visitors: 187, fill: "var(--color-chrome)" },
  { browser: "safari", visitors: 200, fill: "var(--color-safari)" },
]
 
const chartConfig = {
  chrome: {
    label: "Chrome",
    color: "hsl(var(--chart-1))",
  },
  safari: {
    label: "Safari",
    color: "hsl(var(--chart-2))",
  },
} satisfies ChartConfig
<ChartLegend content={<ChartLegendContent nameKey="browser" />} />

This will use Chrome and Safari for the legend names.

Accessibility

You can enable the accessibilityLayer prop to add an accessible layer to your chart. This feature provides keyboard access and screen reader support, ensuring your charts are more inclusive and usable for all audiences.

<LineChart accessibilityLayer />