Pie chart — label custom variant.
View the full component source code below.
"use client"
import { Pie, PieChart } from "recharts"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent } from "@/components/ui/chart"
const data = [
{ sector: "farming", value: 275, fill: "var(--color-farming)" },
{ sector: "mining", value: 200, fill: "var(--color-mining)" },
{ sector: "tourism", value: 187, fill: "var(--color-tourism)" },
{ sector: "tech", value: 173, fill: "var(--color-tech)" },
{ sector: "trade", value: 90, fill: "var(--color-trade)" },
]
const config = {
value: { label: "Output" },
farming: { label: "Farming", color: "hsl(var(--chart-1))" },
mining: { label: "Mining", color: "hsl(var(--chart-2))" },
tourism: { label: "Tourism", color: "hsl(var(--chart-3))" },
tech: { label: "Tech", color: "hsl(var(--chart-4))" },
trade: { label: "Trade", color: "hsl(var(--chart-5))" },
} satisfies ChartConfig
export function ChartPieLabelCustom() {
return (
<Card>
<CardHeader>
<CardTitle>Pie Chart - Custom Labels</CardTitle>
<CardDescription>Custom label with percentage</CardDescription>
</CardHeader>
<CardContent>
<ChartContainer config={config} className="mx-auto aspect-square max-h-[250px]">
<PieChart>
<ChartTooltip content={<ChartTooltipContent nameKey="sector" hideLabel />} />
<Pie data={data} dataKey="value" nameKey="sector" label={({ payload, ...props }) => (
<text x={props.x} y={props.y} textAnchor={props.textAnchor} dominantBaseline={props.dominantBaseline} fill="hsla(var(--foreground))" className="text-xs">
{`${config[payload.sector as keyof typeof config]?.label ?? payload.sector} (${payload.value})`}
</text>
)} />
</PieChart>
</ChartContainer>
</CardContent>
</Card>
)
}
npx shadcn@latest add https://registry.mukoko.com/api/v1/ui/chart-pie-label-customFetch this component's metadata and source code from the registry API.
/api/v1/ui/chart-pie-label-customcomponents/blocks/charts/chart-pie-label-custom.tsx