Mahmoud Ilyan

Chart

Component

About

A composable chart container that wraps Recharts with theme-aware colors, an accessible tooltip, and a legend for building line, bar, area, donut, and sparkline charts.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Chart to your project and fits it to your theme.

Code

How Chart is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client";

import {
  ChartContainer,
  ChartTooltip,
  ChartTooltipContent,
  ChartLegend,
  ChartLegendContent,
  type ChartConfig,
} from "@/components/ui/chart";
import { Cell, Label, Pie, PieChart } from "recharts";

const traffic = [
  { source: "Email", visits: 5200 },
  { source: "Organic", visits: 3800 },
  { source: "Social", visits: 2400 },
  { source: "Direct", visits: 1600 },
];

const donutConfig = {
  Email: {
    label: "Email",
    theme: {
      light: "var(--color-chart-cat-teal)",
      dark: "var(--color-chart-cat-teal)",
    },
  },
  Organic: {
    label: "Organic",
    theme: {
      light: "var(--color-chart-cat-violet)",
      dark: "var(--color-chart-cat-violet)",
    },
  },
  Social: {
    label: "Social",
    theme: {
      light: "var(--color-chart-cat-blue)",
      dark: "var(--color-chart-cat-blue)",
    },
  },
  Direct: {
    label: "Direct",
    theme: {
      light: "var(--color-chart-cat-orange)",
      dark: "var(--color-chart-cat-orange)",
    },
  },
} satisfies ChartConfig;

export default function Demo() {
  const total = traffic.reduce((sum, d) => sum + d.visits, 0);
  return (
    <ChartContainer
      config={donutConfig}
      ariaLabel="Traffic by source"
      className="aspect-square max-h-[300px] w-full"
    >
      <PieChart>
        <ChartTooltip
          content={<ChartTooltipContent hideLabel nameKey="source" />}
        />
        <Pie
          data={traffic}
          dataKey="visits"
          nameKey="source"
          innerRadius={55}
          outerRadius={85}
          strokeWidth={2}
        >
          {traffic.map((d) => (
            <Cell key={d.source} fill={`var(--color-${d.source})`} />
          ))}
          <Label
            content={({ viewBox }) => {
              if (!viewBox || !("cx" in viewBox) || !("cy" in viewBox))
                return null;
              return (
                <text
                  x={viewBox.cx}
                  y={viewBox.cy}
                  textAnchor="middle"
                  dominantBaseline="middle"
                >
                  <tspan
                    x={viewBox.cx}
                    y={viewBox.cy}
                    className="fill-ink-dark text-xl font-semibold"
                  >
                    {total.toLocaleString()}
                  </tspan>
                  <tspan
                    x={viewBox.cx}
                    y={(viewBox.cy ?? 0) + 18}
                    className="fill-ink-light text-xs"
                  >
                    visits
                  </tspan>
                </text>
              );
            }}
          />
        </Pie>
        <ChartLegend content={<ChartLegendContent nameKey="source" />} />
      </PieChart>
    </ChartContainer>
  );
}

Dependencies

More by Mahmoud Ilyan

View all
  • Steps

    Steps

    5
  • Radio Card

    Radio Card

    1
  • Switch

    Switch

    1
  • Input

    Input

    1
  • Badge

    Badge

    1
  • Textarea

    Textarea

  • Tabs

    Tabs

  • Progress

    Progress