Scrub Rule Area
Area chart with a scrub rule: a full-height line follows the pointer or arrow keys, the active point carries a halo and the date and value stay pinned in a pill above the plot.
"use client"
import { useId } from "react"
import { Area, AreaChart, Tooltip, XAxis, YAxis } from "recharts"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import { ChartContainer, type ChartConfig } from "@/components/ui/chart"
// The copy: change text here, not in the markup.
const COPY = {
title: "Visitors",
last: "last",
days: "days",
} as const
const START = Date.UTC(2026, 7, 1)
const DAY = 86_400_000
const DATE_FORMAT = new Intl.DateTimeFormat("en-US", {
month: "short",
day: "numeric",
timeZone: "UTC",
})
const VISITORS = Array.from({ length: 30 }, (_, day) => ({
date: DATE_FORMAT.format(new Date(START + day * DAY)),
visitors: Math.round(
1_800 + Math.sin(day / 3) * 420 + Math.sin(day / 7) * 260 + day * 22
),
}))
const CHART_CONFIG = {
visitors: { label: "Visitors", color: "var(--chart-3)" },
} satisfies ChartConfig
/** Visitors over a month with a scrub rule: while the pointer or the arrow
* keys move along the chart, a full-height rule follows and the reading stays
* pinned to the top instead of covering the line. */
export default function ScrubAreaChart() {
const gradient = useId()
const latest = VISITORS[VISITORS.length - 1].visitors
return (
<Card className="mx-auto w-full max-w-md">
<CardHeader>
<CardDescription>
<h2>
{COPY.title} · {COPY.last} {VISITORS.length} {COPY.days}
</h2>
</CardDescription>
<CardTitle>
<p className="text-4xl/none font-medium tracking-tight tabular-nums">
{latest.toLocaleString("en-US")}
</p>
</CardTitle>
</CardHeader>
<CardContent>
<ChartContainer
config={CHART_CONFIG}
className="aspect-auto h-48 w-full"
>
<AreaChart
accessibilityLayer
data={VISITORS}
// The top margin is the pinned label's lane.
margin={{ top: 32, right: 0, bottom: 0, left: 0 }}
>
<defs>
<linearGradient id={gradient} x1="0" y1="0" x2="0" y2="1">
<stop
offset="0%"
stopColor="var(--color-visitors)"
stopOpacity={0.2}
/>
<stop
offset="100%"
stopColor="var(--color-visitors)"
stopOpacity={0}
/>
</linearGradient>
</defs>
<YAxis hide domain={["dataMin - 200", "dataMax + 100"]} />
{/* Stock ChartContainer's label color misses recharts 3's label layer. */}
<XAxis
dataKey="date"
tickLine={false}
axisLine={false}
tickMargin={8}
interval="preserveStartEnd"
minTickGap={48}
tick={{ fill: "var(--muted-foreground)" }}
/>
{/* y 0 pins the label to the top; offset 0 centers it on the rule. */}
<Tooltip
position={{ y: 0 }}
offset={0}
cursor={{ stroke: "var(--foreground)", strokeWidth: 1 }}
content={<PinnedLabel />}
isAnimationActive={false}
/>
<Area
dataKey="visitors"
type="monotone"
stroke="var(--color-visitors)"
strokeWidth={2}
fill={`url(#${gradient})`}
isAnimationActive={false}
activeDot={{
r: 4,
fill: "var(--color-visitors)",
stroke: "var(--color-visitors)",
strokeOpacity: 0.25,
strokeWidth: 8,
}}
/>
</AreaChart>
</ChartContainer>
</CardContent>
</Card>
)
}
/** The reading at the rule: the date and value in a pill centered over it. */
function PinnedLabel({
active,
payload,
}: {
active?: boolean
payload?: ReadonlyArray<{ value?: unknown; payload?: { date?: string } }>
}) {
if (!active || !payload?.length) return null
const point = payload[0]
return (
<div className="-translate-x-1/2 rounded-full bg-foreground px-2.5 py-1 text-xs/4 font-medium whitespace-nowrap text-background tabular-nums">
{point.payload?.date} · {Number(point.value).toLocaleString("en-US")}
</div>
)
}
Installation
npx shadcn@latest add @01orbital/scrub-area-chartMake it yours
- Its words are in
COPYat the top ofcomponents/blocks/charts/scrub-area-chart/index.tsx: change them there, not in the markup. START,DAY,DATE_FORMAT,VISITORSandCHART_CONFIG, also at the top of the file, hold its lists and settings.
Built from
npm: recharts@3.8.0. The CLI installs it for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on