Horizontal Ranking Bar Chart
Horizontal bar chart card for a leaderboard: five countries ranked by listeners, with country names on the axis and values printed at the end of each bar.
"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-3)" },
} satisfies ChartConfig
export default function ValueLabeledRankingChart() {
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={[0, 2, 2, 0]}
maxBarSize={16}
>
<LabelList
dataKey="listeners"
position="right"
offset={8}
className="fill-foreground"
fontSize={12}
formatter={(value) => Number(value).toLocaleString("en-US")}
/>
</Bar>
</BarChart>
</ChartContainer>
</CardContent>
</Card>
)
}
Installation
npx shadcn@latest add @01orbital/value-labeled-ranking-chartMake it yours
- Its words are in
COPYat the top ofcomponents/blocks/charts/value-labeled-ranking-chart/index.tsx: change them there, not in the markup. COUNTRIESandCHART_CONFIG, also at the top of the file, hold its lists and settings.
Built from
npm: recharts@3.8.0. The CLI installs it for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on