Charts
Donut Chart
Recharts 3 donut (pie) with rounded segments, a center label, and a legend.
MotionRecharts sweep animation; disabled under reduced motion
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import { DonutChart } from "kineto/donut-chart";Preview
$3KBusiness spend
- Income45%
- Expand25%
- Booking30%
tsx
"use client";
import * as React from "react";
import { useReducedMotion } from "motion/react";
import { Cell, Pie, PieChart, ResponsiveContainer } from "recharts";
import { cn } from "@/lib/cn";
const DEFAULT_COLORS = ["--chart-1", "--chart-2", "--chart-3", "--chart-4", "--chart-5"];
const color = (v: string) => `hsl(var(${v}))`;
export interface DonutSegment {
name: string;
value: number;
}
export interface DonutChartProps extends React.HTMLAttributes<HTMLDivElement> {
data: DonutSegment[];
colors?: string[];
size?: number;
thickness?: number;
centerValue?: React.ReactNode;
centerLabel?: React.ReactNode;
showLegend?: boolean;
}
export const DonutChart = React.forwardRef<HTMLDivElement, DonutChartProps>(
(
{
className,
data,
colors = DEFAULT_COLORS,
size = 184,
thickness = 22,
centerValue,
centerLabel,
showLegend = true,
...props
},
ref
) => {
const reduced = useReducedMotion();
const total = Math.max(
1,
data.reduce((sum, s) => sum + s.value, 0)
);
return (
<div
ref={ref}
className={cn("flex flex-col items-center gap-5 sm:flex-row sm:gap-8", className)}
{...props}
>
<div className="relative shrink-0" style={{ width: size, height: size }}>
<ResponsiveContainer width="100%" height="100%">
<PieChart>
<Pie
data={data}
dataKey="value"
nameKey="name"
cx="50%"
cy="50%"
innerRadius={size / 2 - thickness}
outerRadius={size / 2 - 2}
paddingAngle={2}
cornerRadius={5}
strokeWidth={0}
startAngle={90}
endAngle={-270}
isAnimationActive={!reduced}
animationDuration={900}
>
{data.map((_, i) => (
<Cell key={i} fill={color(colors[i % colors.length] ?? "--chart-1")} />
))}
</Pie>
</PieChart>
</ResponsiveContainer>
{(centerValue || centerLabel) && (
<div className="pointer-events-none absolute inset-0 flex flex-col items-center justify-center text-center">
{centerValue ? (
<span className="font-display text-2xl font-semibold tracking-tight text-foreground">
{centerValue}
</span>
) : null}
{centerLabel ? (
<span className="text-xs text-muted-foreground">{centerLabel}</span>
) : null}
</div>
)}
</div>
{showLegend ? (
<ul className="flex flex-col gap-2.5">
{data.map((segment, i) => (
<li key={i} className="flex items-center gap-2.5 text-sm">
<span
className="size-2.5 shrink-0 rounded-full"
style={{ background: color(colors[i % colors.length] ?? "--chart-1") }}
/>
<span className="text-muted-foreground">{segment.name}</span>
<span className="ml-auto font-medium text-foreground">
{Math.round((segment.value / total) * 100)}%
</span>
</li>
))}
</ul>
) : null}
</div>
);
}
);
DonutChart.displayName = "DonutChart";src/components/charts/donut-chart.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| data | { name; value }[] | — | Segments. |
| centerValue / centerLabel | ReactNode | — | Text in the hole. |
| size / thickness | number | — | Diameter and ring width in px. |