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.
KpiCardUsage
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:
@Get("revenue")
revenue() {
return { value: 48200, delta: 12.4, previousValue: 42900, sparkline: [31, 34, 33, 38, 41, 39, 44, 48] };
}
Options
| Option | Type | Default | Description |
|---|---|---|---|
title* Content | string | – | Card heading. |
description Content | string | – | Secondary line under the value. |
compareLabel Content | string | – | Caption of the variation; follows the period selector's comparison when empty. |
icon Appearance | string | – | 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 Appearance | string | – | The currency, as a three-letter code such as EUR, when the value is an amount. |
showDelta Appearance | boolean | – | |
showPreviousValue Appearance | boolean | – | Show the figure the value is compared against. |
showSparkline Appearance | boolean | – | |
sparklineAccent Appearance | string | – | |
invert Behavior | boolean | – | 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 (samevalueFormat, unit, currency and precision):+12.4 % vs €42,900. SetshowPreviousValue: falsefor a card that must stay compact.staticPreviousValuedoes the same on a static card. - The caption. With no
compareLabeland nodescription, the card names the comparison the user picked: "vs previous period", "vs previous year", or "vs comparison period" for a custom window. An explicitcompareLabelis kept as written, whatever is selected, and adescriptiontakes the line instead. Either label shows with the change only: when the route answers nodelta, thedescription(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; adescriptionstays.
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).
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
deltais a change in percent (12.5), not a ratio (0.125).- For several figures read at a glance, without a period, use a
StatGroup: itscardslayout draws the same compact card asKpiCard({ variant: "stat" }).
