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
Bar Chart
Vertical or horizontal bars, stacked or grouped, with reference lines.
import { BarChart } from "@/registry/vcare/ui/bar-chart"
const data = [
{ hour: "8am", patients: 12 }, { hour: "9am", patients: 28 }, { hour: "10am", patients: 41 }, { hour: "11am", patients: 38 },
{ hour: "12pm", patients: 22 }, { hour: "1pm", patients: 15 }, { hour: "2pm", patients: 30 }, { hour: "3pm", patients: 34 },
]
export default function BarChartDemo() {
return (
<BarChart
className="h-64 w-full"
data={data}
xKey="hour"
yKey="patients"
highlight={(row) => (row.patients as number) > 35}
referenceLines={[{ y: 30, label: "Capacity", dashed: true }]}
/>
)
}Installation
npx shadcn@latest add @vcare/bar-chartOr install directly from the URL: https://ui.vcarehms.com/r/bar-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/bar-chart.tsx
'use client';
import * as React from 'react';
import { Bar, BarChart as RechartsBarChart, CartesianGrid, Cell, Rectangle, ReferenceLine, XAxis, YAxis } from 'recharts';
import { cn } from '@/registry/vcare/lib/utils';
import { ChartContainer, ChartTooltip } from '@/registry/vcare/ui/chart';
export type BarRow = Record<string, unknown>;
export interface BarReferenceLine {
/** Horizontal line at this y value. */
y: number;
label?: string;
/** Any CSS color. Defaults to the muted foreground. */
color?: string;
/** Defaults to dashed. */
dashed?: boolean;
}
export interface BarChartProps extends Omit<React.ComponentProps<typeof ChartContainer>, 'config' | 'children'> {
data: BarRow[];
/** Key of the x-axis value in each row. */
xKey: string;
/** Key of the bar value in each row. */
yKey: string;
/**
* Key of a coarser label, e.g. "month" for weekly rows. When set, the x-axis
* shows one tick per group centered under its bars instead of a tick per bar.
*/
groupKey?: string;
/** "gradient" fades each bar toward the bottom; "striped" fills it with diagonal bands. */
variant?: 'gradient' | 'striped' | 'solid';
/** Any CSS color for highlighted bars. Defaults to chart-1. */
color?: string;
/** Any CSS color for bars that are not highlighted. Defaults to the muted foreground. */
mutedColor?: string;
/** Return true for bars drawn in color. Every bar is highlighted when omitted. */
highlight?: (row: BarRow, index: number) => boolean;
grid?: 'dashed' | 'solid' | 'none';
showYAxis?: boolean;
showTooltip?: boolean;
/** Draw a hollow ring on top of the hovered bar. */
showActiveMarker?: boolean;
barRadius?: number;
/** Animate bars after hydration and when data changes. Respects reduced motion. */
animate?: boolean;
animationDuration?: number;
/** Horizontal lines, e.g. a goal or an average. The y-axis extends to fit them. */
referenceLines?: BarReferenceLine[];
xFormatter?: (value: unknown) => string;
yFormatter?: (value: number) => string;
/** Secondary line of the tooltip. Defaults to the formatted x value. */
tooltipLabel?: (row: BarRow, index: number) => React.ReactNode;
onBarClick?: (row: BarRow, index: number) => void;
}
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' && /^\d{4}-\d{2}-\d{2}/.test(value)) {
const [y, m, d] = value.split('-').map(Number);
return new Date(y!, m! - 1, d).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
});
}
return String(value);
}
/** One tick per group, placed under the middle bar of the group. */
function groupTicks(data: BarRow[], groupKey: string) {
const labels: string[] = Array.from({ length: data.length }, () => '');
let start = 0;
for (let i = 1; i <= data.length; i++) {
if (i === data.length || data[i]![groupKey] !== data[start]![groupKey]) {
labels[Math.floor((start + i - 1) / 2)] = String(data[start]![groupKey] ?? '');
start = i;
}
}
return labels;
}
interface ShapeProps {
x?: number;
y?: number;
width?: number;
height?: number;
index?: number;
}
/** A single-series bar chart with gradient or striped fills, highlighted bars, and a hover marker. */
function BarChart({
data,
xKey,
yKey,
groupKey,
variant = 'gradient',
color = 'var(--chart-1)',
mutedColor = 'var(--muted-foreground)',
highlight,
grid = 'dashed',
showYAxis = false,
showTooltip = true,
showActiveMarker = true,
barRadius = 6,
animate = true,
animationDuration = 500,
referenceLines = [],
xFormatter = defaultXFormatter,
yFormatter,
tooltipLabel,
onBarClick,
className,
...props
}: BarChartProps) {
const id = React.useId();
const reducedMotion = React.useSyncExternalStore(subscribeToReducedMotion, getReducedMotionSnapshot, getReducedMotionServerSnapshot);
const isHighlighted = React.useCallback((index: number) => (highlight ? highlight(data[index]!, index) : true), [data, highlight]);
const tickLabels = React.useMemo(() => (groupKey ? groupTicks(data, groupKey) : null), [data, groupKey]);
const fillFor = (index: number) => `url(#${id}-${isHighlighted(index) ? 'on' : 'off'})`;
const activeBar = (shape: ShapeProps) => {
const { x = 0, y = 0, width = 0, height = 0, index = 0 } = shape;
const on = isHighlighted(index);
return (
<g>
<Rectangle {...shape} fill={fillFor(index)} />
{showActiveMarker && height > 0 ? (
<circle cx={x + width / 2} cy={y} r={4} fill="var(--background)" stroke={on ? color : mutedColor} strokeWidth={2} />
) : null}
</g>
);
};
return (
<ChartContainer data-slot="bar-chart" config={{ [yKey]: { label: yKey, color } }} className={cn('aspect-auto h-64 w-full', className)} {...props}>
<RechartsBarChart data={data} margin={{ top: 12, right: 8, bottom: 0, left: showYAxis ? 0 : 8 }} barCategoryGap="28%">
<defs>
{variant === 'gradient' ? (
<>
<linearGradient id={`${id}-on`} x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor={color} stopOpacity={1} />
<stop offset="100%" stopColor={color} stopOpacity={0.6} />
</linearGradient>
<linearGradient id={`${id}-off`} x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor={mutedColor} stopOpacity={0.35} />
<stop offset="100%" stopColor={mutedColor} stopOpacity={0.18} />
</linearGradient>
</>
) : variant === 'striped' ? (
<>
{(['on', 'off'] as const).map((state) => {
const fill = state === 'on' ? color : mutedColor;
const opacity = state === 'on' ? 1 : 0.3;
return (
<pattern key={state} id={`${id}-${state}`} patternUnits="userSpaceOnUse" width={10} height={10} patternTransform="rotate(45)">
<rect width={10} height={10} fill={fill} fillOpacity={opacity} />
<rect width={5} height={10} fill="var(--background)" fillOpacity={0.25} />
</pattern>
);
})}
</>
) : (
<>
<linearGradient id={`${id}-on`}>
<stop stopColor={color} />
</linearGradient>
<linearGradient id={`${id}-off`}>
<stop stopColor={mutedColor} stopOpacity={0.3} />
</linearGradient>
</>
)}
</defs>
{grid !== 'none' ? <CartesianGrid vertical={false} strokeDasharray={grid === 'dashed' ? '6 6' : undefined} /> : null}
<XAxis
dataKey={xKey}
tickLine={false}
axisLine={false}
tickMargin={10}
interval={tickLabels ? 0 : 'preserveStartEnd'}
minTickGap={tickLabels ? 0 : 24}
tickFormatter={tickLabels ? (_, index) => tickLabels[index] ?? '' : (v) => xFormatter(v)}
/>
<YAxis hide={!showYAxis} tickLine={false} axisLine={false} tickMargin={8} width={44} tickFormatter={yFormatter} />
{showTooltip ? (
<ChartTooltip
cursor={false}
isAnimationActive={false}
content={({ active, payload }) => {
const entry = payload?.[0];
if (!active || !entry) return null;
const row = entry.payload as BarRow;
const index = data.indexOf(row);
const value = Number(row[yKey]);
return (
<div className="bg-background rounded-lg border px-3 py-2 text-xs shadow-md">
<div className="text-sm font-semibold tabular-nums">{yFormatter ? yFormatter(value) : String(value)}</div>
<div className="text-muted-foreground">{tooltipLabel ? tooltipLabel(row, index) : xFormatter(row[xKey])}</div>
</div>
);
}}
/>
) : null}
<Bar
dataKey={yKey}
radius={barRadius}
isAnimationActive={animate && !reducedMotion}
animationDuration={animationDuration}
animationEasing="ease-out"
activeBar={activeBar}
onClick={onBarClick ? (_, index) => onBarClick(data[index]!, index) : undefined}
className={onBarClick ? 'cursor-pointer' : undefined}
>
{data.map((_, index) => (
<Cell key={index} fill={fillFor(index)} />
))}
</Bar>
{referenceLines.map((line, index) => (
<ReferenceLine
key={index}
y={line.y}
ifOverflow="extendDomain"
stroke={line.color ?? 'var(--muted-foreground)'}
strokeDasharray={line.dashed === false ? undefined : '4 4'}
label={
line.label
? {
value: line.label,
position: 'insideTopRight',
fill: line.color ?? 'var(--muted-foreground)',
fontSize: 11,
}
: undefined
}
/>
))}
</RechartsBarChart>
</ChartContainer>
);
}
export { BarChart };Usage
import { BarChart } from "@/components/ui/bar-chart"Examples
Striped
import { BarChart } from "@/registry/vcare/ui/bar-chart"
const data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].map((day, i) => ({ day, tests: [64, 72, 58, 81, 90, 44, 21][i] }))
export default function BarChartStriped() {
return <BarChart className="h-56 w-full" variant="striped" grid="none" data={data} xKey="day" yKey="tests" />
}Exports
BarChart
Dependencies
recharts@^3.11.1(npm)@vcare/chart(registry)@vcare/utils(registry)
Raw item: /r/bar-chart.json