[data-reveal]{opacity:1!important;transform:none!important}
AntelopeJS DMS
DocumentationKPI card

KPI card

One headline figure with its variation versus the comparison period and an optional sparkline.

KpiCard headlines one metric followed over a period: a large formatted value, a colored up / down delta badge with its comparison caption, and an optional inline sparkline. The stat variant is the tighter DMS look, with a mono uppercase label and a bare icon.

KPI card KpiCard
Connecting to the DMS preview…
Connecting DMS 0.25.8 11 options set

Usage

src/pages/sales.ts ts
import { KpiCard } from "@antelopejs/interface-dms/base/kpi-card";

static revenue = KpiCard({
  title: "$shop.sales.revenue",
  icon: "i-ph-currency-eur",
  fetchUrl: "/api/sales/revenue",
  periodScope: "sales",
  valueFormat: "currency",
  currencyCode: "EUR",
  showDelta: true,
  showSparkline: true,
});

The route answers the value, its variation, the value it is compared against and the sparkline points:

src/pages/sales.ts ts
@Get("revenue")
revenue() {
  return { value: 48200, delta: 12.4, previousValue: 42900, sparkline: [31, 34, 33, 38, 41, 39, 44, 48] };
}

Options

Options of the KPI card block
OptionTypeDefaultDescription
title* Contentstring–Card heading.
description Contentstring–Secondary line under the value.
compareLabel Contentstring–Caption of the variation; follows the period selector's comparison when empty.
icon Appearancestring–The icon shown on the card.
variant Appearance"default" | "stat"–Stat draws the card in a compact style.
valueFormat Appearance"number" | "currency" | "percent" | "compact"–
currencyCode Appearancestring–The currency, as a three-letter code such as EUR, when the value is an amount.
showDelta Appearanceboolean–
showPreviousValue Appearanceboolean–Show the figure the value is compared against.
showSparkline Appearanceboolean–
sparklineAccent Appearancestring–
invert Behaviorboolean–Treat a falling value as the good outcome.

valuePrecision is available in code (Value precision).

Comparison

A card bound to a PeriodSelector through periodScope shows the comparison the selector applies:

  • The value compared against. When the route answers previousValue, the card prints it next to the change, formatted as the value is (same valueFormat, unit, currency and precision): +12.4 % vs €42,900. Set showPreviousValue: false for a card that must stay compact. staticPreviousValue does the same on a static card.
  • The caption. With no compareLabel and no description, the card names the comparison the user picked: "vs previous period", "vs previous year", or "vs comparison period" for a custom window. An explicit compareLabel is kept as written, whatever is selected, and a description takes the line instead. Either label shows with the change only: when the route answers no delta, the description (or an empty line of the same height) stands in.
  • No comparison. When the user picks "No comparison", or the period has no window to compare against (all), the change, the value compared against and the comparison caption are hidden, even if the route still answers them. The trend line goes too: no dash, no empty row; a description stays.

A card with no periodScope shows what its data and options carry: a change only when the data has one, and its compareLabel as written with it. Without a delta the label, which names the period the change is measured against, gives way to the description if there is one, or to an empty line of the same height, so a change arriving moves nothing.

Recipes

A metric where down is good

Set invert: true for errors, churn or latency: a falling value then reads green.

Refresh on events

realtimeTopic makes the card read its route again on each event published there, keeping its figure meanwhile. On a page declared with live, it reads at most once per maxRate, nothing while paused, and once on resume if it missed an event (What Pause freezes).

ts
static requests = KpiCard({ title: "$api.requests", fetchUrl: "/api/stats/requests", periodScope: "api", realtimeTopic: "api:requests" });

A caption from the route

When the route answers a non-empty caption next to value, the card shows it on the footnote line in place of compareLabel and description, while comparing or not. It is shown as written, not read as a translation key, so a route can put a computed figure there: { value: 4, caption: "4 / 8 · 50%" }. A route that answers no caption leaves the line as described above.

Without a comparison period

When the PeriodSelector compares nothing, the route answers no delta: the card shows no variation, and compareLabel ("vs previous period"), which names the period the variation is measured against, gives way to the description if there is one. Their lines keep their height, so toggling a comparison moves nothing.

A static card

Pass staticValue, staticDelta and staticSparkline instead of fetchUrl for a fixture or a demo.

Pitfalls

  • delta is a change in percent (12.5), not a ratio (0.125).
  • For several figures read at a glance, without a period, use a StatGroup: its cards layout draws the same compact card as KpiCard({ variant: "stat" }).

Next steps

Chart card

The same headline with the series behind it.

Period selector

Drive the card's period.