
Card with a horizontal bar chart ranking five countries by listeners, country names on the axis and values printed at the end of each bar. Client component on recharts 3.8.
blocks/charts/bar-chart-02/index.tsx
"use client"
import { Bar, BarChart, LabelList, XAxis, YAxis } from "recharts"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
type ChartConfig,
} from "@/components/ui/chart"
// The copy: change text here, not in the markup.
const COPY = {
title: "Top countries",
description: "Listeners by country in October",
} as const
const COUNTRIES = [
{ country: "United States", listeners: 4820 },
{ country: "Germany", listeners: 2940 },
{ country: "Japan", listeners: 2310 },
{ country: "Brazil", listeners: 1870 },
{ country: "India", listeners: 1540 },
]
const CHART_CONFIG = {
listeners: { label: "Listeners", color: "var(--chart-2)" },
} satisfies ChartConfig
export default function BarChart02() {
return (
<Card className="mx-auto w-full max-w-md">
<CardHeader>
<CardTitle>
<h2>{COPY.title}</h2>
</CardTitle>
<CardDescription>{COPY.description}</CardDescription>
</CardHeader>
<CardContent>
<ChartContainer config={CHART_CONFIG}>
<BarChart
accessibilityLayer
data={COUNTRIES}
layout="vertical"
margin={{ right: 48 }}
>
{/* Stock ChartContainer's label color misses recharts 3's label layer. */}
<YAxis
tick={{ fill: "var(--muted-foreground)" }}
dataKey="country"
type="category"
tickLine={false}
axisLine={false}
tickMargin={8}
width={104}
/>
<XAxis dataKey="listeners" type="number" hide />
<ChartTooltip
cursor={false}
content={<ChartTooltipContent hideLabel />}
/>
<Bar dataKey="listeners" fill="var(--color-listeners)" radius={6}>
<LabelList
dataKey="listeners"
position="right"
offset={8}
className="fill-foreground"
fontSize={12}
formatter={(value) => Number(value).toLocaleString("en-US")}
/>
</Bar>
</BarChart>
</ChartContainer>
</CardContent>
</Card>
)
}