"use client"; import { motion } from "motion/react"; import { ChartFrame } from "./chart-frame"; import { sectorPath, catColor } from "@/lib/chart-utils"; export type PieSlice = { label: string; value: number }; type PieChartProps = { data: PieSlice[]; /** 0 = pie, >0 = donut (fraction of radius). Default 0. */ innerRatio?: number; size?: number; className?: string; }; /** * PieChart — SVG sectors that sweep open one after another (angle 0 → target), * staggered around the circle. Set `innerRatio` > 0 for a donut. */ export function PieChart({ data, innerRatio = 0, size = 320, className }: PieChartProps) { const total = data.reduce((a, d) => a + d.value, 0) || 1; const cx = size / 2; const cy = size / 2; const rOuter = size / 2 - 6; const rInner = rOuter * innerRatio; const slices = data.map((d, i) => { const before = data.slice(0, i).reduce((a, x) => a + x.value, 0); const start = (before / total) * Math.PI * 2; const end = ((before + d.value) / total) * Math.PI * 2; return { ...d, start, end, color: catColor(i), pct: Math.round((d.value / total) * 100) }; }); return ( {({ inView, reduce }) => ( <> {slices.map((s, i) => { const mid = (s.start + s.end) / 2; const lr = (rOuter + rInner) / 2 || rOuter * 0.62; const lx = cx + Math.cos(mid - Math.PI / 2) * lr; const ly = cy + Math.sin(mid - Math.PI / 2) * lr; return ( {s.pct >= 8 && ( {s.pct}% )} ); })} )} ); }