Pill Bars on Tracks
Weekly bar chart with rounded pill bars on full-height tracks: one day in focus drives the header value and goal percentage, chosen from day buttons or by clicking a bar.
"use client"
import { useState } from "react"
import { Bar, BarChart, Cell, Rectangle, YAxis } from "recharts"
import {
Card,
CardContent,
CardDescription,
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: "steps",
goal: "goal",
ofThe: "of the",
day: "Day",
} as const
const WEEK = [
{ day: "Mon", steps: 6_820 },
{ day: "Tue", steps: 9_410 },
{ day: "Wed", steps: 4_150 },
{ day: "Thu", steps: 11_260 },
{ day: "Fri", steps: 7_930 },
{ day: "Sat", steps: 0 },
{ day: "Sun", steps: 3_480 },
]
const STEP_GOAL = 12_000
const CHART_CONFIG = {
steps: { label: "Steps", color: "var(--chart-3)" },
} satisfies ChartConfig
/** A week of steps as pill bars on full-height tracks, so an empty day still
* reads as a day. One bar is in focus and the header follows it; the day
* buttons, or a click on a bar, move it. */
export default function SelectableGoalPillChart() {
const [selected, setSelected] = useState(3)
const day = WEEK[selected]
return (
<Card className="mx-auto w-full max-w-md">
<CardHeader>
<CardDescription>
<h2>
{day.day} · {COPY.title}
</h2>
</CardDescription>
<CardTitle>
<p className="text-4xl/none font-medium tracking-tight tabular-nums">
{day.steps.toLocaleString("en-US")}
</p>
</CardTitle>
<p className="text-sm/5 text-muted-foreground tabular-nums">
{Math.round((day.steps / STEP_GOAL) * 100)}% {COPY.ofThe}{" "}
{STEP_GOAL.toLocaleString("en-US")} {COPY.goal}
</p>
</CardHeader>
<CardContent className="flex flex-col gap-3">
<ChartContainer
config={CHART_CONFIG}
className="aspect-auto h-44 w-full"
>
<BarChart
data={WEEK}
margin={{ top: 0, right: 0, bottom: 0, left: 0 }}
barCategoryGap="38%"
>
{/* The track is the goal: a full bar meets it. */}
<YAxis hide domain={[0, STEP_GOAL]} />
<Bar
dataKey="steps"
radius={999}
background={{ fill: "var(--muted)", radius: 999 }}
// recharts drops a zero-height bar with its track; a custom
// shape keeps it, so an empty day still shows its track.
shape={(props: React.ComponentProps<typeof Rectangle>) => (
<Rectangle {...props} />
)}
isAnimationActive={false}
onClick={(_, index) => setSelected(index)}
className="cursor-pointer"
>
{WEEK.map((entry, index) => (
<Cell
key={entry.day}
fill={
index === selected ? "var(--color-steps)" : "var(--chart-1)"
}
/>
))}
</Bar>
</BarChart>
</ChartContainer>
<ToggleGroup
size="sm"
spacing={0}
aria-label={COPY.day}
value={[String(selected)]}
onValueChange={(next) => {
if (next[0]) setSelected(Number(next[0]))
}}
className="grid w-full grid-cols-7"
>
{WEEK.map((entry, index) => (
<ToggleGroupItem
key={entry.day}
value={String(index)}
className="min-w-0 px-0 text-xs font-normal text-muted-foreground aria-pressed:bg-transparent aria-pressed:font-medium aria-pressed:text-foreground"
>
{entry.day}
</ToggleGroupItem>
))}
</ToggleGroup>
</CardContent>
</Card>
)
}
Installation
npx shadcn@latest add @01orbital/selectable-goal-pill-chartMake it yours
- Its words are in
COPYat the top ofcomponents/blocks/charts/selectable-goal-pill-chart/index.tsx: change them there, not in the markup. WEEK,STEP_GOALandCHART_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