Fine Exercise Lines
Fine line chart for fitness dashboards: orange exercise minutes against a dashed gray average, with a current value and labeled line keys.
"use client"
import { Line, LineChart, XAxis } from "recharts"
import { Eyebrow } from "@/components/eyebrow/eyebrow"
import {
Stat,
StatGroup,
StatValue,
} from "@/components/stats/stats"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
type ChartConfig,
} from "@/components/ui/chart"
import { Separator } from "@/components/ui/separator"
// The copy: change text here, not in the markup.
const COPY = {
title: "Exercise minutes",
description: "Your exercise minutes are ahead of where you normally are.",
range: "Last 7 days",
} as const
const EXERCISE_MINUTES = [
{ day: "Monday", today: 240, average: 400 },
{ day: "Tuesday", today: 139, average: 300 },
{ day: "Wednesday", today: 980, average: 200 },
{ day: "Thursday", today: 390, average: 278 },
{ day: "Friday", today: 480, average: 189 },
{ day: "Saturday", today: 380, average: 239 },
{ day: "Sunday", today: 430, average: 349 },
]
// The dashed gray line distinguishes the average from the orange observations.
const CHART_CONFIG = {
today: { label: "Today", color: "var(--chart-3)" },
average: { label: "Average", color: "var(--muted-foreground)" },
} satisfies ChartConfig
const LATEST = EXERCISE_MINUTES[EXERCISE_MINUTES.length - 1]
/** Fine exercise and average lines with Activity Goal's spacing and caption. */
export default function CompactExerciseMinutes() {
return (
<Card className="mx-auto w-full max-w-md gap-8 [--card-spacing:24px]">
<CardHeader>
<CardTitle>
<h2>{COPY.title}</h2>
</CardTitle>
<CardDescription>{COPY.description}</CardDescription>
</CardHeader>
<CardContent className="grid min-w-0 gap-8">
<StatGroup cols={1} gap="none">
<Stat className="justify-items-center gap-1 text-center">
<StatValue className="text-4xl/10 tabular-nums">
{LATEST.today}
</StatValue>
<Eyebrow variant="mono">min</Eyebrow>
</Stat>
</StatGroup>
<figure className="grid min-w-0 gap-3">
<ChartContainer config={CHART_CONFIG} className="h-36 w-full min-w-0">
<LineChart
accessibilityLayer
data={EXERCISE_MINUTES}
margin={{ top: 8, right: 4, left: 4, bottom: 4 }}
>
<XAxis dataKey="day" hide />
<Line
type="linear"
dataKey="today"
strokeWidth={1.5}
stroke="var(--color-today)"
dot={false}
activeDot={{ r: 3 }}
/>
<Line
type="linear"
dataKey="average"
strokeWidth={1.5}
stroke="var(--color-average)"
strokeDasharray="2 4"
dot={false}
activeDot={{ r: 3 }}
/>
<ChartTooltip cursor={false} content={<ChartTooltipContent />} />
</LineChart>
</ChartContainer>
<Separator />
<figcaption className="flex flex-wrap items-baseline justify-between gap-x-4 gap-y-2">
<Eyebrow variant="mono" render={<span />}>
{COPY.range}
</Eyebrow>
<span className="flex flex-wrap gap-3 text-xs">
<span className="flex items-center gap-1.5">
<span
aria-hidden="true"
className="w-3 border-t border-chart-3"
/>
Today
</span>
<span className="flex items-center gap-1.5">
<span
aria-hidden="true"
className="w-3 border-t border-dashed border-muted-foreground"
/>
Average
</span>
</span>
</figcaption>
</figure>
</CardContent>
</Card>
)
}
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
const eyebrowVariants = cva("text-eyebrow", {
variants: {
/**
* `default` is a 14px/20px label. `icon` adds a row with a 16px icon
* and a 12px gap; put the icon first and give it `aria-hidden`.
* `mono` is a 12px/20px label in the mono face, in sentence case like
* the others: the kit sets no label in capitals.
*/
variant: {
default: "text-sm/5 font-medium text-eyebrow",
icon: "flex items-center gap-3 text-sm/5 font-medium [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
mono: "font-mono text-xs/5 font-medium",
},
},
defaultVariants: { variant: "default" },
})
/**
* A short label above a heading, rendered as a `<p>` at weight 500.
* The default is 14px/20px in the kit's eyebrow color. The surrounding
* layout controls the space to the heading. Use `className` to customize
* the typography or color, and `render` to change the element: a `p` is
* right when the label stands alone as a line of text; pass
* `render={<span />}` or `render={<div />}` when it sits inside a heading,
* link, button or other inline text, where a `p` is not valid HTML.
*/
function Eyebrow({
className,
variant = "default",
render,
...props
}: useRender.ComponentProps<"p"> & VariantProps<typeof eyebrowVariants>) {
return useRender({
defaultTagName: "p",
render,
state: { slot: "eyebrow", variant },
props: mergeProps<"p">(
{ className: cn(eyebrowVariants({ variant }), className) },
props
),
})
}
export { Eyebrow, eyebrowVariants }
import type { ComponentProps } from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { Grid, type GridProps } from "@/components/grid/grid"
/**
* A list of `Stat` items laid out by `Grid`, rendered as a `ul`. It defaults
* to 3 columns and the large gap. Like `Grid`, its columns follow the width
* it is given, so size its parent to narrow it. `className` lands on the
* list.
*/
function StatGroup({
cols = 3,
gap = "lg",
...props
}: ComponentProps<"ul"> & {
/**
* The most columns, 3 by default. They step up as the list's space
* widens, the way `Grid`'s do.
*/
cols?: GridProps["cols"]
/**
* Space between stats: `xs` 8px, `sm` 16px, `md` 24px, `lg` 40px, `xl` 64px, `px` 1px, `none` 0. `lg`
* is the default.
*/
gap?: GridProps["gap"]
}) {
return (
<Grid
data-slot="stat-group"
render={<ul role="list" />}
cols={cols}
gap={gap}
// Grid's props are typed for its default div; this group renders a list.
{...(props as Omit<GridProps, "cols" | "gap">)}
/>
)
}
// card sets the stat on a filled panel; rule sets a line down its left side.
const statVariants = cva("grid content-start gap-2", {
variants: {
/**
* The stat style. `default` (the default) adds nothing. `card` sets it
* on a `rounded-xl` panel with 32px padding, filled with `muted`, or
* with `card` and `card-foreground` text in dark mode. `rule` draws a
* 1px line in `border` down its left side, 24px from the text.
*/
variant: {
default: "",
card: "rounded-xl bg-muted p-8 dark:bg-card dark:text-card-foreground",
rule: "border-l border-border pl-6",
},
},
defaultVariants: { variant: "default" },
})
/**
* One figure and its label, as an `li` inside `StatGroup`. Holds a
* `StatValue` and a `StatLabel`, stacked with 8px gaps.
*
* `card` sets it on a filled, rounded panel with 32px padding. `rule` draws a
* line down its left side.
*/
function Stat({
className,
variant = "default",
...props
}: ComponentProps<"li"> & VariantProps<typeof statVariants>) {
return (
<li
data-slot="stat"
data-variant={variant}
className={cn(statVariants({ variant }), className)}
{...props}
/>
)
}
/** The figure itself, such as a number or a percentage, at 32px in the body font. */
function StatValue({ className, ...props }: ComponentProps<"p">) {
return (
<p
data-slot="stat-value"
className={cn("text-[2rem]/10 tracking-tight", className)}
{...props}
/>
)
}
/**
* The caption under the figure: what it measures, in a few words. Small
* muted text, 14px on a 20px line, so the number carries the weight. Longer
* explanation belongs in the section's lead or a note, not here.
*/
function StatLabel({ className, ...props }: ComponentProps<"p">) {
return (
<p
data-slot="stat-label"
className={cn("text-sm/5 text-pretty text-muted-foreground", className)}
{...props}
/>
)
}
export { StatGroup, Stat, StatValue, StatLabel }
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
const GRID_DEFAULTS = { cols: 3, gap: "lg" } as const
const containerGridVariants = cva("grid w-full grid-cols-1", {
variants: {
/**
* The most columns. Below 40rem of screen width the grid is one
* column (two for `6`); it steps up at 40rem and 48rem: `2` is two
* columns from 40rem, `3` two then three, `4` two then four, `5` three
* then five, and `6` three, then four, then six from 64rem. The default
* is 3.
*/
cols: {
1: "",
2: "sm:grid-cols-2",
3: "sm:grid-cols-2 md:grid-cols-3",
4: "sm:grid-cols-2 md:grid-cols-4",
5: "sm:grid-cols-3 md:grid-cols-5",
6: "grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6",
},
/**
* Space between items: `xs` 8px, `sm` 16px, `md` 24px, `lg` 40px, `xl` 64px, `px` 1px, `none` 0. `lg`
* is the default. The sizes are the `--gap-*` values in globals.css.
*/
gap: {
xs: "gap-(--gap-xs)",
sm: "gap-(--gap-sm)",
md: "gap-(--gap-md)",
lg: "gap-(--gap-lg)",
xl: "gap-(--gap-xl)",
px: "gap-px",
none: "gap-0",
},
},
defaultVariants: GRID_DEFAULTS,
})
type GridState = { slot: "grid" } & VariantProps<typeof containerGridVariants>
type GridProps = useRender.ComponentProps<"div", GridState> &
VariantProps<typeof containerGridVariants>
/**
* Columns follow the screen, stepping at 40rem and 48rem (and 64rem for six).
* Gap sizes read the shared `--gap-*` values in globals.css. The grid fills
* its parent's width: without that, a grid given `mx-auto` or set inside a
* centering parent shrinks to fit its content, and a part built on it that
* is a container collapses to nothing.
*
* className, render and every other prop go to the grid. To narrow a Grid, pass
* a `max-w-*` class or size its parent.
*/
function Grid({
className,
cols = GRID_DEFAULTS.cols,
gap = GRID_DEFAULTS.gap,
render,
...props
}: GridProps) {
const grid = useRender({
defaultTagName: "div",
render,
state: { slot: "grid", cols, gap },
props: mergeProps<"div">(
{ className: cn(containerGridVariants({ cols, gap }), className) },
props
),
})
return grid
}
export { Grid, type GridProps }
Installation
npx shadcn@latest add @01orbital/compact-exercise-minutesMake it yours
- Its words are in
COPYat the top ofcomponents/blocks/charts/compact-exercise-minutes/index.tsx: change them there, not in the markup. EXERCISE_MINUTES,CHART_CONFIGandLATEST, 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