Skip to content
Components

Bar Chart

Bar Chart with readable values and an accessible data representation.

Open ↗

Make it yours.

Install the editable source and its dependencies into your React + Tailwind project.

Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/bar-chart.json

Use it

Scroll horizontally for long lines
"use client";
import {BarChart} from '@/components/jez-ui/ui/bar-chart';

export default function Example(){

return <><div className="w-full max-w-2xl"><div className="mb-8 flex flex-wrap items-end justify-between gap-4"><div><h3 className="text-3xl tracking-tight">Every day counts.</h3><p className="mt-2 text-sm text-muted-foreground">Orders by day of the week</p></div><span className="rounded-full border border-border px-3 py-1 text-xs text-muted-foreground">Demo data</span></div><BarChart/></div></>;
}

Props & types

Scroll horizontally for long lines
export type ChartProps = {
  data?: ChartDatum[];
  label?: string;
  className?: string;
  color?: string;
};

ChartProps

Source

View 3 source files

registry/ui/bar-chart.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
import {
  ResponsiveContainer,
  BarChart as Plot,
  Bar,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
} from "recharts";
import { ChartFrame, sampleChartData, type ChartProps } from "./chart-frame";
export function BarChart({
  data = sampleChartData,
  label = "Bar chart",
  className,
  color = "var(--primary)",
}: ChartProps) {
  return (
    <ChartFrame data={data} label={label} className={cn("", className)}>
      <ResponsiveContainer
        initialDimension={{ width: 600, height: 240 }}
        width="100%"
        height="100%"
      >
        <Plot
          data={data}
          margin={{ top: 8, right: 12, left: -24, bottom: 0 }}
          accessibilityLayer
        >
          <CartesianGrid vertical={false} stroke="var(--border)" />
          <XAxis
            dataKey="name"
            tickLine={false}
            axisLine={false}
            tick={{ fill: "var(--muted-foreground)", fontSize: 11 }}
          />
          <YAxis
            tickLine={false}
            axisLine={false}
            tick={{ fill: "var(--muted-foreground)", fontSize: 11 }}
          />
          <Tooltip
            contentStyle={{
              background: "var(--background)",
              border: "1px solid var(--border)",
              borderRadius: 8,
              color: "var(--foreground)",
            }}
          />
          <Bar
            dataKey="value"
            fill={color}
            stroke={color}
            radius={[4, 4, 0, 0]}
            isAnimationActive={false}
          />
        </Plot>
      </ResponsiveContainer>
    </ChartFrame>
  );
}

registry/ui/utils.ts

Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
  return twMerge(clsx(values));
}

registry/ui/chart-frame.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
export type ChartDatum = { name: string; value: number; previous?: number };
export const sampleChartData: ChartDatum[] = [
  { name: "Mon", value: 32, previous: 24 },
  { name: "Tue", value: 48, previous: 31 },
  { name: "Wed", value: 39, previous: 36 },
  { name: "Thu", value: 65, previous: 42 },
  { name: "Fri", value: 57, previous: 38 },
  { name: "Sat", value: 82, previous: 49 },
  { name: "Sun", value: 73, previous: 51 },
];
export type ChartProps = {
  data?: ChartDatum[];
  label?: string;
  className?: string;
  color?: string;
};
export function ChartFrame({
  children,
  data,
  label,
  className,
}: {
  children: React.ReactNode;
  data: ChartDatum[];
  label: string;
  className?: string;
}) {
  return (
    <figure className={cn("m-0 w-full min-w-0", className)}>
      <figcaption className="mb-4 text-sm font-medium">{label}</figcaption>
      <div className="h-60 w-full min-w-0 overflow-hidden">{children}</div>
      <details className="mt-3 text-xs text-muted-foreground">
        <summary>View data table</summary>
        <table className="mt-3 w-full text-left">
          <caption className="sr-only">{label}</caption>
          <thead>
            <tr>
              <th>Period</th>
              <th>Value</th>
              <th>Previous</th>
            </tr>
          </thead>
          <tbody>
            {data.map((d, i) => (
              <tr key={i}>
                <th className="py-1 font-normal">{d.name}</th>
                <td>{d.value}</td>
                <td>{d.previous ?? "—"}</td>
              </tr>
            ))}
          </tbody>
        </table>
      </details>
    </figure>
  );
}

Dependencies

recharts@3.8.0 · clsx@2.1.1 · tailwind-merge@3.5.0

Accessibility & behaviour

Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.

Read the accessibility and performance guide →