shadcn.io is not affiliated with official shadcn/ui
Bar Chart - Interactive
Showing total visitors for the last 3 months
Installation
bunx --bun shadcn@latest add https://kit.dev/r/base/chart.jsonnpx shadcn@latest add https://kit.dev/r/base/chart.jsonpnpm dlx shadcn@latest add https://kit.dev/r/base/chart.jsonyarn shadcn@latest add https://kit.dev/r/base/chart.jsonInstall the following dependencies:
bun add rechartsnpm install rechartspnpm add rechartsyarn add rechartsAdd the following to your globals.css.
:root {
--chart-1: var(--color-orange-600);
--chart-2: var(--color-teal-600);
--chart-3: var(--color-cyan-900);
--chart-4: var(--color-amber-400);
--chart-5: var(--color-amber-500);
}
.dark {
--chart-1: var(--color-blue-700);
--chart-2: var(--color-emerald-500);
--chart-3: var(--color-amber-500);
--chart-4: var(--color-purple-500);
--chart-5: var(--color-rose-500);
}Copy and paste the following code into your project.
"use client";
import type React from "react";
import { createContext, useContext, useId, useMemo } from "react";
import {
type DefaultLegendContentProps,
type DefaultTooltipContentProps,
Legend,
ResponsiveContainer,
Tooltip,
type TooltipValueType,
} from "recharts";
import { cn } from "@/lib/utils";
const THEMES = { dark: ".dark", light: "" } as const;
const INITIAL_DIMENSION = { height: 200, width: 320 } as const;Update the import paths to match your project setup.
Usage
import { type MouseEvent, useMemo, useState } from "react";
import { Bar, BarChart, CartesianGrid, XAxis } from "recharts";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import {
type ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
type CustomTooltipProps,
} from "@/components/ui/chart";export const description = "An interactive bar chart";
const chartData = [
{ date: "2024-04-01", desktop: 222, mobile: 150 },
{ date: "2024-04-02", desktop: 97, mobile: 180 },
{ date: "2024-04-03", desktop: 167, mobile: 120 },
{ date: "2024-04-04", desktop: 242, mobile: 260 },
{ date: "2024-04-05", desktop: 373, mobile: 290 },
{ date: "2024-04-06", desktop: 301, mobile: 340 },
{ date: "2024-04-07", desktop: 245, mobile: 180 },
{ date: "2024-04-08", desktop: 409, mobile: 320 },
{ date: "2024-04-09", desktop: 59, mobile: 110 },
{ date: "2024-04-10", desktop: 261, mobile: 190 },
{ date: "2024-04-11", desktop: 327, mobile: 350 },
{ date: "2024-04-12", desktop: 292, mobile: 210 },
{ date: "2024-04-13", desktop: 342, mobile: 380 },
{ date: "2024-04-14", desktop: 137, mobile: 220 },
{ date: "2024-04-15", desktop: 120, mobile: 170 },
{ date: "2024-04-16", desktop: 138, mobile: 190 },
{ date: "2024-04-17", desktop: 446, mobile: 360 },
{ date: "2024-04-18", desktop: 364, mobile: 410 },
{ date: "2024-04-19", desktop: 243, mobile: 180 },
{ date: "2024-04-20", desktop: 89, mobile: 150 },
{ date: "2024-04-21", desktop: 137, mobile: 200 },
{ date: "2024-04-22", desktop: 224, mobile: 170 },
{ date: "2024-04-23", desktop: 138, mobile: 230 },
{ date: "2024-04-24", desktop: 387, mobile: 290 },
{ date: "2024-04-25", desktop: 215, mobile: 250 },
{ date: "2024-04-26", desktop: 75, mobile: 130 },
{ date: "2024-04-27", desktop: 383, mobile: 420 },
{ date: "2024-04-28", desktop: 122, mobile: 180 },
{ date: "2024-04-29", desktop: 315, mobile: 240 },
{ date: "2024-04-30", desktop: 454, mobile: 380 },
];
const chartConfig = {
desktop: {
color: "var(--chart-2)",
label: "Desktop",
},
mobile: {
color: "var(--chart-1)",
label: "Mobile",
},
views: {
label: "Page Views",
},
} satisfies ChartConfig;
type ActiveChart = "desktop" | "mobile";
const formatTickLabel = (value: string) => {
const date = new Date(value);
return date.toLocaleDateString("en-US", {
day: "numeric",
month: "short",
});
};
const formatTooltipLabel = (value: unknown) =>
new Date(String(value)).toLocaleDateString("en-US", {
day: "numeric",
month: "short",
year: "numeric",
});
const renderTooltipContent = (props: CustomTooltipProps) => (
<ChartTooltipContent
{...props}
className="w-[150px]"
labelFormatter={formatTooltipLabel}
nameKey="views"
/>
);
const ChartDemo = () => {
const [activeChart, setActiveChart] = useState<ActiveChart>("desktop");
const total = useMemo(
() => ({
desktop: chartData.reduce((acc, curr) => acc + curr.desktop, 0),
mobile: chartData.reduce((acc, curr) => acc + curr.mobile, 0),
}),
[]
);
const handleActiveChartClick = (event: MouseEvent<HTMLButtonElement>) => {
const { chart } = event.currentTarget.dataset;
if (chart === "desktop" || chart === "mobile") {
setActiveChart(chart);
}
};
return (
<Card className="py-0 pb-4">
<CardHeader className="flex flex-col items-stretch border-b p-0! sm:flex-row">
<div className="flex flex-1 flex-col justify-center gap-1 px-6 pt-4 pb-3 sm:py-0!">
<CardTitle>Bar Chart - Interactive</CardTitle>
<CardDescription>
Showing total visitors for the last 3 months
</CardDescription>
</div>
<div className="flex">
{(["desktop", "mobile"] as const).map((chart) => (
<button
className="relative z-30 flex flex-1 flex-col justify-center gap-1 border-t px-6 py-4 text-left even:border-l data-[active=true]:bg-muted/50 sm:border-t-0 sm:border-l sm:px-8 sm:py-6"
data-active={activeChart === chart}
data-chart={chart}
key={chart}
onClick={handleActiveChartClick}
type="button"
>
<span className="text-muted-foreground text-xs">
{chartConfig[chart].label}
</span>
<span className="font-bold text-lg leading-none sm:text-3xl">
{total[chart].toLocaleString()}
</span>
</button>
))}
</div>
</CardHeader>
<CardContent className="px-2 sm:p-6">
<ChartContainer
className="aspect-auto h-[250px] w-full"
config={chartConfig}
>
<BarChart
accessibilityLayer
data={chartData}
margin={{
left: 12,
right: 12,
}}
>
<CartesianGrid vertical={false} />
<XAxis
axisLine={false}
dataKey="date"
minTickGap={32}
tickFormatter={formatTickLabel}
tickLine={false}
tickMargin={8}
/>
<ChartTooltip content={renderTooltipContent} />
<Bar dataKey={activeChart} fill={`var(--color-${activeChart})`} />
</BarChart>
</ChartContainer>
</CardContent>
</Card>
);
};
Demos
Example
Build a basic bar chart from data and chart config.
Grid
Add a Cartesian grid behind the bars.
Axis
Add an X axis with abbreviated month labels.
Example Tooltip
Add ChartTooltip and ChartTooltipContent to the bar chart.
Legend
Add ChartLegend and ChartLegendContent to the bar chart.
Tooltip
Tooltip anatomy: label, name, indicator, and value combinations.
Rtl
Right-to-left chart. See the RTL configuration guide for document direction.