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
Trend Chart
Lines or areas over time, with comparisons and a hover readout.
import { TrendChart } from "@/registry/vcare/ui/trend-chart"
const data = [{ month: "Jan", visits: 412, revenue: 18.2 }, { month: "Feb", visits: 468, revenue: 20.1 }, { month: "Mar", visits: 501, revenue: 22.4 }, { month: "Apr", visits: 455, revenue: 19.8 }, { month: "May", visits: 590, revenue: 26.3 }, { month: "Jun", visits: 642, revenue: 28.9 }]
export default function TrendChartDemo() {
return <TrendChart className="h-64 w-full" data={data} xKey="month" series={[{ key: "visits", label: "Visits" }]} />
}Installation
npx shadcn@latest add @vcare/trend-chartOr install directly from the URL: https://ui.vcarehms.com/r/trend-chart.json
- Install the dependencies:
npm i recharts - Add the registry items it uses: chart, utils.
- Copy this file into your project and change the
@/registry/vcare/…imports to your own paths.
components/ui/trend-chart.tsx
'use client';
import * as React from 'react';
import {
Area,
AreaChart,
AreaRevealShape,
Bar,
BarChart,
CartesianGrid,
Line,
LineChart,
ReferenceLine,
XAxis,
YAxis,
type AreaRevealShapeProps,
} from 'recharts';
import { cn } from '@/registry/vcare/lib/utils';
import { ChartContainer, ChartLegend, ChartLegendContent, ChartTooltip, ChartTooltipContent, type ChartConfig } from '@/registry/vcare/ui/chart';
export interface TrendSeries {
/** Key in each data row. */
key: string;
label: string;
/** Any CSS color. Defaults to chart-1 through chart-5 in order. */
color?: string;
}
export interface TrendChartProps extends Omit<React.ComponentProps<typeof ChartContainer>, 'config' | 'children'> {
data: Record<string, unknown>[];
/** Key of the x-axis value in each row. */
xKey: string;
series: TrendSeries[];
type?: 'area' | 'line' | 'bar';
/** Area fill: a plain gradient, or a dot grid that fades out toward the line. */
fill?: 'gradient' | 'dots';
/** Bars only: "horizontal" runs bars left to right with categories down the side. */
layout?: 'vertical' | 'horizontal';
/** Bars only: corner radius in px, or "full" for pill-shaped bars. */
barRadius?: number | 'full';
/** Bars only: thickness of each bar in px. Defaults to a share of the category width. */
barSize?: number;
stacked?: boolean;
showLegend?: boolean;
legendPosition?: 'top' | 'bottom';
legendAlign?: 'left' | 'center' | 'right';
showGrid?: boolean;
/** Show the value axis. In the horizontal layout this is the axis along the bottom. */
showYAxis?: boolean;
/** Recharts domain for the value axis, e.g. ["dataMin", "dataMax"] or ["auto", "auto"]. Defaults to [0, "auto"]. */
yDomain?: React.ComponentProps<typeof YAxis>['domain'];
xFormatter?: (value: unknown) => string;
yFormatter?: (value: number) => string;
/** Animate series 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 defaultXFormatter(value: unknown) {
if (typeof value === 'string') {
const match = /^(\d{4})-(\d{2})-(\d{2})/.exec(value);
if (match) {
const y = Number(match[1]);
const m = Number(match[2]);
const d = Number(match[3]);
const date = new Date(y, m - 1, d);
const valid = date.getFullYear() === y && date.getMonth() === m - 1 && date.getDate() === d;
if (valid) {
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
});
}
}
}
return value == null || value === '' ? '—' : String(value);
}
/** Area shape that masks the fill to a dot grid while keeping the stroke solid. */
function DotGridAreaShape({ maskId, ...props }: AreaRevealShapeProps & { maskId: string }) {
return (
<>
<g mask={`url(#${maskId})`}>
<AreaRevealShape {...props} stroke="none" />
</g>
<AreaRevealShape {...props} id={undefined} fill="none" />
</>
);
}
/** Recharts clamps corner radii to half the bar, so an oversized radius draws a pill. */
const PILL = 9999;
type Radius = number | [number, number, number, number];
/** Round only the outer corners of a stack; mixed-sign stacks stay square. */
function barCorners(radius: number | 'full', stacked: boolean, hasNegative: boolean, horizontal: boolean, index: number, count: number): Radius {
const r = radius === 'full' ? PILL : radius;
if (!stacked || count === 1) return r;
if (hasNegative) return 0;
// Corners run [top-left, top-right, bottom-right, bottom-left].
if (index === count - 1) return horizontal ? [0, r, r, 0] : [r, r, 0, 0];
if (index === 0) return horizontal ? [r, 0, 0, r] : [0, 0, r, r];
return 0;
}
function TrendChart({
data,
xKey,
series,
type = 'area',
fill = 'gradient',
layout = 'vertical',
barRadius = 4,
barSize,
stacked = false,
showLegend = false,
legendPosition = 'bottom',
legendAlign = 'center',
showGrid = true,
showYAxis = false,
yDomain,
xFormatter = defaultXFormatter,
yFormatter,
animate = true,
animationDuration = 500,
className,
...props
}: TrendChartProps) {
const id = React.useId();
const reducedMotion = React.useSyncExternalStore(subscribeToReducedMotion, getReducedMotionSnapshot, getReducedMotionServerSnapshot);
const animationProps = {
isAnimationActive: animate && !reducedMotion,
animationDuration,
animationEasing: 'ease-out' as const,
};
const dots = type === 'area' && fill === 'dots';
const horizontal = type === 'bar' && layout === 'horizontal';
const hasNegative = React.useMemo(
() =>
data.some((row) =>
series.some((s) => {
const v = row[s.key];
return typeof v === 'number' && v < 0;
}),
),
[data, series],
);
const config = Object.fromEntries(
series.map((s, index) => [s.key, { label: s.label, color: s.color ?? `var(--chart-${(index % 5) + 1})` }]),
) satisfies ChartConfig;
const axes = (
<>
{showGrid ? <CartesianGrid vertical={horizontal} horizontal={!horizontal} /> : null}
{horizontal ? (
<>
<XAxis type="number" hide={!showYAxis} tickLine={false} axisLine={false} tickMargin={8} domain={yDomain} tickFormatter={yFormatter} />
<YAxis type="category" dataKey={xKey} tickLine={false} axisLine={false} tickMargin={8} width="auto" tickFormatter={xFormatter} />
</>
) : (
<>
<XAxis dataKey={xKey} tickLine={false} axisLine={false} tickMargin={8} minTickGap={32} tickFormatter={xFormatter} />
{showYAxis ? (
<YAxis tickLine={false} axisLine={false} tickMargin={8} width="auto" domain={yDomain} tickFormatter={yFormatter} />
) : yDomain ? (
<YAxis hide domain={yDomain} />
) : null}
</>
)}
{hasNegative ? <ReferenceLine {...(horizontal ? { x: 0 } : { y: 0 })} stroke="var(--border)" strokeWidth={1} /> : null}
<ChartTooltip
cursor={false}
content={
<ChartTooltipContent
indicator={type === 'bar' ? 'dashed' : 'dot'}
labelFormatter={(value) => xFormatter(value)}
formatter={
yFormatter
? (value, name) => (
<div className="flex w-full items-center justify-between gap-4">
<span className="text-muted-foreground">{config[name as string]?.label ?? name}</span>
<span className="font-mono font-medium tabular-nums">{yFormatter(Number(value))}</span>
</div>
)
: undefined
}
/>
}
/>
{showLegend ? (
<ChartLegend
verticalAlign={legendPosition}
align={legendAlign}
content={<ChartLegendContent className={legendAlign === 'right' ? 'justify-end' : legendAlign === 'left' ? 'justify-start' : undefined} />}
/>
) : null}
</>
);
const margin = {
top: 8,
right: 8,
bottom: 0,
left: showYAxis || horizontal ? 0 : 8,
};
return (
<ChartContainer data-slot="trend-chart" config={config} className={cn('aspect-auto h-64 w-full', className)} {...props}>
{type === 'bar' ? (
<BarChart data={data} layout={horizontal ? 'vertical' : 'horizontal'} margin={margin} stackOffset={stacked ? 'sign' : 'none'}>
{axes}
{series.map((s, index) => (
<Bar
key={s.key}
{...animationProps}
dataKey={s.key}
fill={`var(--color-${s.key})`}
barSize={barSize}
radius={barCorners(barRadius, stacked, hasNegative, horizontal, index, series.length)}
stackId={stacked ? 'stack' : undefined}
/>
))}
</BarChart>
) : type === 'line' ? (
<LineChart data={data} margin={margin}>
{axes}
{series.map((s) => (
<Line key={s.key} {...animationProps} type="monotone" dataKey={s.key} stroke={`var(--color-${s.key})`} strokeWidth={2} dot={false} />
))}
</LineChart>
) : (
<AreaChart data={data} margin={margin} stackOffset={stacked ? 'sign' : 'none'}>
<defs>
{series.map((s) => (
<linearGradient key={s.key} id={`${id}-${s.key}`} x1="0" y1="0" x2="0" y2="1">
<stop offset="5%" stopColor={`var(--color-${s.key})`} stopOpacity={dots ? 0.08 : 0.6} />
<stop offset="95%" stopColor={`var(--color-${s.key})`} stopOpacity={dots ? 0.9 : 0.05} />
</linearGradient>
))}
{dots ? (
<>
<pattern id={`${id}-dots`} patternUnits="userSpaceOnUse" width={6} height={6}>
<circle cx={3} cy={3} r={1.25} fill="#fff" />
</pattern>
<mask id={`${id}-mask`} maskUnits="userSpaceOnUse" x={0} y={0} width="100%" height="100%">
<rect width="100%" height="100%" fill={`url(#${id}-dots)`} />
</mask>
</>
) : null}
</defs>
{axes}
{series.map((s) => (
<Area
key={s.key}
{...animationProps}
type="monotone"
dataKey={s.key}
stroke={`var(--color-${s.key})`}
fill={`url(#${id}-${s.key})`}
fillOpacity={dots ? 1 : undefined}
strokeWidth={2}
stackId={stacked ? 'stack' : undefined}
shape={dots ? (shapeProps: AreaRevealShapeProps) => <DotGridAreaShape {...shapeProps} maskId={`${id}-mask`} /> : undefined}
/>
))}
</AreaChart>
)}
</ChartContainer>
);
}
export { TrendChart };Usage
import { TrendChart } from "@/components/ui/trend-chart"Examples
Lines
import { TrendChart } from "@/registry/vcare/ui/trend-chart"
const data = [
{ day: "Mon", opd: 120, ipd: 34 }, { day: "Tue", opd: 142, ipd: 38 }, { day: "Wed", opd: 131, ipd: 41 },
{ day: "Thu", opd: 158, ipd: 37 }, { day: "Fri", opd: 171, ipd: 45 }, { day: "Sat", opd: 98, ipd: 30 },
]
export default function TrendChartLines() {
return (
<TrendChart
className="h-64 w-full"
type="line"
showLegend
data={data}
xKey="day"
series={[{ key: "opd", label: "Outpatients" }, { key: "ipd", label: "Admissions" }]}
/>
)
}Stacked bars
import { TrendChart } from "@/registry/vcare/ui/trend-chart"
const data = [
{ week: "W1", cash: 42, card: 31, upi: 58 }, { week: "W2", cash: 38, card: 35, upi: 64 },
{ week: "W3", cash: 45, card: 29, upi: 71 }, { week: "W4", cash: 40, card: 33, upi: 77 },
]
export default function TrendChartStackedBars() {
return (
<TrendChart
className="h-64 w-full"
type="bar"
stacked
showLegend
data={data}
xKey="week"
yFormatter={(v) => `₹${v}k`}
series={[{ key: "cash", label: "Cash" }, { key: "card", label: "Card" }, { key: "upi", label: "UPI" }]}
/>
)
}Exports
TrendChart
Dependencies
recharts@^3.11.1(npm)@vcare/chart(registry)@vcare/utils(registry)
Raw item: /r/trend-chart.json