Follow values across an ordered series. This module includes both a line and a filled area presentation.
Line chart example
Example.jsx
import { LineChart } from "./ui/line-chart";
import "./example.css";
export default function Example() {
return (
<section class="sample">
<header>
<h2>Proof turnaround</h2>
<p>Median time from submission to approval.</p>
</header>
<LineChart
label="Proof turnaround time"
unit=" h"
height={115}
area
data={[
{ label: "Mon", value: 24 },
{ label: "Tue", value: 19 },
{ label: "Wed", value: 21 },
{ label: "Thu", value: 14 },
{ label: "Fri", value: 12 },
]}
/>
</section>
);
}
example.css
@media screen and (prefers-reduced-motion: no-preference) {
#example button:is(.v-primary, .v-secondary):enabled {
transition: transform 120ms ease-out;
}
#example button:is(.v-primary, .v-secondary):enabled:active {
transform: scale(0.98);
}
}
@media (prefers-reduced-motion: reduce) {
#example button:is(.v-primary, .v-secondary):enabled:active {
transition: none;
transform: none;
}
}
* {
box-sizing: border-box;
}
:root {
--example-seed: var(--aster-primary);
--example-secondary-text: var(--aster-on-surface-subtle);
}
body {
--aster-primary: color-mix(in srgb, var(--example-seed) 84%, var(--aster-on-surface));
--aster-on-surface-subtle: color-mix(in srgb, var(--example-secondary-text) 84%, var(--aster-on-surface));
--aster-input-border: color-mix(in srgb, var(--aster-on-surface) 55%, var(--aster-surface));
margin: 0;
padding: clamp(0.8rem, 4vw, 1.5rem);
color: var(--aster-on-surface);
background: var(--aster-surface-muted);
font: 1rem/1.5 var(--aster-font-sans);
}
#example {
display: grid;
gap: 0.8rem;
min-width: 0;
align-content: start;
}
#example > button,
.sample > button {
justify-self: start;
}
.sample,
.tile {
background: var(--aster-surface);
border: 1px solid var(--aster-border);
border-radius: var(--aster-radius-lg);
}
.sample {
display: grid;
gap: 0.8rem;
padding: clamp(0.85rem, 3vw, 1.35rem);
box-shadow: var(--aster-shadow-xs);
min-width: 0;
}
.tile {
padding: 1rem;
min-width: 0;
}
h2,
h3,
p,
dl,
ul {
margin-block: 0;
}
h2,
h3 {
letter-spacing: -0.025em;
line-height: 1.2;
}
h2 {
font-size: 1.25rem;
}
h3 {
font-size: 1rem;
}
.sample > header,
.identity > div,
.receipt {
display: grid;
gap: 0.25rem;
}
.sample > header p,
.tile p,
small {
color: var(--aster-on-surface-muted);
}
small {
font-size: 0.82rem;
}
.split,
.actions,
.identity {
display: flex;
align-items: center;
gap: 0.65rem;
flex-wrap: wrap;
}
.split {
justify-content: space-between;
padding-block: 0.4rem;
border-bottom: 1px solid var(--aster-border);
}
.split:last-child {
border-bottom: 0;
}
.metric {
font-size: 2rem;
letter-spacing: -0.06em;
}
.facts {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.45rem 1rem;
}
.facts dt {
color: var(--aster-on-surface-muted);
}
.facts dd {
margin: 0;
font-weight: 600;
}
.type-sample {
font:
2rem/1.2 Georgia,
serif;
}
.listing {
padding-inline-start: 1.25rem;
}
.ink-strip {
height: 1.2rem;
background: var(--aster-primary);
border-radius: var(--aster-radius-sm);
}
.ink-proof {
display: grid;
gap: 0.15rem;
border-left: 0.6rem solid;
padding-left: 0.8rem;
}
.ink-proof strong {
font-size: 1.25rem;
letter-spacing: 0.06em;
}
.glyph {
display: inline-grid;
place-items: center;
width: 2rem;
height: 2rem;
border-radius: var(--aster-radius-sm);
color: var(--aster-on-primary-subtle);
background: var(--aster-primary-subtle);
}
.cover-box {
width: min(100%, 18rem);
}
.cover {
display: flex;
min-height: 7.5rem;
height: 100%;
padding: 1rem 1.25rem;
flex-direction: column;
justify-content: space-between;
gap: 1.6rem;
color: #fff;
background: repeating-linear-gradient(150deg, transparent 0 8px, #fff2 8px 9px), #204a48;
}
.cover strong {
font:
italic 2.45rem/1 Georgia,
serif;
}
.cover span {
font: 0.78rem var(--aster-font-mono);
}
.cover.grove {
background-color: #3d4b27;
}
.cover.dune {
background-color: #7c4428;
}
button:not([class]),
.entry {
font: inherit;
font-size: 0.875rem;
padding: 0.45rem 0.8rem;
border: 1px solid var(--aster-border-strong);
border-radius: var(--aster-radius-control);
background: var(--aster-surface);
color: var(--aster-on-surface);
}
button:not([class]) {
cursor: pointer;
font-weight: 600;
}
button:not([class]):hover:enabled {
background: var(--aster-primary-subtle);
}
button:disabled {
cursor: default;
}
:focus-visible {
outline: 2px solid var(--aster-primary);
outline-offset: 3px;
}
output:not(:empty) {
display: block;
color: var(--aster-on-primary-subtle);
font-weight: 600;
font-size: 0.9rem;
}
Site-owned component source and scoped styles
src/ui/line-chart
import './theme.css';
import { scope, type Ctx } from '@fierst/aster';
const lcScope = scope`
:scope { display: grid; gap: 0.5rem; font-family: var(--aster-font-sans); min-width: 0; }
.lc-meta { display: flex; justify-content: space-between; font-size: 0.8rem; color: var(--aster-on-surface-muted); }
.lc-val { font-family: var(--aster-font-mono); font-weight: 600; color: var(--aster-on-surface); }
svg { width: 100%; height: auto; display: block; overflow: visible; }
.grid-line { stroke: var(--aster-border); stroke-dasharray: 3 3; }
.line-path { fill: none; stroke: var(--aster-primary); stroke-width: 2.25; stroke-linecap: round; stroke-linejoin: round; }
.area-path { fill: var(--aster-primary); opacity: 0.12; }
.pt { fill: var(--aster-surface); stroke: var(--aster-primary); stroke-width: 2; cursor: pointer; }
.pt.is-active { fill: var(--aster-primary); }
.axis-lbl { fill: var(--aster-on-surface-subtle); font-size: 11px; }
`;
export function* LineChart(
props: {
data: readonly { label: string; value: number }[];
area?: boolean;
height?: number;
unit?: string;
label?: string;
class?: string;
},
ctx: Ctx,
) {
let hoveredIdx = -1;
for (props of ctx) {
const h = props.height ?? 190;
const w = 420;
const padX = 16;
const padT = 12;
const padB = 24;
const plotW = w - padX * 2;
const plotH = h - padT - padB;
const maxVal = Math.max(...props.data.map((d) => d.value), 1);
const minVal = Math.min(...props.data.map((d) => d.value), 0);
const span = Math.max(1, maxVal - minVal);
const n = Math.max(2, props.data.length);
const pts = props.data.map((d, i) => ({
...d,
x: padX + (i / (n - 1)) * plotW,
y: padT + plotH - ((d.value - minVal) / span) * plotH,
}));
const lineD = pts
.map((p, i) => `${i === 0 ? 'M' : 'L'}${p.x.toFixed(1)},${p.y.toFixed(1)}`)
.join(' ');
const areaD =
pts.length > 0
? `${lineD} L${pts[pts.length - 1].x.toFixed(1)},${(padT + plotH).toFixed(1)} L${pts[0].x.toFixed(1)},${(padT + plotH).toFixed(1)} Z`
: '';
const active = hoveredIdx >= 0 ? pts[hoveredIdx] : pts[pts.length - 1];
yield (
<div class={[lcScope, props.class].filter(Boolean).join(' ')}>
<div class="lc-meta">
<span>{active ? active.label : (props.label ?? 'Trend')}</span>
<span class="lc-val">
{active ? `${active.value.toLocaleString()}${props.unit ?? ''}` : ''}
</span>
</div>
<svg
viewBox={`0 0 ${w} ${h}`}
role="img"
aria-label={props.label ?? 'Line chart'}
on:pointerleave={() => (hoveredIdx = -1)}
>
{[0.25, 0.5, 0.75, 1].map((t) => {
const y = padT + plotH * (1 - t);
return <line key={t} class="grid-line" x1={padX} y1={y} x2={w - padX} y2={y} />;
})}
{props.area && areaD ? <path d={areaD} class="area-path" /> : null}
{lineD ? <path d={lineD} class="line-path" /> : null}
{pts.map((p, idx) => (
<g key={p.label} on:pointerenter={() => (hoveredIdx = idx)}>
<circle
cx={p.x}
cy={p.y}
r={hoveredIdx === idx ? 5 : 3.5}
class="pt"
class:is-active={hoveredIdx === idx}
/>
<text x={p.x} y={h - 6} text-anchor="middle" class="axis-lbl">
{p.label}
</text>
</g>
))}
</svg>
</div>
);
}
}
export function AreaChart(props: {
data: readonly { label: string; value: number }[];
height?: number;
unit?: string;
label?: string;
class?: string;
}) {
return <LineChart {...props} area />;
}
Install
sh
npx @fierst/ui@1.0.2 add line-chartRegistry dependencies: theme.
Exports from ./ui/line-chart: LineChart, AreaChart.
Download the registry source for line-chart 1.0.2.
API contracts
ts
export function* LineChart(
props: {
data: readonly { label: string; value: number }[];
area?: boolean;
height?: number;
unit?: string;
label?: string;
class?: string;
},
ctx: Ctx,
)
export function AreaChart(props: {
data: readonly { label: string; value: number }[];
height?: number;
unit?: string;
label?: string;
class?: string;
})