Collection
Mobility / EV Charging · v0.2.3Validated
Compare daily charging uptime, usage and revenue across a network of ports.
Charging Network Trends · Preview
Loading widget…
Preparing source code…
Idle
Mobility / EV Charging · v0.2.3Validated
Compare daily charging uptime, usage and revenue across a network of ports.
Loading widget…
Preparing source code…
Import @lumo/widgets/widget-utilities.css in your Tailwind stylesheet, alongside the Lumo theme.
import "@lumo/tokens/tokens.css";
import { NetworkTrendWidget } from "@lumo/widgets/catalog/ev-charging/network-trend";
export default function Example() {
return <NetworkTrendWidget />;
}Props for integrating the React component in your app.
snapshotChargingNetworkSnapshot | null (optional){
"snapshot": {
"network": "Atlantic loop",
"ports": 48,
"uptimeTargetPercent": 97,
"currency": "EUR",
"daily": [
{
"date": "2026-11-28",
"uptimePercent": 96.2,
"utilisationPercent": 24.5,
"revenueMinor": 125000,
"note": "Site communications restored"
}
]
}
}import type { ChargingNetworkDay } from "@lumo/widgets/catalog/ev-charging/network-trend";
type DailyTotals = { date: string; monitoredPortMinutes: number; availablePortMinutes: number; activePortMinutes: number; settledRevenueMinor: readonly number[] | null };
export function toDay(day: DailyTotals): ChargingNetworkDay {
const ratio = (minutes: number) => day.monitoredPortMinutes > 0 ? minutes / day.monitoredPortMinutes * 100 : null;
return { date: day.date, uptimePercent: ratio(day.availablePortMinutes), utilisationPercent: ratio(day.activePortMinutes),
revenueMinor: day.settledRevenueMinor?.reduce((sum, amount) => sum + amount, 0) ?? null };
}"use client";
import { useMemo, useState } from "react";
import { ChartNoAxesCombined } from "lucide-react";
import { Badge, Button, SegmentedControl, WidgetCard, useScrollShadow } from "@lumo/ui";
import { calendarDate, calendarDayDifference } from "../../shared/billing-calendar";
import { WidgetReadingSeries } from "../../shared/WidgetReadingSeries";
import shared from "../../shared/operations-list.module.css";
import styles from "../../shared/reading-report.module.css";
import { networkDemo, networkMetrics } from "./network-trend.constants";
import type { ChargingNetworkSnapshot, Metric } from "./network-trend.types";
import layout from "./network-trend.module.css";
export type { ChargingNetworkDay, ChargingNetworkSnapshot } from "./network-trend.types";
const dateLabel = new Intl.DateTimeFormat("en-GB", { month: "short", day: "numeric", timeZone: "UTC" });
const options = Object.entries(networkMetrics).map(([value, item]) => ({ value, label: item.label }));
const percent = (value: number | null | undefined): number | null =>
typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 100 ? value : null;
const currencyFormat = (currency: string) => {
try {
const format = new Intl.NumberFormat("en", { style: "currency", currency });
const compact = new Intl.NumberFormat("en", { style: "currency", currency, notation: "compact", maximumFractionDigits: 2 });
return { compact, exact: format, unit: 10 ** (format.resolvedOptions().maximumFractionDigits ?? 2) };
}
catch { return null; }
};
export function NetworkTrendWidget({ snapshot = networkDemo }: { index?: number; snapshot?: ChargingNetworkSnapshot | null } = {}) {
const [metric, setMetric] = useState<Metric>("uptime");
const [selected, setSelected] = useState<string>();
const scrollNote = useScrollShadow<HTMLParagraphElement>();
const formatter = useMemo(() => currencyFormat(snapshot?.currency ?? "USD"), [snapshot?.currency]);
const { label, key, color } = networkMetrics[metric];
const target = metric === "uptime" ? percent(snapshot?.uptimeTargetPercent) : null;
const days = useMemo(() => {
const ordered = new Map((snapshot?.daily ?? []).flatMap(day => {
const date = calendarDate(day.date);
return date ? [[day.date, { ...day, parsed: date }] as const] : [];
}));
return [...ordered.values()].sort((a, b) => a.parsed.getTime() - b.parsed.getTime()).slice(-30);
}, [snapshot?.daily]);
const points = useMemo(() => {
const from = days[0]?.parsed;
const to = days.at(-1)?.parsed;
const span = from && to ? calendarDayDifference(to, from) : 0;
return days.map(day => ({
id: day.date,
label: dateLabel.format(day.parsed),
position: span && from ? calendarDayDifference(day.parsed, from) / span : .5,
value: key === "revenueMinor" ? (formatter && day.revenueMinor !== null && Number.isSafeInteger(day.revenueMinor) && day.revenueMinor >= 0
? day.revenueMinor / formatter.unit : null) : percent(day[key]),
}));
}, [days, formatter, key]);
const point = points.find(point => point.id === selected);
const measured = points.filter(point => point.value !== null);
const sum = measured.reduce((total, point) => total + (point.value ?? 0), 0);
const value = point ? point.value : measured.length ? metric === "revenue" ? sum : sum / measured.length : null;
const display = (value: number | null, exact = false) => value === null ? "Unavailable"
: metric === "revenue" ? (exact ? formatter?.exact : formatter?.compact)?.format(value) ?? "Unavailable" : `${value.toFixed(1)}%`;
const below = target === null ? [] : measured.filter(point => point.value !== null && point.value < target);
const nextBelow = below.find(day => !point || day.id > point.id) ?? below[0];
const belowTarget = target !== null && point?.value != null && point.value < target;
const context = point ? point.label : metric === "revenue" ? "Period total" : "Daily average";
const subtitle = snapshot && Number.isSafeInteger(snapshot.ports) && snapshot.ports! > 0 ? `${snapshot.network} · ${snapshot.ports} ports` : snapshot?.network;
const note = point ? days.find(day => day.date === point.id)?.note ?? (point.value === null ? "Reading missing" : label)
: target !== null ? `${below.length} of ${measured.length} reported days below target` : `${measured.length} reported days`;
return <WidgetCard title="Network" label="Network performance" description={subtitle} icon={<ChartNoAxesCombined />} size="medium"
className={`${shared.mediumCard} ${layout.card}`}>
{days.length ? <div className={`${styles.report} ${layout.report}`}>
<SegmentedControl options={options} value={metric} size="sm" fullWidth label="Network metric"
onValueChange={next => { setMetric(next as Metric); setSelected(undefined); }} />
<div className={`${styles.reading} ${layout.reading}`}>
<span>{context}</span>
{target !== null && <Badge size="sm" tone={belowTarget ? "warning" : "neutral"}>{belowTarget ? "Below " : ""}{target}% target</Badge>}
<strong>{display(value)}</strong>
</div>
<WidgetReadingSeries points={points} activeId={point?.id} onSelect={setSelected} color={color} threshold={target ?? undefined}
label={`${label} by day`} describe={sample => `${sample.label}, ${display(sample.value, true)}`} />
<div className={`${styles.foot} ${layout.footer}`}>
<p ref={scrollNote} tabIndex={0} className={layout.note}>{note}</p>
<div className={styles.footActions}>
{nextBelow && <Button type="button" size="xs" variant="ghost" aria-label="Next day below uptime target"
disabled={nextBelow.id === point?.id} onClick={() => setSelected(nextBelow.id)}>Next dip</Button>}
<Button type="button" size="xs" variant="ghost" disabled={!point} onClick={() => setSelected(undefined)}>Summary</Button>
</div>
</div>
</div> : <p className={shared.muted}>{snapshot ? "No network history" : "Network report unavailable"}</p>}
</WidgetCard>;
}
lucide-reactmotionreactreact-dom