Hatched Partial Period
Monthly bar chart that marks the unfinished month: the amount so far is solid and the projection for the rest is hatched, with an on-pace total and a legend.
"use client"
import { useId } from "react"
import { Bar, BarChart, XAxis } 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: "Revenue",
soFar: "so far",
onPace: "On pace for",
} as const
// The last month is two thirds through: actual so far, projected for the rest.
const MONTHS = [
{ month: "Jun", actual: 31_200, projected: 0 },
{ month: "Jul", actual: 34_800, projected: 0 },
{ month: "Aug", actual: 33_100, projected: 0 },
{ month: "Sep", actual: 38_900, projected: 0 },
{ month: "Oct", actual: 41_600, projected: 0 },
{ month: "Nov", actual: 28_400, projected: 15_100 },
]
const MONEY = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: 0,
})
const CHART_CONFIG = {
actual: { label: "So far", color: "var(--chart-3)" },
projected: { label: "Projected", color: "var(--chart-3)" },
} satisfies ChartConfig
/** Monthly revenue where the unfinished month says so: what has come in is
* solid and the projection for the rest of the month is hatched, so a short
* last bar never reads as a bad month. */
export default function HatchedProjectionBarChart() {
const hatch = useId()
const current = MONTHS[MONTHS.length - 1]
return (
<Card className="mx-auto w-full max-w-md">
<CardHeader>
<CardDescription>
<h2>
{COPY.title} · {current.month} {COPY.soFar}
</h2>
</CardDescription>
<CardTitle>
<p className="text-4xl/none font-medium tracking-tight tabular-nums">
{MONEY.format(current.actual)}
</p>
</CardTitle>
<p className="text-sm/5 text-muted-foreground tabular-nums">
{COPY.onPace} {MONEY.format(current.actual + current.projected)}
</p>
</CardHeader>
<CardContent className="flex flex-col gap-4">
<ChartContainer
config={CHART_CONFIG}
className="aspect-auto h-44 w-full"
>
<BarChart
data={MONTHS}
margin={{ top: 0, right: 0, bottom: 0, left: 0 }}
>
<defs>
<pattern
id={hatch}
width={6}
height={6}
patternUnits="userSpaceOnUse"
patternTransform="rotate(45)"
>
<line
x1={0}
y1={0}
x2={0}
y2={6}
stroke="var(--color-projected)"
strokeWidth={2}
/>
</pattern>
</defs>
{/* Stock ChartContainer's label color misses recharts 3's label layer. */}
<XAxis
dataKey="month"
tickLine={false}
axisLine={false}
tickMargin={8}
tick={{ fill: "var(--muted-foreground)" }}
/>
<Bar
dataKey="actual"
stackId="month"
fill="var(--color-actual)"
isAnimationActive={false}
/>
{/* 0.85 keeps the hatch visible on a dark card. */}
<Bar
dataKey="projected"
stackId="month"
fill={`url(#${hatch})`}
fillOpacity={0.85}
radius={[4, 4, 0, 0]}
isAnimationActive={false}
/>
</BarChart>
</ChartContainer>
<ul className="flex flex-wrap gap-x-5 gap-y-1 text-xs/5 text-muted-foreground">
<li className="flex items-center gap-2">
<span
aria-hidden="true"
className="size-2.5 rounded-xs bg-(--color-actual)"
/>
{CHART_CONFIG.actual.label}
</li>
<li className="flex items-center gap-2">
<svg
aria-hidden="true"
viewBox="0 0 10 10"
className="size-2.5 rounded-xs"
>
<rect
width={10}
height={10}
fill={`url(#${hatch})`}
opacity={0.85}
/>
</svg>
{CHART_CONFIG.projected.label}
</li>
</ul>
</CardContent>
</Card>
)
}
Installation
npx shadcn@latest add @01orbital/hatched-projection-bar-chartMake it yours
- Its words are in
COPYat the top ofcomponents/blocks/charts/hatched-projection-bar-chart/index.tsx: change them there, not in the markup. MONTHS,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