Trend Indicator
StableA sparkline that refuses to draw a trend it cannot justify — across an assay change, a unit change, or two points.
Why it exists
A sparkline is a claim that the points are comparable, and three things routinely break that claim while no library checks any of them.
The assay changed: a lab switching immunoassay platforms shifts every ferritin by 20% with no clinical change at all. The units changed, silently, in an interface feed. Or there are simply two points, and a line between two points is not a trend, it is a rhetorical device. The second problem is separate and just as common: a downward line is not automatically good news. A falling PHQ-9 is improvement; a falling eGFR is not. Direction has no valence until somebody supplies one, so valence is a required prop rather than an assumption baked into a colour — a library that guesses gets half of all clinical measures wrong, silently, in green.
- Observation
- Consumes a series. method, device and valueQuantity.unit decide comparability; referenceRange shades the band behind the line.
Playground
Change a prop and watch the component and its code change with it.
| Date | Value | Note |
|---|---|---|
| 2026-03-04T09:00:00Z | ||
| 2026-04-04T09:00:00Z | ||
| 2026-05-04T09:00:00Z | ||
| 2026-06-04T09:00:00Z | ||
| 2026-07-04T09:00:00Z |
- onSelectPoint — nothing yet
import { TrendIndicator } from "@/components/zoblocks/trend-indicator";export default function Example() { return ( <TrendIndicator series={phq9} width={64} height={20} /> );}Examples
The cases worth copying. Open one in StackBlitz to run it with its dependencies installed.
DataobservationPanelobservationCorrectedobservationPotassiumCritical
A falling line is not automatically good news
observationPanelThe same shape, twice. A falling PHQ-9 is improvement; a falling eGFR is not. Valence is a required prop because a library that guesses gets half of all clinical measures wrong, silently, in green.
import { TrendIndicator } from "@/components/zoblocks/trend-indicator";// The same falling shape, twice: improvement for PHQ-9, decline for eGFR.const phq9 = [ { at: "2026-05-02", value: 18 }, { at: "2026-06-06", value: 14 }, { at: "2026-07-04", value: 12 },];const egfr = [ { at: "2026-05-02", value: 92 }, { at: "2026-06-06", value: 88 }, { at: "2026-07-04", value: 86 },];export default function ValenceExample() { return ( <div className="space-y-5"> <div className="flex flex-wrap items-center justify-between gap-3"> <span className="text-sm font-medium">PHQ-9</span> <TrendIndicator series={{ id: "phq9", label: "PHQ-9", valence: "higher-is-worse", significantChange: 5, points: phq9, }} width={120} height={32} /> </div> <div className="flex flex-wrap items-center justify-between gap-3"> <span className="text-sm font-medium">eGFR</span> <TrendIndicator series={{ id: "egfr", label: "eGFR", valence: "higher-is-better", unit: "mL/min", points: egfr, }} width={120} height={32} /> </div> </div> );}Where comparability breaks, the line breaks
observationCorrectedA lab switching immunoassay platforms shifts every ferritin by 20% with no clinical change at all. Two separate paths say "these are not the same series" without a legend; a dashed gap says it only to somebody who already knows the convention.
import { TrendIndicator } from "@/components/zoblocks/trend-indicator";export default function ComparabilityExample() { return ( <div className="space-y-3"> <span className="text-sm font-medium">Ferritin</span> <TrendIndicator width={180} height={36} series={{ id: "ferritin", label: "Ferritin", valence: "higher-is-worse", unit: "ng/mL", points: [ { at: "2026-01-04", value: 180 }, { at: "2026-03-02", value: 176 }, { at: "2026-04-10", value: 172 }, // Everything after this is on a different scale. { at: "2026-05-09", value: 212, breaksComparability: "Switched to Roche Elecsys" }, { at: "2026-07-11", value: 218 }, ], }} /> </div> );}Two points of PHQ-9 is not a change
observationPotassiumCriticalBelow the reliable-change threshold the trend renders flat and neutral, whatever the arithmetic says. That is the clinical rule for the instrument, not a softening of a real move.
import { TrendIndicator } from "@/components/zoblocks/trend-indicator";export default function NoiseExample() { return ( <div className="space-y-3"> <span className="text-sm font-medium">PHQ-9</span> <TrendIndicator width={180} height={36} series={{ id: "phq9", label: "PHQ-9", valence: "higher-is-worse", // The reliable-change index for the PHQ-9. Below it, noise. significantChange: 5, points: [ { at: "2026-05-02", value: 14 }, { at: "2026-06-06", value: 13 }, { at: "2026-07-04", value: 12 }, ], }} /> </div> );}Two points is not a trend
observationPanelIt is a rhetorical device. Below three comparable points the component draws no line and says how many it has — because an absent trend and a trend that did not move are different facts, and drawing the second for the first is the lie it exists to refuse.
import { TrendIndicator } from "@/components/zoblocks/trend-indicator";export default function TooFewExample() { return ( <div className="space-y-3"> <span className="text-sm font-medium">Creatinine</span> {/* Renders "2 results. A trend needs at least 3." — no line. */} <TrendIndicator width={180} height={36} series={{ id: "cr", label: "Creatinine", valence: "higher-is-worse", unit: "µmol/L", points: [ { at: "2026-06-01", value: 88 }, { at: "2026-07-01", value: 104 }, ], }} /> </div> );}Variants
Every variant is the same accessibility contract and the same keyboard model. Pick by what the surface needs.
Default
64 × 20, with the glyph and delta beside the line.
width=64 height=20
Wide
For a detail panel, where the shape is the point rather than the summary.
width=160 height=32
Narrow
Below 40px the line is dropped for the glyph and the delta — never an unreadable line.
width=32 height=20
Installation
npx @zoblocks/cli add trend-indicatorFirst install? Run zoblocks init once to say where your @/ alias points. The source is copied into your repository, along with anything it depends on.
clsxtailwind-mergeUsage
Props are the FHIR resource.
import { TrendIndicator } from "@/components/zoblocks/trend-indicator";import "@/styles/zoblocks-trend.css";<TrendIndicator series={{ id: "phq9", label: "PHQ-9", valence: "higher-is-worse", significantChange: 5, points: [{ at: "2026-05-02", value: 18 }, { at: "2026-06-06", value: 14 }], }}/>Use it when
- In a flowsheet or a results list, beside the latest value rather than instead of it.
- For measurement-based care, with significantChange set to the instrument's reliable-change index — so a two-point PHQ-9 move renders as noise and a six-point move renders as change.
- With referenceRange when the measure has one. The band behind the line is what turns a shape into a judgement.
- At any width. Below 40px it drops the line for the glyph and the delta rather than drawing something unreadable.
Avoid it when
- As the only representation of a series. It is a shape; the values, the dates and the reference range are the rest, and the hidden table exists precisely because the shape is not enough.
- With valence guessed upstream. If nobody knows whether up is good, pass "neutral" and let the colour stay out of it.
- For a series whose points came from different instruments without saying so. The component can only break where the data admits a break.
API
Every prop is typed. Props not listed are forwarded to the root element.
TrendIndicator
5 propsseriesrequiredTrendSeries- Default
— - The points, oldest first. A direction is drawn only when the units and the method match across them.
describedBystring- Default
— - An element id holding the series as text. When absent the component emits its own visually-hidden table. A sparkline with no text alternative is unreadable and unsearchable, and "decorative" is not true of a line somebody is about to act on.
heightnumber- Default
20 - Drawing height in pixels. The sparkline scales to it; the labels do not, so very small heights lose the axis rather than the numbers.
onSelectPoint((index: number, series: TrendSeries) => void)- Default
— - Fired when a point is chosen. Supplying it makes the points interactive; without it the trend is a picture.
widthnumber- Default
64 - Caller's choice. Below 40px the line is dropped for the glyph and delta.
Accessibility
Screen readers and semantics
- A text alternative is emitted, not assumed. Unless the host supplies describedBy, the component renders the whole series as a visually-hidden table. A sparkline with no text behind it is unreadable to a screen reader and invisible to a page search, and "decorative" is not true of a line somebody is about to act on.
- Direction and valence are both spoken. "Falling, improving" for a PHQ-9 and "falling, worsening" for an eGFR. They are different facts, and a component that spoke only one would be unreadable for exactly the readers who most need the alternative.
- The glyph carries direction without the hue. A triangle for rising, an inverted one for falling, a bar for flat — and the sign on the delta says the same thing again. Remove every colour and the direction survives, which matters here more than almost anywhere: hue is the only channel that ever distinguished a good fall from a bad one.
- A break is a gap and a sentence. Segments are separate paths rather than one path with a dashed gap. A dash is a convention a reader has to already know; two lines that do not join are unambiguous, and the reason is in text beside them rather than in a tooltip.
- Noise is neutral, not softened. Below the reliable-change threshold the direction is flat and the colour is neutral, whatever the arithmetic says. That is the clinical rule for a two-point PHQ-9 move, not a visual softening of a real change.
Conformance · WCAG 2.2 AA · 7 of 8 pass
| Criterion | Status | How | Evidence |
|---|---|---|---|
| 1.1.1Non-text content | pass | role="figure" with a composed name, and a visually-hidden data table emitted unless the host supplies describedBy. A test asserts the table carries every point. | trend-indicator.test.tsx |
| 1.4.1Use of colour | pass | Direction is a CSS glyph and a signed delta before it is a hue, and both the direction and the valence reach the accessible name as words. A test asserts a rising-worse and a rising-better series differ in text, not only in colour. | trend-indicator.test.tsx |
| 1.3.1Info and relationships | pass | The hidden alternative is a real table with a caption, column headers and row headers — not a paragraph of comma-separated numbers. | trend-indicator.test.tsx |
| 4.1.2Name, role, value | pass | The svg is aria-hidden and the figure carries the name, so a screen reader gets one statement and one table rather than an unlabelled graphic. | trend-indicator.test.tsx |
| 2.1.1Keyboard | pass | Point selection, when the host wires it, is a native button per row inside the table — reachable by keyboard without the component managing focus. | trend-indicator.test.tsx |
| 1.4.10Reflow | pass | The caller sets the width and the component degrades to glyph and delta below 40px rather than compressing a line into illegibility. | trend-indicator.test.tsx |
| 1.4.11Non-text contrast | pass | The line and the terminus draw from gated status tokens; the reference band is deliberately below the floor because it is a backdrop rather than a signal, and it carries no information the text does not. | contrast.gate |
| 2.2.1Timing adjustable | n/a | No timing and no animation. | — |
Clinical
The workhorse of measurement-based care. It carries the reliable-change threshold for the instrument, so a two-point PHQ-9 move renders as noise and a six-point move renders as change — which is the actual clinical rule rather than a visual flourish.
- Industries
- healthcare, behavioral-health
- Workflows
- assessment, documentation, treatment-planning, care-coordination
- Terminology
- LOINC, FHIR
- Permissions
- observation.read
- Audit
- Emits no AuditEvent
Protected health information
Handles PHI
Renders a person's measurements over time, including the dates. The hidden table carries all of them in text, which is the point and is also the part a screenshot will not show.
Source
Exactly what lands in your repository, read from the published registry so it can never drift from what the CLI installs.
Show sourceHide source237 lines
/** * TrendIndicator — a sparkline that refuses to draw a trend it cannot justify. * * <TrendIndicator series={{ id, label, points, valence: "higher-is-worse" }} /> * * Below three comparable points it draws no line and says why. Where an assay, * a method or a unit changed it breaks the series and states the reason — * a dashed gap is a convention a reader has to already know, and two lines * that do not join are unambiguous. * * `valence` is required. Half of clinical measures improve by falling and half * by rising: a falling PHQ-9 is improvement, a falling eGFR is not. A library * that guesses gets one half wrong silently, and in colour. * * One SVG path per segment, memoised on series identity. No chart library, * no per-point DOM, and it renders the same on a server. * * Styling lives in `styles/zoblocks-trend.css`, installed alongside. */import * as React from "react";import { cn } from "@/lib/utils";import { describeTrend, geometry, readTrend, segment, whyNoTrend, type TrendSeries,} from "@/lib/zoblocks-trend";export { DEFAULT_MIN_POINTS, comparableRun, describeTrend, geometry, readTrend, segment, whyNoTrend, type Direction, type Judgement, type PathGeometry, type Segment, type TrendPoint, type TrendReading, type TrendSeries, type Valence,} from "@/lib/zoblocks-trend";export interface TrendIndicatorProps extends Omit< React.HTMLAttributes<HTMLDivElement>, "children"> { /** * The points, oldest first. A direction is drawn only when the units and the method match * across them. */ series: TrendSeries; /** Caller's choice. Below 40px the line is dropped for the glyph and delta. */ width?: number; /** * Drawing height in pixels. The sparkline scales to it; the labels do not, so very small * heights lose the axis rather than the numbers. */ height?: number; /** * An element id holding the series as text. * * When absent the component emits its own visually-hidden table. A sparkline * with no text alternative is unreadable and unsearchable, and "decorative" * is not true of a line somebody is about to act on. */ describedBy?: string; /** * Fired when a point is chosen. Supplying it makes the points interactive; without it the * trend is a picture. */ onSelectPoint?: (index: number, series: TrendSeries) => void;}/** Below this the line is unreadable and the glyph is more honest. */const MIN_LINE_WIDTH = 40;function TrendIndicatorImpl({ series, width = 64, height = 20, describedBy, onSelectPoint, className, ...rest}: TrendIndicatorProps) { const reading = readTrend(series); const breaks = React.useMemo(() => segment(series).filter((s) => s.breakReason), [series]); const path = React.useMemo(() => geometry(series, width, height), [series, width, height]); const tableId = React.useId(); const label = describeTrend(series); const drawLine = reading !== null && width >= MIN_LINE_WIDTH; return ( <div {...rest} className={cn("zb-trend", className)} data-zb-trend="" data-zb-direction={reading?.direction ?? "none"} data-zb-judgement={reading?.judgement ?? "unknown"} data-zb-noise={reading?.withinNoise ? "" : undefined} role="figure" aria-label={label} aria-describedby={describedBy ?? tableId} > {drawLine ? ( <svg className="zb-trend__chart" viewBox={`0 0 ${width} ${height}`} width={width} height={height} // The figure carries the name and the table carries the data. The // svg itself is neither, so it stays out of the tree entirely. aria-hidden="true" focusable="false" preserveAspectRatio="none" > {path.band ? ( <rect className="zb-trend__band" x={0} y={path.band.y} width={width} height={path.band.height} /> ) : null} {/* One path per comparable segment. Two lines that do not join say "these are not the same series" without a legend; a dashed gap says it only to someone who already knows the convention. */} {path.paths.map((d, index) => ( <path key={index} className="zb-trend__line" d={d} fill="none" /> ))} {path.last ? ( <circle className="zb-trend__terminus" cx={path.last.x} cy={path.last.y} r={1.75} /> ) : null} </svg> ) : null} {/* The glyph carries direction independently of the colour, and it is the only thing left below 40px — never an unreadable line. */} {reading ? ( <span className="zb-trend__readout" aria-hidden="true"> <span className="zb-trend__glyph" data-zb-direction={reading.direction} /> <span className="zb-trend__delta"> {reading.change > 0 ? "+" : ""} {reading.change} {series.unit ? <span className="zb-trend__unit">{series.unit}</span> : null} </span> </span> ) : ( // Not a flat line. An absent trend and a trend that did not move are // different facts, and drawing the second for the first is the lie // this component exists to refuse. <span className="zb-trend__none" aria-hidden="true"> {whyNoTrend(series)} </span> )} {breaks.length ? ( <span className="zb-trend__break" aria-hidden="true"> {breaks[0]?.breakReason} </span> ) : null} {/* The text alternative, emitted unless the host supplied one. A sparkline with no series behind it is unreadable to a screen reader and invisible to a page search, and neither is acceptable for a line somebody is about to act on. */} {describedBy ? null : ( <table id={tableId} className="zb-trend__table"> <caption>{series.label}</caption> <thead> <tr> <th scope="col">Date</th> <th scope="col">Value{series.unit ? ` (${series.unit})` : ""}</th> <th scope="col">Note</th> </tr> </thead> <tbody> {series.points.map((point, index) => ( <tr key={`${point.at}-${index}`}> <th scope="row">{point.at}</th> <td> {onSelectPoint ? ( <button type="button" onClick={() => onSelectPoint(index, series)}> {point.value} </button> ) : ( point.value )} </td> <td>{point.breaksComparability ?? ""}</td> </tr> ))} </tbody> </table> )} </div> );}/** * Memoised on the series identity. * * A flowsheet renders one of these per row and re-renders on every filter * keystroke. Comparing the whole object would defeat that: hosts rebuild * series from a query response, so the reference changes even when nothing * did. */export const TrendIndicator = React.memo(TrendIndicatorImpl, (a, b) => { return ( a.series.id === b.series.id && a.series.points === b.series.points && a.series.valence === b.series.valence && a.width === b.width && a.height === b.height && a.describedBy === b.describedBy && a.onSelectPoint === b.onSelectPoint && a.className === b.className );});TrendIndicator.displayName = "TrendIndicator";