Zone Axis Line
Score line chart whose axis names zones instead of counting: tick labels at each zone middle, dotted zone boundaries and a shaded target band, with the current zone beside the value.
"use client"
import { Line, LineChart, ReferenceArea, ReferenceLine, 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: "Typical sleep score · 14-day median",
} as const
// The axis names these zones instead of counting; the last one is the target.
const ZONES = [
{ from: 55, to: 70, label: "Pay attention" },
{ from: 70, to: 80, label: "Fair" },
{ from: 80, to: 88, label: "Good" },
{ from: 88, to: 100, label: "Optimal" },
]
const TARGET = ZONES[ZONES.length - 1]
const SLEEP = Array.from({ length: 28 }, (_, day) => ({
day,
score: Math.round(
80 + Math.sin(day / 3.5) * 7 + Math.sin(day / 1.3) * 2 - (day > 18 ? 6 : 0)
),
}))
const CHART_CONFIG = {
score: { label: "Sleep score", color: "var(--chart-3)" },
} satisfies ChartConfig
const midpoint = (zone: (typeof ZONES)[number]) => (zone.from + zone.to) / 2
/** A score line with words on the axis: each tick names a zone at its middle,
* dotted lines mark the zone boundaries and a band marks the target range. */
export default function ZoneAxisLineChart() {
const latest = SLEEP[SLEEP.length - 1].score
const zone =
ZONES.find((entry) => latest >= entry.from && latest < entry.to) ?? TARGET
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">
{latest}{" "}
<span className="text-base font-normal text-muted-foreground">
{zone.label}
</span>
</p>
</CardTitle>
</CardHeader>
<CardContent>
<ChartContainer
config={CHART_CONFIG}
className="aspect-auto h-48 w-full"
>
<LineChart
data={SLEEP}
margin={{ top: 8, right: 0, bottom: 0, left: 0 }}
>
<ReferenceArea
y1={TARGET.from}
y2={TARGET.to}
fill="var(--chart-1)"
fillOpacity={0.18}
/>
{ZONES.slice(1).map((entry) => (
<ReferenceLine
key={entry.from}
y={entry.from}
stroke="var(--border)"
strokeDasharray="1 4"
strokeLinecap="round"
/>
))}
{/* Width fits the longest zone name on one line. Stock
ChartContainer's label color misses recharts 3's label layer. */}
<YAxis
orientation="right"
domain={[ZONES[0].from, TARGET.to]}
ticks={ZONES.map(midpoint)}
tickFormatter={(value: number) =>
ZONES.find((entry) => midpoint(entry) === value)?.label ?? ""
}
tickLine={false}
axisLine={false}
width={104}
tick={{ fill: "var(--muted-foreground)", fontSize: 12 }}
/>
<Line
dataKey="score"
type="monotone"
stroke="var(--color-score)"
strokeWidth={2}
dot={false}
isAnimationActive={false}
/>
</LineChart>
</ChartContainer>
</CardContent>
</Card>
)
}
Installation
npx shadcn@latest add @01orbital/zone-axis-line-chartMake it yours
- Its words are in
COPYat the top ofcomponents/blocks/charts/zone-axis-line-chart/index.tsx: change them there, not in the markup. ZONES,TARGET,SLEEPandCHART_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