Skip to content
Components

Donut Chart

Donut 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/donut-chart.json

Use it

Scroll horizontally for long lines
"use client";
import {DonutChart} from '@/components/jez-ui/ui/donut-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">Where it all goes.</h3><p className="mt-2 text-sm text-muted-foreground">A week of studio time</p></div><span className="rounded-full border border-border px-3 py-1 text-xs text-muted-foreground">Demo data</span></div><DonutChart/></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/donut-chart.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
import { ResponsiveContainer, PieChart, Pie, Cell, Tooltip } from "recharts";
import { ChartFrame, type ChartProps } from "./chart-frame";
export function DonutChart({
  data = [
    { name: "Direct", value: 48 },
    { name: "Search", value: 32 },
    { name: "Referral", value: 20 },
  ],
  label = "Traffic sources",
  className,
}: ChartProps) {
  return (
    <ChartFrame data={data} label={label} className={cn("", className)}>
      <ResponsiveContainer initialDimension={{ width: 600, height: 240 }}>
        <PieChart>
          <Pie
            data={data}
            dataKey="value"
            nameKey="name"
            innerRadius="58%"
            outerRadius="88%"
            paddingAngle={3}
            isAnimationActive={false}
          >
            {data.map((_, i) => (
              <Cell
                key={i}
                fill={["#525be3", "#9eb95f", "#e29366", "#8498bb"][i % 4]}
              />
            ))}
          </Pie>
          <Tooltip />
        </PieChart>
      </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 →