Charts

Horizontal Ranking Bar Chart

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.

Free, MIT licenseOpen in Browse
"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>
  )
}

Installation

pnpm dlx shadcn add @01orbital/bar-chart-02
npx shadcn add @01orbital/bar-chart-02
yarn shadcn add @01orbital/bar-chart-02
bunx --bun shadcn add @01orbital/bar-chart-02

Free under the MIT license. Add the @01orbital registry to components.json once (install guide), then run the command.

It lands at components/blocks/charts/bar-chart-02/index.tsx, as source you own and edit.

Usage

import BarChart02 from "@/components/blocks/charts/bar-chart-02"

export default function Example() {
  return <BarChart02 />
}

Dependencies

npm: recharts@3.8.0. The CLI installs them for you.

Registry pieces it installs with it:

  • card
  • chart