Documentation menu
Getting Started
Components
Primitives
- Agent Thinking
- Alert
- Alert Dialog
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Calendar
- Card
- Checkbox
- Command
- Command Palette
- Context Menu
- Data Table
- Dialog
- Dropdown Menu
- Empty
- Field
- Glow Cloud
- Input
- Label
- Medication Schedule
- Period Tabs
- Popover
- Rainbow Button
- Scroll Area
- Select
- Selectable Card
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner
- Spinner
- Switch
- Table
- Tabs
- Tag
- Textarea
- Timeline
- Tooltip
- Use Count Up
Charts and metrics
Patterns
- Confirm Button
- Date Picker
- Embedded
- Empty State
- Entity Drawer
- Error Boundary
- Field Row
- File Dropzone
- Form Dialog
- Kv
- List Editor
- List Page
- Live Time
- Mini Table
- Money
- Note Field
- Note Text
- Page Card
- Page Header
- Page Tabs
- Page Toolbar
- Person Cell
- Picture Field
- Print Card
- Range Switcher
- Row Actions
- Search Input
- Search Select
- Selected Box
- Signature Pad
- Status Badge
- Sub Nav Layout
- Suggest Input
- Tab Icons
- Table Footer
- Theme Accent Picker
- Time Picker
- View All Link
Dashboard cards
Advanced
Device frames
Website widgets
Website screens
Website panels
Charts and metrics
Donut Chart
A donut with the total in the middle and a legend.
import { DonutChart } from "@/registry/vcare/ui/donut-chart"
const data = [
{ name: "General medicine", value: 412 },
{ name: "Orthopaedics", value: 186 },
{ name: "Paediatrics", value: 154 },
{ name: "Gynaecology", value: 121 },
{ name: "Other", value: 77 },
]
export default function DonutChartDemo() {
return <DonutChart className="mx-auto h-64 max-w-md" data={data} centerLabel="Visits" showLegend />
}Installation
npx shadcn@latest add @vcare/donut-chartOr install directly from the URL: https://ui.vcarehms.com/r/donut-chart.json
- Install the dependencies:
npm i recharts - Add the registry items it uses: chart, format, utils.
- Copy this file into your project and change the
@/registry/vcare/…imports to your own paths.
components/ui/donut-chart.tsx
'use client';
import * as React from 'react';
import { Label, Pie, PieChart } from 'recharts';
import { cn } from '@/registry/vcare/lib/utils';
import { formatNumber } from '@/registry/vcare/lib/format';
import { ChartContainer, ChartLegend, ChartLegendContent, ChartTooltip, ChartTooltipContent, type ChartConfig } from '@/registry/vcare/ui/chart';
export interface DonutSlice {
name: string;
value: number;
/** Any CSS color. Defaults to chart-1 through chart-5 in order. */
color?: string;
}
export interface DonutChartProps extends Omit<React.ComponentProps<typeof ChartContainer>, 'config' | 'children'> {
data: DonutSlice[];
/** Inner radius as a fraction of the outer radius. 0 draws a pie. */
innerRadius?: number;
/** Label shown in the middle, under the value. */
centerLabel?: React.ReactNode;
/** Value shown in the middle. Defaults to the total. */
centerValue?: React.ReactNode;
valueFormatter?: (value: number) => string;
showLegend?: boolean;
showTooltip?: boolean;
/** Angle where the first slice starts, in degrees. */
startAngle?: number;
/** Total sweep in degrees. 180 draws a half donut. */
sweep?: number;
paddingAngle?: number;
/** Animate slices after hydration and when data changes. Respects reduced motion. */
animate?: boolean;
animationDuration?: number;
}
const reducedMotionQuery = '(prefers-reduced-motion: reduce)';
function subscribeToReducedMotion(onChange: () => void) {
const query = window.matchMedia(reducedMotionQuery);
query.addEventListener('change', onChange);
return () => query.removeEventListener('change', onChange);
}
function getReducedMotionSnapshot() {
return window.matchMedia(reducedMotionQuery).matches;
}
function getReducedMotionServerSnapshot() {
return true;
}
function DonutChart({
data,
innerRadius = 0.7,
centerLabel,
centerValue,
valueFormatter = (value) => formatNumber(value, { format: 'compact' }),
showLegend = false,
showTooltip = true,
startAngle = 90,
sweep = 360,
paddingAngle = 2,
animate = true,
animationDuration = 500,
className,
...props
}: DonutChartProps) {
const reducedMotion = React.useSyncExternalStore(subscribeToReducedMotion, getReducedMotionSnapshot, getReducedMotionServerSnapshot);
const total = data.reduce((sum, slice) => sum + slice.value, 0);
const slices = data.map((slice, index) => ({
...slice,
key: slice.name.toLowerCase().replace(/[^a-z0-9]+/g, '-'),
fill: slice.color ?? `var(--chart-${(index % 5) + 1})`,
}));
const config = Object.fromEntries(slices.map((slice) => [slice.key, { label: slice.name, color: slice.fill }])) satisfies ChartConfig;
const center = centerValue ?? valueFormatter(total);
return (
<ChartContainer data-slot="donut-chart" config={config} className={cn('mx-auto aspect-square w-full max-h-64', className)} {...props}>
<PieChart>
{showTooltip ? (
<ChartTooltip
cursor={false}
content={
<ChartTooltipContent
hideLabel
nameKey="key"
formatter={(value, _name, item) => (
<div className="flex w-full items-center justify-between gap-4">
<span className="text-muted-foreground">{item.payload.name}</span>
<span className="font-mono font-medium tabular-nums">{valueFormatter(Number(value))}</span>
</div>
)}
/>
}
/>
) : null}
<Pie
data={slices}
dataKey="value"
nameKey="key"
innerRadius={`${innerRadius * 100}%`}
outerRadius="100%"
startAngle={startAngle}
endAngle={startAngle - sweep}
paddingAngle={paddingAngle}
strokeWidth={0}
cornerRadius={2}
isAnimationActive={animate && !reducedMotion}
animationDuration={animationDuration}
animationEasing="ease-out"
>
{innerRadius > 0 && (center || centerLabel) ? (
<Label
content={({ viewBox }) => {
if (!viewBox || !('cx' in viewBox) || !('cy' in viewBox)) return null;
const cx = Number(viewBox.cx);
const cy = Number(viewBox.cy) + (sweep < 360 ? 12 : 0);
return (
<text x={cx} y={cy} textAnchor="middle" dominantBaseline="middle">
<tspan x={cx} y={cy} className="fill-foreground text-2xl font-semibold tabular-nums">
{center}
</tspan>
{centerLabel ? (
<tspan x={cx} y={cy + 22} className="fill-muted-foreground text-xs">
{centerLabel}
</tspan>
) : null}
</text>
);
}}
/>
) : null}
</Pie>
{showLegend ? <ChartLegend content={<ChartLegendContent nameKey="key" className="flex-wrap gap-x-4 gap-y-1" />} /> : null}
</PieChart>
</ChartContainer>
);
}
export { DonutChart };Usage
import { DonutChart } from "@/components/ui/donut-chart"Examples
Half
import { DonutChart } from "@/registry/vcare/ui/donut-chart"
export default function DonutChartHalf() {
return (
<DonutChart
className="mx-auto h-48 max-w-xs"
startAngle={180}
sweep={180}
centerLabel="Beds used"
centerValue="82%"
data={[{ name: "Occupied", value: 82 }, { name: "Free", value: 18, color: "var(--muted)" }]}
/>
)
}Exports
DonutChart
Dependencies
recharts@^3.11.1(npm)@vcare/chart(registry)@vcare/format(registry)@vcare/utils(registry)
Raw item: /r/donut-chart.json