Split at Zero
Profit and loss area chart whose line and fill change color at zero: the chart color above a dotted baseline, muted gray below, from one gradient stop.
"use client"
import { useId } from "react"
import { Area, AreaChart, ReferenceLine, YAxis } from "recharts"
import {
Card,
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: "Profit and loss",
days: "days",
} as const
const PNL = Array.from({ length: 40 }, (_, day) => ({
day,
pnl: Math.round(
Math.sin(day / 5) * 420 + Math.sin(day / 2.1) * 140 + (day - 20) * 9
),
}))
const MONEY = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2,
})
const CHART_CONFIG = {
pnl: { label: "Profit and loss", color: "var(--chart-3)" },
} satisfies ChartConfig
/** Profit and loss as one line that changes color at zero: the chart color
* above the baseline, muted gray below it, from a single gradient
* whose stop sits exactly at zero. */
export default function ZeroSplitAreaChart() {
const stroke = useId()
const fill = useId()
const values = PNL.map((entry) => entry.pnl)
const max = Math.max(...values)
const min = Math.min(...values)
// Where zero falls between the top (0) and bottom (1) of the plot.
const zero = max <= 0 ? 0 : min >= 0 ? 1 : max / (max - min)
const total = values.reduce((sum, value) => sum + value, 0)
return (
<Card className="mx-auto w-full max-w-md">
<CardHeader>
<CardDescription>
<h2>
{COPY.title} · {PNL.length} {COPY.days}
</h2>
</CardDescription>
<CardTitle>
<p
className={
total < 0
? "text-4xl/none font-medium tracking-tight text-muted-foreground tabular-nums"
: "text-4xl/none font-medium tracking-tight tabular-nums"
}
>
{MONEY.format(total)}
</p>
</CardTitle>
</CardHeader>
{/* Outside CardContent so the line runs to the card's edges. */}
<ChartContainer config={CHART_CONFIG} className="aspect-auto h-44 w-full">
<AreaChart data={PNL} margin={{ top: 8, right: 0, bottom: 8, left: 0 }}>
<defs>
<linearGradient id={stroke} x1="0" y1="0" x2="0" y2="1">
<stop offset={zero} stopColor="var(--color-pnl)" />
<stop offset={zero} stopColor="var(--muted-foreground)" />
</linearGradient>
<linearGradient id={fill} x1="0" y1="0" x2="0" y2="1">
<stop
offset={0}
stopColor="var(--color-pnl)"
stopOpacity={0.22}
/>
<stop
offset={zero}
stopColor="var(--color-pnl)"
stopOpacity={0.02}
/>
<stop
offset={zero}
stopColor="var(--muted-foreground)"
stopOpacity={0.02}
/>
<stop
offset={1}
stopColor="var(--muted-foreground)"
stopOpacity={0.22}
/>
</linearGradient>
</defs>
{/* The domain must be the data's exact range for the stop to land on zero. */}
<YAxis hide domain={[min, max]} />
<ReferenceLine
y={0}
stroke="var(--muted-foreground)"
strokeDasharray="1 4"
strokeLinecap="round"
/>
<Area
dataKey="pnl"
type="monotone"
baseValue={0}
stroke={`url(#${stroke})`}
strokeWidth={2}
fill={`url(#${fill})`}
dot={false}
isAnimationActive={false}
/>
</AreaChart>
</ChartContainer>
</Card>
)
}
Installation
npx shadcn@latest add @01orbital/zero-split-area-chartMake it yours
- Its words are in
COPYat the top ofcomponents/blocks/charts/zero-split-area-chart/index.tsx: change them there, not in the markup. PNL,MONEYandCHART_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