Balance Backdrop Line
Balance card where the number leads and an edge-to-edge area line sits behind it: no grid or axes, a dotted line at the period opening, a halo on the last point and 1W, 1M and 3M range buttons.
"use client"
import { useId, useState } from "react"
import { Area, AreaChart, ReferenceLine, YAxis } from "recharts"
import {
Card,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import { ChartContainer, type ChartConfig } from "@/components/ui/chart"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
// The copy: change text here, not in the markup.
const COPY = {
title: "Total balance",
since: "since",
range: "Range",
} as const
const RANGES = [
{ days: 7, label: "1W" },
{ days: 30, label: "1M" },
{ days: 90, label: "3M" },
] 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 MONEY = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: 0,
})
const PERCENT = new Intl.NumberFormat("en-US", {
style: "percent",
maximumFractionDigits: 2,
signDisplay: "always",
})
const BALANCE = Array.from({ length: 90 }, (_, day) => ({
date: DATE_FORMAT.format(new Date(START + day * DAY)),
balance: Math.round(
48_000 +
day * 95 +
Math.sin(day / 4) * 1_400 +
Math.sin(day / 11) * 2_600 +
Math.sin(day * 1.7) * 380
),
}))
const CHART_CONFIG = {
balance: { label: "Balance", color: "var(--chart-3)" },
} satisfies ChartConfig
/** A balance card where the number leads and the line is backdrop: no grid or
* axes, a dotted line at the period's opening value and a halo on the last
* point. The range buttons change the period. */
export default function BalanceRangeAreaChart() {
const gradient = useId()
const [days, setDays] = useState<number>(30)
const data = BALANCE.slice(-days)
const first = data[0].balance
const last = data[data.length - 1].balance
const change = (last - first) / first
return (
<Card className="mx-auto w-full max-w-md">
<CardHeader>
<CardDescription>
<h2>{COPY.title}</h2>
</CardDescription>
<CardTitle>
<p className="text-4xl/none font-medium tracking-tight tabular-nums">
{MONEY.format(last)}
</p>
</CardTitle>
<p className="text-sm/5 text-muted-foreground tabular-nums">
<span
className={change >= 0 ? "text-foreground" : "text-destructive"}
>
{PERCENT.format(change)}
</span>{" "}
{COPY.since} {data[0].date}
</p>
</CardHeader>
{/* Outside CardContent so the line runs to the card's edges. */}
<ChartContainer config={CHART_CONFIG} className="aspect-auto h-40 w-full">
<AreaChart
data={data}
// Room on the right for the last point's halo; the left still bleeds.
margin={{ top: 12, right: 12, bottom: 0, left: 0 }}
>
<defs>
<linearGradient id={gradient} x1="0" y1="0" x2="0" y2="1">
<stop
offset="0%"
stopColor="var(--color-balance)"
stopOpacity={0.24}
/>
<stop
offset="100%"
stopColor="var(--color-balance)"
stopOpacity={0}
/>
</linearGradient>
</defs>
<YAxis hide domain={["dataMin - 600", "dataMax + 600"]} />
{/* Where the period opened: above it is up, below it is down. */}
<ReferenceLine
y={first}
stroke="var(--muted-foreground)"
strokeDasharray="1 4"
strokeLinecap="round"
/>
<Area
dataKey="balance"
type="monotone"
stroke="var(--color-balance)"
strokeWidth={2}
fill={`url(#${gradient})`}
isAnimationActive={false}
dot={(props: { cx?: number; cy?: number; index?: number }) =>
props.index === data.length - 1 ? (
<g key="end">
<circle
cx={props.cx}
cy={props.cy}
r={9}
fill="var(--color-balance)"
fillOpacity={0.18}
/>
<circle
cx={props.cx}
cy={props.cy}
r={4}
fill="var(--color-balance)"
/>
</g>
) : (
<g key={props.index} />
)
}
/>
</AreaChart>
</ChartContainer>
<CardFooter>
<ToggleGroup
size="sm"
spacing={1}
aria-label={COPY.range}
value={[String(days)]}
onValueChange={(next) => {
if (next[0]) setDays(Number(next[0]))
}}
>
{RANGES.map((range) => (
<ToggleGroupItem
key={range.days}
value={String(range.days)}
className="text-muted-foreground aria-pressed:text-foreground"
>
{range.label}
</ToggleGroupItem>
))}
</ToggleGroup>
</CardFooter>
</Card>
)
}
Installation
npx shadcn@latest add @01orbital/balance-range-area-chartMake it yours
- Its words are in
COPYat the top ofcomponents/blocks/charts/balance-range-area-chart/index.tsx: change them there, not in the markup. RANGES,START,DAY,DATE_FORMAT,MONEY,PERCENT,BALANCEandCHART_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