Chart

Recharts wrapper that colours series from theme tokens, with a themed tooltip and legend.

Installation

pnpm dlx shadcn@latest add @ward/chart

Add the Ward registry to components.json once:

"registries": {
  "@ward": "https://ward.so/r/{name}.json"
}

Or install from the URL with no configuration: npx shadcn@latest add https://ward.so/r/chart.json

Usage

import { Area, AreaChart, CartesianGrid, XAxis } from "recharts";
import { ChartContainer, ChartTooltip, ChartTooltipContent, type ChartConfig } from "@/components/ui/chart";
const config = { clicks: { label: "Clicks", color: "var(--chart-1)" } } satisfies ChartConfig;
const data = [
  { day: "Mon", clicks: 12 }, { day: "Tue", clicks: 19 }, { day: "Wed", clicks: 8 },
  { day: "Thu", clicks: 24 }, { day: "Fri", clicks: 17 }, { day: "Sat", clicks: 31 }, { day: "Sun", clicks: 22 },
];

<ChartContainer config={config} className="h-48 w-full">
  <AreaChart data={data}>
    <CartesianGrid vertical={false} />
    <XAxis dataKey="day" tickLine={false} axisLine={false} />
    <ChartTooltip content={<ChartTooltipContent indicator="line" />} />
    <Area dataKey="clicks" type="monotone" stroke="var(--color-clicks)" fill="var(--color-clicks)" fillOpacity={0.15} />
  </AreaChart>
</ChartContainer>

Props

PropTypeDefaultDescription
configChartConfig-Label, icon and colour for each series, keyed by its dataKey. Use var(--color-<key>) in the chart to draw with a colour from it.
config colorstring-Any CSS colour; the theme tokens --chart-1 to --chart-5 change with light and dark on their own.
config theme{ light: string; dark: string }-Separate colours for each theme, instead of color.
classNamestring-The chart is 16:9 by default; set a height such as h-48 to size it yourself.
initialDimension{ width: number; height: number }{ width: 320, height: 200 }Size used before the chart has been measured, so it does not render at zero on the server.
ChartTooltipContent indicator"dot" | "line" | "dashed""dot"Shape of the colour marker beside each value.
ChartTooltipContent hideLabelbooleanfalseHide the heading, such as the day, above the values.
ChartTooltipContent hideIndicatorbooleanfalseHide the colour marker beside each value.
ChartTooltipContent nameKeystring-Key in the payload, or in config, to take each series name from.
ChartTooltipContent labelFormatter(label, payload) => ReactNode-Format the heading; formatter does the same for each value row.
ChartLegendContent hideIconbooleanfalseHide the icon or colour swatch beside each legend label. Pass it as ChartLegend content.

Ward · Version 1.0.0 · Registry manifest