Charts and graphs · @jengacore/chart-donut
Pie and donut chart
Shares of one whole, up to five slices, total in the middle of the donut, every slice in the legend.
October payments by channel (thousands)Wallet is the largest channel with 389 thousand of 841 thousand payments (46%); agent cash-in is the smallest at 9%.
| Category | Value | Share |
|---|---|---|
| Wallet | 389 | 46% |
| Card | 248 | 29% |
| Bank transfer | 130 | 15% |
| Agent cash-in | 74 | 9% |
Disputes by reasonDuplicate charges are the largest reason, 41 of 96 disputes (43%).
| Category | Value | Share |
|---|---|---|
| Duplicate charge | 41 | 43% |
| Not received | 28 | 29% |
| Wrong amount | 19 | 20% |
| Other | 8 | 8% |
Install
terminal
npx shadcn@latest add @jengacore/chart-donutWithout the namespace configured: npx shadcn@latest add https://ui.jengacore.com/r/<new-york-v4|base-nova>/chart-donut.json
Registry dependencies
Source
components/blocks/chart-donut.tsx
"use client"
import { Label, Pie, PieChart } from "recharts"
import { ChartContainer, ChartLegend, ChartLegendContent, ChartTooltip, ChartTooltipContent } from "@/components/ui/chart"
import { ChartFigure, type ChartCopy, seriesColor, slug } from "@/components/jc/chart-figure"
export type Share = { name: string; value: number; color?: string }
const SAMPLE: Share[] = [
{ name: "Wallet", value: 389 },
{ name: "Card", value: 248 },
{ name: "Bank transfer", value: 130 },
{ name: "Agent cash-in", value: 74 },
]
/**
* Pie or donut: shares of one whole, at most five slices (group the rest as "Other"). Slices are separated by a
* card-coloured gap and the legend names every one; the donut prints the total in the middle.
*/
export function ChartDonut({
data = SAMPLE,
title = "October payments by channel (thousands)",
summary = "Wallet is the largest channel with 389 thousand of 841 thousand payments (46%); agent cash-in is the smallest at 9%.",
totalLabel = "Payments",
donut = true,
showTable,
className,
}: {
/** Word under the total in the middle of the donut. */
totalLabel?: string
/** false draws a full pie. */
donut?: boolean
showTable?: boolean
className?: string
} & ChartCopy<Share[]>) {
const total = data.reduce((sum, d) => sum + d.value, 0)
const rows = data.map((d, i) => ({ ...d, key: slug(d.name, i), fill: `var(--color-${slug(d.name, i)})` }))
const config = Object.fromEntries(rows.map((d, i) => [d.key, { label: d.name, color: d.color ?? seriesColor(i) }]))
return (
<ChartFigure
title={title}
summary={summary}
data={rows.map((d) => ({ name: d.name, value: d.value, share: total ? `${Math.round((d.value / total) * 100)}%` : "0%" }))}
columns={[
{ key: "name", label: "Category" },
{ key: "value", label: "Value" },
{ key: "share", label: "Share" },
]}
showTable={showTable}
className={className}
>
<ChartContainer config={config} className="mx-auto aspect-square h-64">
<PieChart accessibilityLayer title={title}>
<ChartTooltip content={<ChartTooltipContent nameKey="key" hideLabel />} />
<Pie
data={rows}
dataKey="value"
nameKey="key"
innerRadius={donut ? "58%" : 0}
outerRadius="85%"
stroke="var(--card)"
strokeWidth={2}
isAnimationActive={false}
>
{donut ? (
<Label
content={({ viewBox }) =>
viewBox && "cx" in viewBox && "cy" in viewBox ? (
<text x={viewBox.cx} y={viewBox.cy} textAnchor="middle" dominantBaseline="middle">
<tspan x={viewBox.cx} y={viewBox.cy} className="fill-foreground font-display text-2xl font-semibold tabular-nums">
{total.toLocaleString("en")}
</tspan>
<tspan x={viewBox.cx} y={(viewBox.cy ?? 0) + 20} className="fill-muted-foreground text-xs">
{totalLabel}
</tspan>
</text>
) : null
}
/>
) : null}
</Pie>
<ChartLegend itemSorter={(item) => rows.findIndex((r) => r.key === (item.payload as { key?: string } | undefined)?.key)} content={<ChartLegendContent nameKey="key" />} className="flex-wrap gap-2" />
</PieChart>
</ChartContainer>
</ChartFigure>
)
}