@rtkelly-labs/design-system 0.16.0-dev.0.37dedac → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/components/AlertList.js +246 -0
- package/dist/components/AlertList.js.map +1 -0
- package/dist/components/FilterBar.js +215 -0
- package/dist/components/FilterBar.js.map +1 -0
- package/dist/components/FreshnessIndicator.js +50 -0
- package/dist/components/FreshnessIndicator.js.map +1 -0
- package/dist/components/IdChip.js +58 -0
- package/dist/components/IdChip.js.map +1 -0
- package/dist/components/IncidentTimeline.js +122 -0
- package/dist/components/IncidentTimeline.js.map +1 -0
- package/dist/components/LatencyHeatmap.js +382 -0
- package/dist/components/LatencyHeatmap.js.map +1 -0
- package/dist/components/LatencyHistogram.js +282 -0
- package/dist/components/LatencyHistogram.js.map +1 -0
- package/dist/components/LogViewer.js +393 -0
- package/dist/components/LogViewer.js.map +1 -0
- package/dist/components/ServiceMap.js +404 -0
- package/dist/components/ServiceMap.js.map +1 -0
- package/dist/components/SeverityBadge.js +15 -0
- package/dist/components/SeverityBadge.js.map +1 -0
- package/dist/components/TimeRange.js +89 -0
- package/dist/components/TimeRange.js.map +1 -0
- package/dist/components/TimeSeriesChart.js +507 -0
- package/dist/components/TimeSeriesChart.js.map +1 -0
- package/dist/components/TraceViewer.js +159 -0
- package/dist/components/TraceViewer.js.map +1 -0
- package/dist/components/TraceWaterfall.js +226 -0
- package/dist/components/TraceWaterfall.js.map +1 -0
- package/dist/components/saas/ObservabilityConsole.js +220 -0
- package/dist/components/saas/ObservabilityConsole.js.map +1 -0
- package/dist/components/saas/observabilityData.js +250 -0
- package/dist/components/saas/observabilityData.js.map +1 -0
- package/dist/hooks/useMeasuredWidth.js +22 -0
- package/dist/hooks/useMeasuredWidth.js.map +1 -0
- package/dist/hooks/useTraceClock.js +17 -0
- package/dist/hooks/useTraceClock.js.map +1 -0
- package/dist/hooks/useTraceReplay.js +75 -0
- package/dist/hooks/useTraceReplay.js.map +1 -0
- package/dist/index.d.ts +1562 -1
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/filters.js +130 -0
- package/dist/lib/filters.js.map +1 -0
- package/dist/lib/ids.js +9 -0
- package/dist/lib/ids.js.map +1 -0
- package/dist/lib/latency.js +228 -0
- package/dist/lib/latency.js.map +1 -0
- package/dist/lib/logs.js +78 -0
- package/dist/lib/logs.js.map +1 -0
- package/dist/lib/serviceGraph.js +243 -0
- package/dist/lib/serviceGraph.js.map +1 -0
- package/dist/lib/severity.js +23 -0
- package/dist/lib/severity.js.map +1 -0
- package/dist/lib/timeRange.js +94 -0
- package/dist/lib/timeRange.js.map +1 -0
- package/dist/lib/timeSeries.js +285 -0
- package/dist/lib/timeSeries.js.map +1 -0
- package/dist/lib/trace.js +251 -0
- package/dist/lib/trace.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/TimeSeriesChart.tsx
|
|
4
|
+
import { scaleLinear, scaleLog, scaleTime } from "@visx/scale";
|
|
5
|
+
import {
|
|
6
|
+
forwardRef,
|
|
7
|
+
useCallback,
|
|
8
|
+
useId,
|
|
9
|
+
useMemo,
|
|
10
|
+
useRef,
|
|
11
|
+
useState
|
|
12
|
+
} from "react";
|
|
13
|
+
import { useMeasuredWidth } from "../hooks/useMeasuredWidth.js";
|
|
14
|
+
import { cn, recipe } from "../lib/recipe.js";
|
|
15
|
+
import { formatTimeTick, formatTimestamp, timeTicks } from "../lib/timeRange.js";
|
|
16
|
+
import {
|
|
17
|
+
areaPath,
|
|
18
|
+
cleanPoints,
|
|
19
|
+
clipPoints,
|
|
20
|
+
fittedDomain,
|
|
21
|
+
labelRows,
|
|
22
|
+
formatMetric,
|
|
23
|
+
linePath,
|
|
24
|
+
logDomain,
|
|
25
|
+
nearestIndex,
|
|
26
|
+
nearestTimeIndex,
|
|
27
|
+
nudgeLabels,
|
|
28
|
+
pointsInRange,
|
|
29
|
+
resolveTimeDomain,
|
|
30
|
+
splitRuns,
|
|
31
|
+
stackSeries,
|
|
32
|
+
summarisePoints,
|
|
33
|
+
unionTimes,
|
|
34
|
+
valueDomain,
|
|
35
|
+
withUnit
|
|
36
|
+
} from "../lib/timeSeries.js";
|
|
37
|
+
import { ChartTooltip } from "./ChartTooltip.js";
|
|
38
|
+
import { useTimeCursor, useTimeRange } from "./TimeRange.js";
|
|
39
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
40
|
+
var TIME_SERIES_VARIANTS = {
|
|
41
|
+
variant: ["line", "area", "stacked"],
|
|
42
|
+
curve: ["linear", "monotone", "step"],
|
|
43
|
+
points: ["none", "hover", "all"],
|
|
44
|
+
grid: ["none", "horizontal", "both"],
|
|
45
|
+
yScale: ["linear", "log"],
|
|
46
|
+
yDomain: ["zero", "data"],
|
|
47
|
+
legend: ["top", "bottom", "none", "inline"],
|
|
48
|
+
colorStrategy: ["categorical", "emphasis"],
|
|
49
|
+
density: ["regular", "compact"]
|
|
50
|
+
};
|
|
51
|
+
var FALLBACK_WIDTH = 640;
|
|
52
|
+
var MIN_BRUSH = 4;
|
|
53
|
+
var TOOLTIP_WIDTH = 196;
|
|
54
|
+
var SERIES_TONES = [
|
|
55
|
+
{ stroke: "stroke-accent-primary", fill: "fill-accent-primary", swatch: "bg-accent-primary" },
|
|
56
|
+
{ stroke: "stroke-accent-secondary", fill: "fill-accent-secondary", swatch: "bg-accent-secondary" },
|
|
57
|
+
{ stroke: "stroke-content-secondary", fill: "fill-content-secondary", swatch: "bg-content-secondary" }
|
|
58
|
+
];
|
|
59
|
+
var SERIES_DASHES = [void 0, "6 3", "2 3", "8 3 2 3"];
|
|
60
|
+
var NAMED_TONES = {
|
|
61
|
+
primary: SERIES_TONES[0],
|
|
62
|
+
secondary: SERIES_TONES[1],
|
|
63
|
+
neutral: SERIES_TONES[2],
|
|
64
|
+
info: { stroke: "stroke-intent-info", fill: "fill-intent-info", swatch: "bg-intent-info" },
|
|
65
|
+
success: { stroke: "stroke-intent-success", fill: "fill-intent-success", swatch: "bg-intent-success" },
|
|
66
|
+
warning: { stroke: "stroke-intent-warning", fill: "fill-intent-warning", swatch: "bg-intent-warning" },
|
|
67
|
+
danger: { stroke: "stroke-intent-danger", fill: "fill-intent-danger", swatch: "bg-intent-danger" }
|
|
68
|
+
};
|
|
69
|
+
function Swatch({ tone, className }) {
|
|
70
|
+
return /* @__PURE__ */ jsx("svg", { "aria-hidden": "true", width: 16, height: 8, className: cn("shrink-0 overflow-visible", className), children: /* @__PURE__ */ jsx("line", { x1: 0, x2: 16, y1: 4, y2: 4, className: tone.stroke, strokeWidth: 3, strokeDasharray: tone.dash }) });
|
|
71
|
+
}
|
|
72
|
+
var MUTED_TONE = { stroke: "stroke-content-muted", fill: "fill-content-muted", swatch: "bg-content-muted" };
|
|
73
|
+
var MARK_TONES = {
|
|
74
|
+
info: { stroke: "stroke-intent-info", fill: "fill-intent-info" },
|
|
75
|
+
warning: { stroke: "stroke-intent-warning", fill: "fill-intent-warning" },
|
|
76
|
+
danger: { stroke: "stroke-intent-danger", fill: "fill-intent-danger" }
|
|
77
|
+
};
|
|
78
|
+
var timeSeriesChart = recipe({
|
|
79
|
+
slots: {
|
|
80
|
+
root: "flex min-w-0 flex-col gap-2 font-mono text-xs text-content-primary",
|
|
81
|
+
title: "text-xs font-bold uppercase tracking-wider text-content-secondary",
|
|
82
|
+
frame: "w-full min-w-0",
|
|
83
|
+
plot: "relative block w-full touch-pan-y select-none",
|
|
84
|
+
svg: "block max-w-full overflow-visible",
|
|
85
|
+
tick: "fill-content-muted font-mono text-[10px] tabular-nums",
|
|
86
|
+
mark: "font-mono text-[10px] font-bold",
|
|
87
|
+
legend: "flex flex-wrap gap-1.5",
|
|
88
|
+
toggle: "inline-flex cursor-pointer items-center gap-1.5 border border-edge-default px-2 py-0.5 text-content-primary transition-colors hover:bg-surface-raised motion-reduce:transition-none aria-[pressed=false]:text-content-muted aria-[pressed=false]:line-through",
|
|
89
|
+
readout: "flex items-center justify-between gap-3 tabular-nums"
|
|
90
|
+
},
|
|
91
|
+
variants: {
|
|
92
|
+
zoomable: { true: { plot: "cursor-crosshair" } },
|
|
93
|
+
compact: {
|
|
94
|
+
true: {
|
|
95
|
+
root: "gap-1 text-[10px]",
|
|
96
|
+
title: "text-[10px]",
|
|
97
|
+
tick: "text-[9px]",
|
|
98
|
+
mark: "text-[9px]",
|
|
99
|
+
legend: "gap-1",
|
|
100
|
+
toggle: "gap-1 px-1.5 py-0"
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
function useControllable(value, initial, onChange) {
|
|
106
|
+
const [owned, setOwned] = useState(initial);
|
|
107
|
+
const current = value === void 0 ? owned : value;
|
|
108
|
+
const set = useCallback((next) => {
|
|
109
|
+
if (value === void 0) setOwned(next);
|
|
110
|
+
onChange?.(next);
|
|
111
|
+
}, [value, onChange]);
|
|
112
|
+
return [current, set];
|
|
113
|
+
}
|
|
114
|
+
function plotMargin(compact, showAxes, inline, annotated) {
|
|
115
|
+
const margin = showAxes ? compact ? { top: 10, right: 8, bottom: 18, left: 36 } : { top: 14, right: 16, bottom: 26, left: 52 } : compact ? { top: 4, right: 4, bottom: 4, left: 4 } : { top: 8, right: 8, bottom: 8, left: 8 };
|
|
116
|
+
if (inline) margin.right += compact ? 56 : 84;
|
|
117
|
+
if (annotated) margin.top += compact ? 8 : 10;
|
|
118
|
+
return margin;
|
|
119
|
+
}
|
|
120
|
+
var NO_THRESHOLDS = [];
|
|
121
|
+
var NO_ANNOTATIONS = [];
|
|
122
|
+
var isDecade = (tick) => Math.abs(Math.log10(tick) - Math.round(Math.log10(tick))) < 1e-9;
|
|
123
|
+
var TimeSeriesChart = forwardRef(function TimeSeriesChart2({
|
|
124
|
+
series,
|
|
125
|
+
variant = "line",
|
|
126
|
+
curve = "linear",
|
|
127
|
+
strokeWidth = 1.5,
|
|
128
|
+
points: pointMarkers = "hover",
|
|
129
|
+
fillOpacity,
|
|
130
|
+
grid = "horizontal",
|
|
131
|
+
yScale: yScaleType = "linear",
|
|
132
|
+
yDomain = "zero",
|
|
133
|
+
legend = "bottom",
|
|
134
|
+
colorStrategy = "categorical",
|
|
135
|
+
emphasisSeriesId,
|
|
136
|
+
density = "regular",
|
|
137
|
+
annotations = NO_ANNOTATIONS,
|
|
138
|
+
showAxes = true,
|
|
139
|
+
thresholds = NO_THRESHOLDS,
|
|
140
|
+
range: rangeProp,
|
|
141
|
+
formatValue = formatMetric,
|
|
142
|
+
unit,
|
|
143
|
+
hiddenSeries: hiddenProp,
|
|
144
|
+
defaultHiddenSeries = [],
|
|
145
|
+
onHiddenSeriesChange,
|
|
146
|
+
onRangeSelect,
|
|
147
|
+
zoomable = true,
|
|
148
|
+
height = 220,
|
|
149
|
+
title,
|
|
150
|
+
showTitle = true,
|
|
151
|
+
className,
|
|
152
|
+
...props
|
|
153
|
+
}, ref) {
|
|
154
|
+
const shared = useTimeRange();
|
|
155
|
+
const sharedCursor = useTimeCursor();
|
|
156
|
+
const [localCursor, setLocalCursor] = useState(null);
|
|
157
|
+
const cursor = sharedCursor ? sharedCursor.cursor : localCursor;
|
|
158
|
+
const setCursor = sharedCursor ? sharedCursor.setCursor : setLocalCursor;
|
|
159
|
+
const [hiddenList, setHiddenList] = useControllable(hiddenProp, defaultHiddenSeries, onHiddenSeriesChange);
|
|
160
|
+
const hidden = useMemo(() => new Set(hiddenList), [hiddenList]);
|
|
161
|
+
const compact = density === "compact";
|
|
162
|
+
const log = yScaleType === "log";
|
|
163
|
+
const inline = legend === "inline";
|
|
164
|
+
const plottable = useCallback((value) => value !== null && (!log || value > 0), [log]);
|
|
165
|
+
const uid = useId().replace(/[^a-zA-Z0-9_-]/g, "");
|
|
166
|
+
const clipId = `tsc-clip-${uid}`;
|
|
167
|
+
const summaryId = `tsc-summary-${uid}`;
|
|
168
|
+
const emphasised = emphasisSeriesId ?? series[0]?.id;
|
|
169
|
+
const all = useMemo(
|
|
170
|
+
() => series.map((entry, index) => {
|
|
171
|
+
const cycled = { ...SERIES_TONES[index % SERIES_TONES.length], dash: SERIES_DASHES[Math.floor(index / SERIES_TONES.length) % SERIES_DASHES.length] };
|
|
172
|
+
const tone = colorStrategy === "emphasis" ? entry.id === emphasised ? SERIES_TONES[0] : MUTED_TONE : entry.tone ? NAMED_TONES[entry.tone] : cycled;
|
|
173
|
+
return { ...entry, points: cleanPoints(entry.points), tone };
|
|
174
|
+
}),
|
|
175
|
+
[series, colorStrategy, emphasised]
|
|
176
|
+
);
|
|
177
|
+
const domain = useMemo(() => resolveTimeDomain(rangeProp, shared?.range, all), [rangeProp, shared?.range, all]);
|
|
178
|
+
const visible = useMemo(() => {
|
|
179
|
+
if (!domain) return [];
|
|
180
|
+
return all.filter((entry) => !hidden.has(entry.id)).map((entry) => ({ ...entry, inRange: pointsInRange(entry.points, domain), drawn: clipPoints(entry.points, domain) }));
|
|
181
|
+
}, [all, hidden, domain]);
|
|
182
|
+
const hasData = visible.some((entry) => entry.inRange.some(([, value]) => plottable(value)));
|
|
183
|
+
const annotationsInDomain = useMemo(
|
|
184
|
+
() => domain ? annotations.filter((note) => note.time >= domain.from && note.time <= domain.to) : [],
|
|
185
|
+
[annotations, domain]
|
|
186
|
+
);
|
|
187
|
+
const margin = plotMargin(compact, showAxes, inline, annotationsInDomain.length > 0);
|
|
188
|
+
const [parentRef, width] = useMeasuredWidth(FALLBACK_WIDTH);
|
|
189
|
+
const innerWidth = Math.max(1, width - margin.left - margin.right);
|
|
190
|
+
const innerHeight = Math.max(1, height - margin.top - margin.bottom);
|
|
191
|
+
const stacked = useMemo(() => variant === "stacked" ? stackSeries(visible.map((entry) => entry.drawn)) : null, [variant, visible]);
|
|
192
|
+
const xScale = useMemo(
|
|
193
|
+
() => scaleTime({ domain: domain ? [domain.from, domain.to] : [0, 1], range: [0, innerWidth] }),
|
|
194
|
+
[domain, innerWidth]
|
|
195
|
+
);
|
|
196
|
+
const y = useMemo(() => {
|
|
197
|
+
const values = [];
|
|
198
|
+
if (stacked && domain) {
|
|
199
|
+
stacked.layers.forEach((layer) => layer.forEach((point) => {
|
|
200
|
+
if (point.value !== null && point.time >= domain.from && point.time <= domain.to) values.push(point.y0, point.y1);
|
|
201
|
+
}));
|
|
202
|
+
} else {
|
|
203
|
+
visible.forEach((entry) => entry.inRange.forEach(([, value]) => {
|
|
204
|
+
if (value !== null) values.push(value);
|
|
205
|
+
}));
|
|
206
|
+
}
|
|
207
|
+
const levels = thresholds.map((threshold) => threshold.value);
|
|
208
|
+
const explicit = Array.isArray(yDomain) && Number.isFinite(yDomain[0]) && Number.isFinite(yDomain[1]) && yDomain[0] !== yDomain[1] ? [Math.min(yDomain[0], yDomain[1]), Math.max(yDomain[0], yDomain[1])] : null;
|
|
209
|
+
const nTicks = Math.max(2, Math.floor(innerHeight / (compact ? 30 : 40)));
|
|
210
|
+
if (log) {
|
|
211
|
+
const positive = explicit && explicit[0] > 0 ? explicit : null;
|
|
212
|
+
const scale2 = scaleLog({ domain: positive ?? logDomain(values, levels), range: [innerHeight, 0], nice: !positive });
|
|
213
|
+
const ticksAll = scale2.ticks(nTicks);
|
|
214
|
+
const ticks = ticksAll.length > nTicks + 2 ? ticksAll.filter(isDecade) : ticksAll;
|
|
215
|
+
return {
|
|
216
|
+
// Zero and below sit on the plot's floor, where a log axis begins.
|
|
217
|
+
pos: (value) => value <= 0 ? innerHeight : scale2(value),
|
|
218
|
+
ticks,
|
|
219
|
+
baseline: innerHeight
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
const scale = scaleLinear({
|
|
223
|
+
domain: explicit ?? (yDomain === "data" ? fittedDomain(values) : valueDomain(values, levels)),
|
|
224
|
+
range: [innerHeight, 0],
|
|
225
|
+
nice: !explicit
|
|
226
|
+
});
|
|
227
|
+
const [low, high] = scale.domain();
|
|
228
|
+
return { pos: (value) => scale(value), ticks: scale.ticks(nTicks), baseline: scale(Math.max(low, Math.min(high, 0))) };
|
|
229
|
+
}, [stacked, visible, domain, thresholds, innerHeight, log, yDomain, compact]);
|
|
230
|
+
const xTicks = domain ? timeTicks(domain, Math.max(2, Math.floor(innerWidth / (compact ? 90 : 110)))) : [];
|
|
231
|
+
const span = domain ? domain.to - domain.from : 0;
|
|
232
|
+
const dangerLevel = thresholds.filter((threshold) => threshold.intent === "danger").reduce((lowest, threshold) => Math.min(lowest, threshold.value), Infinity);
|
|
233
|
+
const geometry = useMemo(() => visible.map((entry, index) => {
|
|
234
|
+
if (stacked) {
|
|
235
|
+
const layer = stacked.layers[index];
|
|
236
|
+
const defined = (point) => point.value !== null && (!log || point.y1 > 0);
|
|
237
|
+
const runs2 = splitRuns(layer, defined);
|
|
238
|
+
const bands2 = runs2.map((run) => run.map((point) => ({ x: xScale(point.time), y0: y.pos(point.y0), y1: y.pos(point.y1) })));
|
|
239
|
+
const lines2 = bands2.map((run) => run.map((band) => ({ x: band.x, y: band.y1 })));
|
|
240
|
+
const marks2 = layer.filter((point) => defined(point) && domain !== null && point.time >= domain.from && point.time <= domain.to).map((point) => ({ time: point.time, value: point.value, x: xScale(point.time), y: y.pos(point.y1) }));
|
|
241
|
+
return { entry, bands: bands2, lines: lines2, marks: marks2 };
|
|
242
|
+
}
|
|
243
|
+
const runs = splitRuns([...entry.drawn], ([, value]) => plottable(value));
|
|
244
|
+
const lines = runs.map((run) => run.map(([time, value]) => ({ x: xScale(time), y: y.pos(value) })));
|
|
245
|
+
const bands = variant === "area" ? lines.map((run) => run.map((point) => ({ x: point.x, y0: y.baseline, y1: point.y }))) : [];
|
|
246
|
+
const marks = entry.inRange.filter(([, value]) => plottable(value)).map(([time, value]) => ({ time, value, x: xScale(time), y: y.pos(value) }));
|
|
247
|
+
return { entry, bands, lines, marks };
|
|
248
|
+
}), [visible, stacked, xScale, y, variant, log, domain, plottable]);
|
|
249
|
+
const drawOrder = colorStrategy === "emphasis" ? [...geometry.filter((g) => g.entry.id !== emphasised), ...geometry.filter((g) => g.entry.id === emphasised)] : geometry;
|
|
250
|
+
const stepTimes = useMemo(() => unionTimes(visible.map((entry) => entry.inRange)), [visible]);
|
|
251
|
+
const cursorInDomain = cursor !== null && domain !== null && cursor >= domain.from && cursor <= domain.to && hasData;
|
|
252
|
+
const readout = useMemo(() => {
|
|
253
|
+
if (!cursorInDomain || cursor === null) return null;
|
|
254
|
+
if (stacked) {
|
|
255
|
+
const at2 = stepTimes[nearestTimeIndex(stepTimes, cursor)];
|
|
256
|
+
if (at2 === void 0) return null;
|
|
257
|
+
const position = stacked.times.indexOf(at2);
|
|
258
|
+
return {
|
|
259
|
+
time: at2,
|
|
260
|
+
rows: visible.map((entry, index) => {
|
|
261
|
+
const point = stacked.layers[index][position];
|
|
262
|
+
return { entry, time: at2, value: point?.value ?? null, y: point && point.value !== null ? point.y1 : null };
|
|
263
|
+
})
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
let at = null;
|
|
267
|
+
for (const entry of visible) {
|
|
268
|
+
const index = nearestIndex(entry.inRange, cursor);
|
|
269
|
+
const time = index >= 0 ? entry.inRange[index][0] : null;
|
|
270
|
+
if (time !== null && (at === null || Math.abs(time - cursor) < Math.abs(at - cursor))) at = time;
|
|
271
|
+
}
|
|
272
|
+
if (at === null) return { time: cursor, rows: visible.map((entry) => ({ entry, time: null, value: null, y: null })) };
|
|
273
|
+
const rows = visible.map((entry) => {
|
|
274
|
+
const point = entry.inRange.find(([time]) => time === at);
|
|
275
|
+
return { entry, time: point ? at : null, value: point?.[1] ?? null, y: point?.[1] ?? null };
|
|
276
|
+
});
|
|
277
|
+
return { time: at, rows };
|
|
278
|
+
}, [cursorInDomain, cursor, stacked, stepTimes, visible]);
|
|
279
|
+
const describeValue = (value, entryUnit) => value === null ? "no data" : withUnit(formatValue(value), entryUnit ?? unit);
|
|
280
|
+
const readoutText = readout ? `${formatTimestamp(readout.time)} UTC. ${readout.rows.map((row) => `${row.entry.label} ${describeValue(row.value, row.entry.unit)}`).join(", ")}.` : "";
|
|
281
|
+
const summary = useMemo(() => {
|
|
282
|
+
if (!hasData) return "";
|
|
283
|
+
const parts = visible.map((entry) => {
|
|
284
|
+
const stats = summarisePoints(entry.inRange);
|
|
285
|
+
if (!stats) return `${entry.label}: no data.`;
|
|
286
|
+
const entryUnit = entry.unit ?? unit;
|
|
287
|
+
const breach = Number.isFinite(dangerLevel) && stats.max > dangerLevel ? " Crosses the danger threshold." : "";
|
|
288
|
+
return `${entry.label}: min ${withUnit(formatValue(stats.min), entryUnit)}, max ${withUnit(formatValue(stats.max), entryUnit)}, latest ${withUnit(formatValue(stats.latest), entryUnit)}.${breach}`;
|
|
289
|
+
});
|
|
290
|
+
const lines = thresholds.map((threshold) => `${threshold.intent === "danger" ? "Danger" : "Warning"} threshold ${threshold.label} at ${withUnit(formatValue(threshold.value), unit)}.`);
|
|
291
|
+
const notes = annotationsInDomain.map((note) => `${note.label} at ${formatTimestamp(note.time)} UTC.`);
|
|
292
|
+
return [...parts, ...lines, ...notes].join(" ");
|
|
293
|
+
}, [hasData, visible, thresholds, annotationsInDomain, dangerLevel, formatValue, unit]);
|
|
294
|
+
const svgRef = useRef(null);
|
|
295
|
+
const [drag, setDrag] = useState(null);
|
|
296
|
+
const plotX = (clientX) => {
|
|
297
|
+
const rect = svgRef.current?.getBoundingClientRect();
|
|
298
|
+
const scale = rect && rect.width > 0 ? width / rect.width : 1;
|
|
299
|
+
const x = (clientX - (rect?.left ?? 0)) * scale - margin.left;
|
|
300
|
+
return Math.max(0, Math.min(innerWidth, x));
|
|
301
|
+
};
|
|
302
|
+
const onPointerMove = (event) => {
|
|
303
|
+
if (!domain) return;
|
|
304
|
+
const x = plotX(event.clientX);
|
|
305
|
+
if (drag) setDrag({ start: drag.start, end: x });
|
|
306
|
+
setCursor(xScale.invert(x).getTime());
|
|
307
|
+
};
|
|
308
|
+
const onPointerDown = (event) => {
|
|
309
|
+
if (!zoomable || !domain || event.button !== 0) return;
|
|
310
|
+
const x = plotX(event.clientX);
|
|
311
|
+
setDrag({ start: x, end: x });
|
|
312
|
+
if (typeof event.currentTarget.setPointerCapture === "function") {
|
|
313
|
+
try {
|
|
314
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
315
|
+
} catch {
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
const onPointerUp = (event) => {
|
|
320
|
+
if (!drag) return;
|
|
321
|
+
const end = plotX(event.clientX);
|
|
322
|
+
setDrag(null);
|
|
323
|
+
const left = Math.min(drag.start, end);
|
|
324
|
+
const right = Math.max(drag.start, end);
|
|
325
|
+
if (right - left < MIN_BRUSH) return;
|
|
326
|
+
const selected = { from: xScale.invert(left).getTime(), to: xScale.invert(right).getTime() };
|
|
327
|
+
shared?.setRange(selected);
|
|
328
|
+
onRangeSelect?.(selected);
|
|
329
|
+
};
|
|
330
|
+
const onPointerLeave = () => {
|
|
331
|
+
if (!drag) setCursor(null);
|
|
332
|
+
};
|
|
333
|
+
const onKeyDown = (event) => {
|
|
334
|
+
if (!stepTimes.length) return;
|
|
335
|
+
const last = stepTimes.length - 1;
|
|
336
|
+
const current = cursorInDomain && cursor !== null ? nearestTimeIndex(stepTimes, cursor) : null;
|
|
337
|
+
let next;
|
|
338
|
+
switch (event.key) {
|
|
339
|
+
case "ArrowRight":
|
|
340
|
+
next = current === null ? 0 : Math.min(last, current + 1);
|
|
341
|
+
break;
|
|
342
|
+
case "ArrowLeft":
|
|
343
|
+
next = current === null ? last : Math.max(0, current - 1);
|
|
344
|
+
break;
|
|
345
|
+
case "Home":
|
|
346
|
+
next = 0;
|
|
347
|
+
break;
|
|
348
|
+
case "End":
|
|
349
|
+
next = last;
|
|
350
|
+
break;
|
|
351
|
+
case "Escape":
|
|
352
|
+
if (cursor === null) return;
|
|
353
|
+
setCursor(null);
|
|
354
|
+
event.preventDefault();
|
|
355
|
+
return;
|
|
356
|
+
default:
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
event.preventDefault();
|
|
360
|
+
setCursor(stepTimes[next]);
|
|
361
|
+
};
|
|
362
|
+
const toggleSeries = (id) => {
|
|
363
|
+
setHiddenList(hidden.has(id) ? hiddenList.filter((candidate) => candidate !== id) : [...hiddenList, id]);
|
|
364
|
+
};
|
|
365
|
+
const styles = timeSeriesChart({ zoomable, compact });
|
|
366
|
+
const label = title ?? "Time series chart";
|
|
367
|
+
const areaOpacity = fillOpacity ?? (variant === "stacked" ? 0.35 : 0.2);
|
|
368
|
+
const markerRadius = compact ? 1.75 : 2.25;
|
|
369
|
+
const cursorX = cursorInDomain && cursor !== null ? xScale(cursor) : null;
|
|
370
|
+
const tooltipHeight = readout ? 34 + readout.rows.length * 17 : 0;
|
|
371
|
+
const tooltipX = cursorX === null ? 0 : cursorX + 12 + TOOLTIP_WIDTH > innerWidth ? cursorX - 12 - TOOLTIP_WIDTH : cursorX + 12;
|
|
372
|
+
const brush = drag && Math.abs(drag.end - drag.start) >= 1 ? { x: Math.min(drag.start, drag.end), width: Math.abs(drag.end - drag.start) } : null;
|
|
373
|
+
const breaching = Number.isFinite(dangerLevel) && visible.some((entry) => entry.inRange.some(([, value]) => value !== null && value > dangerLevel));
|
|
374
|
+
const ends = inline ? geometry.filter((g) => g.marks.length > 0).map((g) => ({ entry: g.entry, y: g.marks[g.marks.length - 1].y })) : [];
|
|
375
|
+
const endsPlaced = nudgeLabels(ends.map((end) => end.y), compact ? 10 : 12, 0, innerHeight);
|
|
376
|
+
const inlineLabels = ends.map((end, index) => ({ entry: end.entry, y: endsPlaced[index] }));
|
|
377
|
+
const charWidth = compact ? 5.4 : 6;
|
|
378
|
+
const sortedNotes = [...annotationsInDomain].sort((a, b) => a.time - b.time);
|
|
379
|
+
const noteRows = labelRows(sortedNotes.map((note) => [xScale(note.time), note.label.length * charWidth + 8]));
|
|
380
|
+
const annotationMarks = sortedNotes.map((note, index) => ({ note, x: xScale(note.time), row: noteRows[index] }));
|
|
381
|
+
const legendButtons = series.length && (legend === "top" || legend === "bottom") ? /* @__PURE__ */ jsx("div", { role: "group", "aria-label": `${label} series`, "data-part": "legend", className: styles.legend(), children: all.map((entry) => /* @__PURE__ */ jsxs("button", { type: "button", "aria-pressed": !hidden.has(entry.id), "data-series": entry.id, className: styles.toggle(), onClick: () => toggleSeries(entry.id), children: [
|
|
382
|
+
/* @__PURE__ */ jsx(Swatch, { tone: entry.tone, className: cn(hidden.has(entry.id) && "opacity-30") }),
|
|
383
|
+
entry.label
|
|
384
|
+
] }, entry.id)) }) : null;
|
|
385
|
+
return /* @__PURE__ */ jsxs(
|
|
386
|
+
"div",
|
|
387
|
+
{
|
|
388
|
+
ref,
|
|
389
|
+
"data-slot": "time-series-chart",
|
|
390
|
+
"data-variant": variant,
|
|
391
|
+
"data-density": density,
|
|
392
|
+
"data-breach": breaching || void 0,
|
|
393
|
+
...props,
|
|
394
|
+
className: styles.root({ className }),
|
|
395
|
+
children: [
|
|
396
|
+
title && showTitle ? /* @__PURE__ */ jsx("p", { className: styles.title(), children: title }) : null,
|
|
397
|
+
legend === "top" ? legendButtons : null,
|
|
398
|
+
/* @__PURE__ */ jsx("div", { ref: parentRef, className: styles.frame(), children: !hasData || !domain ? /* @__PURE__ */ jsxs("svg", { role: "img", "aria-label": `${label}: no data in this range.`, width, height, viewBox: `0 0 ${width} ${height}`, className: styles.svg(), children: [
|
|
399
|
+
/* @__PURE__ */ jsx("rect", { x: 0.5, y: 0.5, width: width - 1, height: height - 1, className: "fill-none stroke-edge-subtle", strokeDasharray: "4 3" }),
|
|
400
|
+
/* @__PURE__ */ jsx("text", { "aria-hidden": "true", x: width / 2, y: height / 2, textAnchor: "middle", dominantBaseline: "central", className: "fill-content-muted font-mono text-xs", children: "No data in this range." })
|
|
401
|
+
] }) : /* @__PURE__ */ jsx(
|
|
402
|
+
"div",
|
|
403
|
+
{
|
|
404
|
+
role: "slider",
|
|
405
|
+
"aria-label": label,
|
|
406
|
+
"aria-describedby": summaryId,
|
|
407
|
+
"aria-orientation": "horizontal",
|
|
408
|
+
"aria-valuemin": domain.from,
|
|
409
|
+
"aria-valuemax": domain.to,
|
|
410
|
+
"aria-valuenow": readout ? readout.time : domain.from,
|
|
411
|
+
"aria-valuetext": readout ? readoutText : "No time selected. Use the arrow keys to step through samples.",
|
|
412
|
+
tabIndex: 0,
|
|
413
|
+
"data-part": "plot",
|
|
414
|
+
className: styles.plot(),
|
|
415
|
+
onKeyDown,
|
|
416
|
+
children: /* @__PURE__ */ jsxs(
|
|
417
|
+
"svg",
|
|
418
|
+
{
|
|
419
|
+
ref: svgRef,
|
|
420
|
+
"aria-hidden": "true",
|
|
421
|
+
width,
|
|
422
|
+
height,
|
|
423
|
+
viewBox: `0 0 ${width} ${height}`,
|
|
424
|
+
className: styles.svg(),
|
|
425
|
+
onPointerDown,
|
|
426
|
+
onPointerMove,
|
|
427
|
+
onPointerUp,
|
|
428
|
+
onPointerCancel: () => setDrag(null),
|
|
429
|
+
onPointerLeave,
|
|
430
|
+
children: [
|
|
431
|
+
/* @__PURE__ */ jsx("defs", { children: /* @__PURE__ */ jsx("clipPath", { id: clipId, children: /* @__PURE__ */ jsx("rect", { x: -markerRadius - 1, y: -markerRadius - 1, width: innerWidth + 2 * markerRadius + 2, height: innerHeight + 2 * markerRadius + 2 }) }) }),
|
|
432
|
+
/* @__PURE__ */ jsxs("g", { transform: `translate(${margin.left} ${margin.top})`, children: [
|
|
433
|
+
/* @__PURE__ */ jsx("rect", { x: 0, y: 0, width: innerWidth, height: innerHeight, className: "fill-transparent" }),
|
|
434
|
+
grid !== "none" ? y.ticks.map((tick) => /* @__PURE__ */ jsx("line", { "data-part": "grid-row", x1: 0, x2: innerWidth, y1: y.pos(tick), y2: y.pos(tick), className: "stroke-edge-subtle" }, `gy-${tick}`)) : null,
|
|
435
|
+
grid === "both" ? xTicks.map((tick) => /* @__PURE__ */ jsx("line", { "data-part": "grid-column", x1: xScale(tick), x2: xScale(tick), y1: 0, y2: innerHeight, className: "stroke-edge-subtle" }, `gx-${tick}`)) : null,
|
|
436
|
+
showAxes ? y.ticks.map((tick) => /* @__PURE__ */ jsx("text", { "data-part": "y-tick", x: -6, y: y.pos(tick), textAnchor: "end", dominantBaseline: "central", className: styles.tick(), children: formatValue(tick) }, `y-${tick}`)) : null,
|
|
437
|
+
showAxes && unit && !compact ? /* @__PURE__ */ jsx("text", { "data-part": "unit", x: -6, y: -6, textAnchor: "end", className: styles.tick(), children: unit }) : null,
|
|
438
|
+
thresholds.map((threshold) => {
|
|
439
|
+
const at = y.pos(threshold.value);
|
|
440
|
+
if (threshold.intent !== "danger" || !(at > 0) || at > innerHeight) return null;
|
|
441
|
+
return /* @__PURE__ */ jsx("rect", { "data-part": "danger-band", x: 0, y: 0, width: innerWidth, height: at, className: MARK_TONES.danger.fill, fillOpacity: 0.08 }, `band-${threshold.label}`);
|
|
442
|
+
}),
|
|
443
|
+
/* @__PURE__ */ jsx("g", { clipPath: `url(#${clipId})`, children: drawOrder.map(({ entry, bands, lines, marks }) => /* @__PURE__ */ jsxs("g", { "data-series": entry.id, children: [
|
|
444
|
+
bands.length ? /* @__PURE__ */ jsx("path", { "data-part": "area", d: areaPath(bands, curve), className: entry.tone.fill, fillOpacity: areaOpacity }) : null,
|
|
445
|
+
/* @__PURE__ */ jsx("path", { "data-part": "line", d: linePath(lines, curve), className: cn("fill-none", entry.tone.stroke), strokeDasharray: entry.tone.dash, strokeWidth, strokeLinejoin: "round", strokeLinecap: "round" }),
|
|
446
|
+
pointMarkers === "all" ? marks.map((mark) => /* @__PURE__ */ jsx("circle", { "data-part": "point", cx: mark.x, cy: mark.y, r: markerRadius, className: entry.tone.fill }, mark.time)) : null,
|
|
447
|
+
!stacked && Number.isFinite(dangerLevel) ? marks.map((mark) => mark.value > dangerLevel ? /* @__PURE__ */ jsx("circle", { "data-part": "breach", cx: mark.x, cy: mark.y, r: markerRadius + 0.5, className: "fill-intent-danger" }, `b-${mark.time}`) : null) : null
|
|
448
|
+
] }, entry.id)) }),
|
|
449
|
+
thresholds.map((threshold) => {
|
|
450
|
+
const at = y.pos(threshold.value);
|
|
451
|
+
if (!(at >= 0) || at > innerHeight) return null;
|
|
452
|
+
const tone = MARK_TONES[threshold.intent];
|
|
453
|
+
return /* @__PURE__ */ jsxs("g", { "data-part": "threshold", "data-intent": threshold.intent, children: [
|
|
454
|
+
/* @__PURE__ */ jsx("line", { x1: 0, x2: innerWidth, y1: at, y2: at, className: tone.stroke, strokeDasharray: "5 4", strokeWidth: 1.25 }),
|
|
455
|
+
/* @__PURE__ */ jsxs("text", { x: innerWidth - 4, y: at - 4, textAnchor: "end", className: cn(styles.mark(), tone.fill), children: [
|
|
456
|
+
threshold.label,
|
|
457
|
+
" \xB7 ",
|
|
458
|
+
withUnit(formatValue(threshold.value), unit)
|
|
459
|
+
] })
|
|
460
|
+
] }, `threshold-${threshold.label}`);
|
|
461
|
+
}),
|
|
462
|
+
annotationMarks.map(({ note, x, row }) => {
|
|
463
|
+
const tone = MARK_TONES[note.intent ?? "info"];
|
|
464
|
+
return /* @__PURE__ */ jsxs("g", { "data-part": "annotation", "data-intent": note.intent ?? "info", children: [
|
|
465
|
+
/* @__PURE__ */ jsx("line", { x1: x, x2: x, y1: row === 0 ? -(compact ? 8 : 10) : 0, y2: innerHeight, className: tone.stroke, strokeDasharray: "2 3" }),
|
|
466
|
+
/* @__PURE__ */ jsx("text", { x: x + 3, y: row === 0 ? -2 : compact ? 9 : 11, className: cn(styles.mark(), tone.fill), children: note.label })
|
|
467
|
+
] }, `note-${note.time}-${note.label}`);
|
|
468
|
+
}),
|
|
469
|
+
showAxes ? /* @__PURE__ */ jsxs("g", { "data-part": "axes", children: [
|
|
470
|
+
/* @__PURE__ */ jsx("line", { x1: 0, x2: 0, y1: 0, y2: innerHeight, className: "stroke-edge-strong" }),
|
|
471
|
+
/* @__PURE__ */ jsx("line", { x1: 0, x2: innerWidth, y1: innerHeight, y2: innerHeight, className: "stroke-edge-strong" }),
|
|
472
|
+
xTicks.map((tick) => /* @__PURE__ */ jsxs("g", { "data-part": "x-tick", transform: `translate(${xScale(tick)} ${innerHeight})`, children: [
|
|
473
|
+
/* @__PURE__ */ jsx("line", { y1: 0, y2: 4, className: "stroke-edge-strong" }),
|
|
474
|
+
/* @__PURE__ */ jsx("text", { y: compact ? 13 : 16, textAnchor: "middle", className: styles.tick(), children: formatTimeTick(tick, span) })
|
|
475
|
+
] }, `x-${tick}`))
|
|
476
|
+
] }) : null,
|
|
477
|
+
inlineLabels.map(({ entry, y: at }) => /* @__PURE__ */ jsx("text", { "data-part": "inline-label", "data-series": entry.id, x: innerWidth + 6, y: at, dominantBaseline: "central", className: cn(styles.mark(), entry.tone.fill), children: entry.label }, `label-${entry.id}`)),
|
|
478
|
+
brush ? /* @__PURE__ */ jsx("rect", { "data-part": "brush", x: brush.x, y: 0, width: brush.width, height: innerHeight, className: "fill-accent-primary stroke-accent-primary", fillOpacity: 0.15 }) : null,
|
|
479
|
+
cursorX !== null ? /* @__PURE__ */ jsxs("g", { "data-part": "cursor", children: [
|
|
480
|
+
/* @__PURE__ */ jsx("line", { x1: cursorX, x2: cursorX, y1: 0, y2: innerHeight, className: "stroke-content-primary", strokeWidth: 1 }),
|
|
481
|
+
pointMarkers !== "none" ? readout?.rows.map((row) => row.y !== null && row.time !== null && plottable(row.y) ? /* @__PURE__ */ jsx("circle", { "data-part": "cursor-point", cx: xScale(row.time), cy: y.pos(row.y), r: 3, className: cn(row.entry.tone.fill, "stroke-surface-base"), strokeWidth: 1.5 }, row.entry.id) : null) : null
|
|
482
|
+
] }) : null,
|
|
483
|
+
readout && cursorX !== null ? /* @__PURE__ */ jsx("foreignObject", { x: tooltipX, y: 0, width: TOOLTIP_WIDTH, height: tooltipHeight, "data-part": "tooltip", className: "pointer-events-none overflow-visible", children: /* @__PURE__ */ jsx(ChartTooltip, { title: `${formatTimestamp(readout.time)} UTC`, className: "w-full", children: readout.rows.map((row) => /* @__PURE__ */ jsxs("div", { className: styles.readout(), children: [
|
|
484
|
+
/* @__PURE__ */ jsxs("span", { className: "flex min-w-0 items-center gap-1.5", children: [
|
|
485
|
+
/* @__PURE__ */ jsx(Swatch, { tone: row.entry.tone }),
|
|
486
|
+
/* @__PURE__ */ jsx("span", { className: "truncate", children: row.entry.label })
|
|
487
|
+
] }),
|
|
488
|
+
/* @__PURE__ */ jsx("span", { children: describeValue(row.value, row.entry.unit) })
|
|
489
|
+
] }, row.entry.id)) }) }) : null
|
|
490
|
+
] })
|
|
491
|
+
]
|
|
492
|
+
}
|
|
493
|
+
)
|
|
494
|
+
}
|
|
495
|
+
) }),
|
|
496
|
+
legend === "bottom" ? legendButtons : null,
|
|
497
|
+
hasData ? /* @__PURE__ */ jsx("p", { id: summaryId, className: "sr-only", children: summary }) : null
|
|
498
|
+
]
|
|
499
|
+
}
|
|
500
|
+
);
|
|
501
|
+
});
|
|
502
|
+
TimeSeriesChart.displayName = "TimeSeriesChart";
|
|
503
|
+
export {
|
|
504
|
+
TIME_SERIES_VARIANTS,
|
|
505
|
+
TimeSeriesChart
|
|
506
|
+
};
|
|
507
|
+
//# sourceMappingURL=TimeSeriesChart.js.map
|