Skip to content

Trend Indicator

Stable

A sparkline that refuses to draw a trend it cannot justify — across an assay change, a unit change, or two points.

13 states5 props15.3 KB installedsince 0.4.0
npx @zoblocks/cli add trend-indicator
View sourceRegistry JSON ↗

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.

Design language
PHQ-9
DateValueNote
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
Props
Events
  • onSelectPoint — nothing yet
Playground.tsx
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

observationPanel

The 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.

Where comparability breaks, the line breaks

observationCorrected

A 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.

Two points of PHQ-9 is not a change

observationPotassiumCritical

Below 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.

Two points is not a trend

observationPanel

It 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.

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

Terminal
npx @zoblocks/cli add trend-indicator

First install? Run zoblocks init once to say where your @/ alias points. The source is copied into your repository, along with anything it depends on.

Dependenciesclsxtailwind-merge

Usage

Props are the FHIR resource.

usage.tsx
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 props
seriesrequired
TrendSeries
Default—
The points, oldest first. A direction is drawn only when the units and the method match across them.
describedBy
string
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.
height
number
Default20
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.
width
number
Default64
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

CriterionStatusHowEvidence
1.1.1Non-text contentpassrole="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 colourpassDirection 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 relationshipspassThe 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, valuepassThe 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.1KeyboardpassPoint 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.10ReflowpassThe 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 contrastpassThe 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 adjustablen/aNo 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 source237 lines
trend-indicator.tsx
/** * 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";