Skip to content
Drivn

Chart

Charts powered by recharts with config-driven colors, themed tooltip, and legend.

Preview

Installation

npx drivn add chart

Usage

Declare a config with satisfies ChartConfig, then reference each key as var(--color-{key}) on any recharts series.

1import { Chart, type ChartConfig } from "@/components/ui/chart"
2import { Bar, BarChart } from "recharts"
3
4const data = [
5 { month: "Jan", desktop: 186, mobile: 80 },
6 { month: "Feb", desktop: 305, mobile: 200 },
7 { month: "Mar", desktop: 237, mobile: 120 },
8 { month: "Apr", desktop: 73, mobile: 190 },
9 { month: "May", desktop: 209, mobile: 130 },
10 { month: "Jun", desktop: 214, mobile: 140 },
11]
12
13const config = {
14 desktop: { label: "Desktop", color: "var(--color-chart-1)" },
15 mobile: { label: "Mobile", color: "var(--color-chart-3)" },
16} satisfies ChartConfig
17
18export default function Page() {
19 return (
20 <Chart config={config}>
21 <BarChart data={data}>
22 <Chart.Grid />
23 <Chart.XAxis dataKey="month" />
24 <Chart.Legend />
25 <Chart.Tooltip />
26 <Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
27 <Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
28 </BarChart>
29 </Chart>
30 )
31}

Stacked Bar

Share a stackId across series and round only the top segment.

Line

Use indicator="line" on the tooltip content to match the series stroke.

Area

Reuse the series color for the fill and soften it with fillOpacity.

Pie

Each data row carries its own fill; nameKey maps rows to config entries.

Donut

Add innerRadius and a centered Label for a total.

Interactive

Drive dataKey from state to switch series without remounting the chart.

API Reference

Chart

PropTypeDefaultDescription
configChartConfigSeries map: label, optional icon, and color exposed as --color-{key}
childrenReactElementA single recharts chart (BarChart, LineChart, PieChart, etc.)
classNamestringAdditional CSS classes for the container (override size with aspect-* or h-*)

ChartConfig

PropTypeDefaultDescription
labelReactNodeDisplay name used in tooltip and legend
iconComponentType<{ className?: string }>Icon rendered in the legend instead of a color swatch
colorstringAny CSS color, typically var(--color-chart-1) through var(--color-chart-5)

Chart.Grid

PropTypeDefaultDescription
verticalbooleanfalseDraw vertical grid lines
horizontalbooleantrueDraw horizontal grid lines

Chart.XAxis

PropTypeDefaultDescription
dataKeystringField in each data row used for tick labels
tickLinebooleanfalseDraw a small line at each tick
axisLinebooleanfalseDraw the axis baseline

Chart.Tooltip

PropTypeDefaultDescription
contentReactElement<Chart.TooltipContent />Custom tooltip renderer
cursorboolean | objectfalseShow the hover cursor behind the active bar or line

Chart.TooltipContent

PropTypeDefaultDescription
hideLabelbooleanfalseHide the axis label heading
indicator"dot" | "line""dot"Shape of the color indicator next to each series
classNamestringAdditional CSS classes for the tooltip panel

Chart.Legend

PropTypeDefaultDescription
contentReactElement<Chart.LegendContent />Custom legend renderer
verticalAlign"top" | "bottom""bottom"Legend placement relative to the plot area

Chart.LegendContent

PropTypeDefaultDescription
classNamestringAdditional CSS classes for the legend row