@schemavaults/ui 0.108.0 → 0.109.1
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/dist/components/hooks/index.d.ts +2 -0
- package/dist/components/hooks/index.js +1 -0
- package/dist/components/hooks/index.js.map +1 -1
- package/dist/components/hooks/use-measured-width.d.ts +33 -0
- package/dist/components/hooks/use-measured-width.js +61 -0
- package/dist/components/hooks/use-measured-width.js.map +1 -0
- package/dist/components/ui/bar-chart/bar-chart.d.ts +71 -13
- package/dist/components/ui/bar-chart/bar-chart.js +263 -96
- package/dist/components/ui/bar-chart/bar-chart.js.map +1 -1
- package/dist/components/ui/bar-list/bar-list.d.ts +63 -0
- package/dist/components/ui/bar-list/bar-list.js +39 -0
- package/dist/components/ui/bar-list/bar-list.js.map +1 -0
- package/dist/components/ui/bar-list/index.d.ts +2 -0
- package/dist/components/ui/bar-list/index.js +2 -0
- package/dist/components/ui/bar-list/index.js.map +1 -0
- package/dist/components/ui/chart-primitives/chart-colors.d.ts +69 -0
- package/dist/components/ui/chart-primitives/chart-colors.js +201 -0
- package/dist/components/ui/chart-primitives/chart-colors.js.map +1 -0
- package/dist/components/ui/chart-primitives/chart-data-table.d.ts +20 -0
- package/dist/components/ui/chart-primitives/chart-data-table.js +11 -0
- package/dist/components/ui/chart-primitives/chart-data-table.js.map +1 -0
- package/dist/components/ui/chart-primitives/chart-format.d.ts +12 -0
- package/dist/components/ui/chart-primitives/chart-format.js +40 -0
- package/dist/components/ui/chart-primitives/chart-format.js.map +1 -0
- package/dist/components/ui/chart-primitives/chart-legend.d.ts +20 -0
- package/dist/components/ui/chart-primitives/chart-legend.js +22 -0
- package/dist/components/ui/chart-primitives/chart-legend.js.map +1 -0
- package/dist/components/ui/chart-primitives/chart-scale.d.ts +59 -0
- package/dist/components/ui/chart-primitives/chart-scale.js +178 -0
- package/dist/components/ui/chart-primitives/chart-scale.js.map +1 -0
- package/dist/components/ui/chart-primitives/chart-tooltip.d.ts +67 -0
- package/dist/components/ui/chart-primitives/chart-tooltip.js +57 -0
- package/dist/components/ui/chart-primitives/chart-tooltip.js.map +1 -0
- package/dist/components/ui/chart-primitives/index.d.ts +12 -0
- package/dist/components/ui/chart-primitives/index.js +7 -0
- package/dist/components/ui/chart-primitives/index.js.map +1 -0
- package/dist/components/ui/chart-primitives/responsive-chart.d.ts +26 -0
- package/dist/components/ui/chart-primitives/responsive-chart.js +25 -0
- package/dist/components/ui/chart-primitives/responsive-chart.js.map +1 -0
- package/dist/components/ui/chart-primitives/use-chart-width.d.ts +30 -0
- package/dist/components/ui/chart-primitives/use-chart-width.js +56 -0
- package/dist/components/ui/chart-primitives/use-chart-width.js.map +1 -0
- package/dist/components/ui/histogram/bin-values.d.ts +53 -0
- package/dist/components/ui/histogram/bin-values.js +137 -0
- package/dist/components/ui/histogram/bin-values.js.map +1 -0
- package/dist/components/ui/histogram/histogram.d.ts +76 -0
- package/dist/components/ui/histogram/histogram.js +149 -0
- package/dist/components/ui/histogram/histogram.js.map +1 -0
- package/dist/components/ui/histogram/index.d.ts +4 -0
- package/dist/components/ui/histogram/index.js +3 -0
- package/dist/components/ui/histogram/index.js.map +1 -0
- package/dist/components/ui/index.d.ts +6 -0
- package/dist/components/ui/index.js +3 -0
- package/dist/components/ui/index.js.map +1 -1
- package/dist/components/ui/line-chart/line-chart.d.ts +104 -20
- package/dist/components/ui/line-chart/line-chart.js +290 -151
- package/dist/components/ui/line-chart/line-chart.js.map +1 -1
- package/dist/components/ui/pie-chart/pie-chart.d.ts +35 -7
- package/dist/components/ui/pie-chart/pie-chart.js +200 -88
- package/dist/components/ui/pie-chart/pie-chart.js.map +1 -1
- package/dist/components/ui/scatter-plot/scatter-plot.d.ts +123 -21
- package/dist/components/ui/scatter-plot/scatter-plot.js +507 -243
- package/dist/components/ui/scatter-plot/scatter-plot.js.map +1 -1
- package/dist/components/ui/sparkline/sparkline.d.ts +15 -5
- package/dist/components/ui/sparkline/sparkline.js +21 -37
- package/dist/components/ui/sparkline/sparkline.js.map +1 -1
- package/package.json +3 -2
|
@@ -1,23 +1,31 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
3
|
import { cva } from "class-variance-authority";
|
|
4
|
-
import { useId } from "react";
|
|
4
|
+
import { useId, useState } from "react";
|
|
5
5
|
import { cn } from "../../../lib/utils";
|
|
6
|
+
import { chartColorIds, chartPaintClass, createChartColorScale, getChartSeriesColorId, } from "../../ui/chart-primitives/chart-colors";
|
|
7
|
+
import { ChartDataTable } from "../../ui/chart-primitives/chart-data-table";
|
|
8
|
+
import { formatChartNumber } from "../../ui/chart-primitives/chart-format";
|
|
9
|
+
import { ChartLegend } from "../../ui/chart-primitives/chart-legend";
|
|
10
|
+
import { nearestSortedIndex, niceTicksWithin, thinLabels, } from "../../ui/chart-primitives/chart-scale";
|
|
11
|
+
import { ChartLiveRegion, ChartTooltip, ChartTooltipRow, } from "../../ui/chart-primitives/chart-tooltip";
|
|
12
|
+
import { useChartWidth, } from "../../ui/chart-primitives/use-chart-width";
|
|
6
13
|
export const lineChartSizeIds = [
|
|
7
14
|
"sm",
|
|
8
15
|
"md",
|
|
9
16
|
"lg",
|
|
10
17
|
"xl",
|
|
11
18
|
];
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"primary",
|
|
15
|
-
"positive",
|
|
16
|
-
"warning",
|
|
17
|
-
"destructive",
|
|
18
|
-
"muted",
|
|
19
|
-
];
|
|
19
|
+
/** Preset series colours; see `chartColorIds`. */
|
|
20
|
+
export const lineChartColorIds = chartColorIds;
|
|
20
21
|
export const lineChartCurveIds = ["linear", "smooth"];
|
|
22
|
+
/** Tick-label font size per size preset, for thinning crowded labels. */
|
|
23
|
+
const SIZE_TO_FONT_PX = {
|
|
24
|
+
sm: 10,
|
|
25
|
+
md: 11,
|
|
26
|
+
lg: 12,
|
|
27
|
+
xl: 14,
|
|
28
|
+
};
|
|
21
29
|
/** Default canvas dimensions per size. Either axis can be overridden via the
|
|
22
30
|
* `width` / `height` props. */
|
|
23
31
|
const SIZE_TO_CANVAS = {
|
|
@@ -26,42 +34,6 @@ const SIZE_TO_CANVAS = {
|
|
|
26
34
|
lg: { width: 480, height: 260 },
|
|
27
35
|
xl: { width: 640, height: 340 },
|
|
28
36
|
};
|
|
29
|
-
const STROKE_CLASSES = {
|
|
30
|
-
default: "stroke-schemavaults-brand-blue",
|
|
31
|
-
primary: "stroke-primary",
|
|
32
|
-
positive: "stroke-emerald-500 dark:stroke-emerald-400",
|
|
33
|
-
warning: "stroke-warning",
|
|
34
|
-
destructive: "stroke-destructive",
|
|
35
|
-
muted: "stroke-muted-foreground",
|
|
36
|
-
};
|
|
37
|
-
const FILL_CLASSES = {
|
|
38
|
-
default: "fill-schemavaults-brand-blue",
|
|
39
|
-
primary: "fill-primary",
|
|
40
|
-
positive: "fill-emerald-500 dark:fill-emerald-400",
|
|
41
|
-
warning: "fill-warning",
|
|
42
|
-
destructive: "fill-destructive",
|
|
43
|
-
muted: "fill-muted-foreground",
|
|
44
|
-
};
|
|
45
|
-
const TEXT_CLASSES = {
|
|
46
|
-
default: "text-schemavaults-brand-blue",
|
|
47
|
-
primary: "text-primary",
|
|
48
|
-
positive: "text-emerald-500 dark:text-emerald-400",
|
|
49
|
-
warning: "text-warning",
|
|
50
|
-
destructive: "text-destructive",
|
|
51
|
-
muted: "text-muted-foreground",
|
|
52
|
-
};
|
|
53
|
-
/**
|
|
54
|
-
* Fallback color rotation when a series doesn't specify its own color. Index
|
|
55
|
-
* is the series position in the input array, modulo the palette length.
|
|
56
|
-
*/
|
|
57
|
-
const DEFAULT_COLOR_ROTATION = [
|
|
58
|
-
"default",
|
|
59
|
-
"positive",
|
|
60
|
-
"warning",
|
|
61
|
-
"destructive",
|
|
62
|
-
"primary",
|
|
63
|
-
"muted",
|
|
64
|
-
];
|
|
65
37
|
export const lineChartVariants = cva("relative inline-flex shrink-0 items-center justify-center", {
|
|
66
38
|
variants: {
|
|
67
39
|
size: {
|
|
@@ -143,9 +115,10 @@ function buildSmoothPath(points) {
|
|
|
143
115
|
}
|
|
144
116
|
return d;
|
|
145
117
|
}
|
|
146
|
-
function
|
|
147
|
-
|
|
148
|
-
|
|
118
|
+
function buildCurvePath(points, curve) {
|
|
119
|
+
return curve === "smooth" ? buildSmoothPath(points) : buildLinePath(points);
|
|
120
|
+
}
|
|
121
|
+
function buildAreaPath(points, curve, baselineY) {
|
|
149
122
|
// Close each sub-path of the line back down to the baseline.
|
|
150
123
|
const segments = [];
|
|
151
124
|
let current = [];
|
|
@@ -161,34 +134,35 @@ function buildAreaPath(linePath, points, baselineY) {
|
|
|
161
134
|
}
|
|
162
135
|
if (current.length > 0)
|
|
163
136
|
segments.push(current);
|
|
164
|
-
//
|
|
165
|
-
//
|
|
137
|
+
// Re-trace each segment with the same curve as the line, so the fill's top
|
|
138
|
+
// edge sits exactly under the stroke, then close it along the baseline.
|
|
166
139
|
let d = "";
|
|
167
140
|
for (const seg of segments) {
|
|
168
|
-
if (seg.length === 0)
|
|
169
|
-
continue;
|
|
170
141
|
const first = seg[0];
|
|
171
142
|
const last = seg[seg.length - 1];
|
|
172
|
-
|
|
173
|
-
const subPoints = seg;
|
|
174
|
-
const subLine = subPoints.length > 1
|
|
175
|
-
? buildLinePath(subPoints)
|
|
176
|
-
: `M ${first.x.toFixed(2)} ${first.y.toFixed(2)}`;
|
|
143
|
+
const subLine = buildCurvePath(seg, curve);
|
|
177
144
|
d += `${d ? " " : ""}${subLine} L ${last.x.toFixed(2)} ${baselineY.toFixed(2)} L ${first.x.toFixed(2)} ${baselineY.toFixed(2)} Z`;
|
|
178
145
|
}
|
|
179
146
|
return d;
|
|
180
147
|
}
|
|
181
|
-
function LineChart({ series, label, size, width, height, yMin, yMax, xMin, xMax, curve = "linear", showPoints = false, showAxis = true, gridLineCount = 0, categories, xTickFormatter, xTickCount = 5, yTickFormatter, yTickCount = 5, showYAxisLabels
|
|
148
|
+
function LineChart({ series, label, size, width, height, seriesOrder, yMin, yMax, xMin, xMax, curve = "linear", showPoints = false, showAxis = true, gridLineCount = 0, categories, formatX, formatY = formatChartNumber, formatTooltip, showTooltip = true, showLegend, xTickFormatter, xTickCount = 5, xTickValues, showXAxisLabels, yTickFormatter, yTickCount = 5, niceTicks, showYAxisLabels, yTickLabelWidth = 36, showValueLabels = false, valueLabelFormatter, loading = false, onPointClick, children, className, style, overlayClassName, valueLabelClassName, categoryLabelClassName, yAxisLabelClassName, axisClassName, ref, ...props }) {
|
|
182
149
|
const resolvedSize = size ?? "md";
|
|
183
150
|
const canvas = SIZE_TO_CANVAS[resolvedSize];
|
|
184
|
-
const
|
|
151
|
+
const chart = useChartWidth(width, canvas.width, ref);
|
|
152
|
+
const W = chart.width;
|
|
185
153
|
const H = height ?? canvas.height;
|
|
186
|
-
const titleId = useId();
|
|
187
154
|
const gradientIdPrefix = useId();
|
|
155
|
+
const hintId = useId();
|
|
156
|
+
/** The x the crosshair sits on (kept as a value, so it survives a data refresh). */
|
|
157
|
+
const [activeXValue, setActiveXValue] = useState(null);
|
|
158
|
+
/** Whether the crosshair was moved with the arrow keys (so it is announced). */
|
|
159
|
+
const [announce, setAnnounce] = useState(false);
|
|
188
160
|
// Reserve gutters for axis labels.
|
|
189
161
|
const hasCategoryLabels = (categories !== undefined && categories.length > 0) ||
|
|
190
|
-
typeof xTickFormatter === "function"
|
|
191
|
-
|
|
162
|
+
typeof xTickFormatter === "function" ||
|
|
163
|
+
(showXAxisLabels ?? typeof formatX === "function");
|
|
164
|
+
const hasYAxisLabels = typeof yTickFormatter === "function" ||
|
|
165
|
+
(showYAxisLabels ?? formatY !== formatChartNumber);
|
|
192
166
|
const categoryGutter = hasCategoryLabels ? 22 : 6;
|
|
193
167
|
const valueGutter = showValueLabels ? 18 : 6;
|
|
194
168
|
const padTop = valueGutter;
|
|
@@ -197,7 +171,7 @@ function LineChart({ series, label, size, width, height, yMin, yMax, xMin, xMax,
|
|
|
197
171
|
const padRight = 6;
|
|
198
172
|
const plotX0 = padLeft;
|
|
199
173
|
const plotY0 = padTop;
|
|
200
|
-
const plotW = Math.max(0, W - padLeft - padRight);
|
|
174
|
+
const plotW = Math.max(0, (W ?? 0) - padLeft - padRight);
|
|
201
175
|
const plotH = Math.max(0, H - padTop - padBottom);
|
|
202
176
|
// Determine the domain across all series.
|
|
203
177
|
let domainXMin = Number.POSITIVE_INFINITY;
|
|
@@ -246,16 +220,20 @@ function LineChart({ series, label, size, width, height, yMin, yMax, xMin, xMax,
|
|
|
246
220
|
const ySpan = effectiveYMax > effectiveYMin ? effectiveYMax - effectiveYMin : 1;
|
|
247
221
|
const projectX = (x) => plotX0 + ((x - effectiveXMin) / xSpan) * plotW;
|
|
248
222
|
const projectY = (y) => plotY0 + plotH - ((y - effectiveYMin) / ySpan) * plotH;
|
|
223
|
+
const colorScale = seriesOrder
|
|
224
|
+
? createChartColorScale(seriesOrder)
|
|
225
|
+
: null;
|
|
249
226
|
const resolved = series.map((s, seriesIndex) => {
|
|
250
|
-
const
|
|
251
|
-
|
|
227
|
+
const colorId = s.color ??
|
|
228
|
+
(colorScale ? colorScale(s.id) : getChartSeriesColorId(seriesIndex));
|
|
252
229
|
const seriesCurve = s.curve ?? curve;
|
|
253
230
|
const seriesShowPoints = s.showPoints ?? showPoints;
|
|
231
|
+
const byX = new Map();
|
|
254
232
|
const points = s.points.map((p, idx) => {
|
|
255
233
|
const domainX = isFiniteNumber(p.x) ? p.x : idx;
|
|
256
234
|
const domainY = isFiniteNumber(p.y) ? p.y : 0;
|
|
257
235
|
const isGap = !isFiniteNumber(p.y);
|
|
258
|
-
|
|
236
|
+
const rp = {
|
|
259
237
|
point: p,
|
|
260
238
|
domainX,
|
|
261
239
|
domainY,
|
|
@@ -264,15 +242,33 @@ function LineChart({ series, label, size, width, height, yMin, yMax, xMin, xMax,
|
|
|
264
242
|
isGap,
|
|
265
243
|
index: idx,
|
|
266
244
|
};
|
|
245
|
+
if (!isGap && !byX.has(domainX))
|
|
246
|
+
byX.set(domainX, rp);
|
|
247
|
+
return rp;
|
|
267
248
|
});
|
|
268
249
|
return {
|
|
269
250
|
series: s,
|
|
270
251
|
points,
|
|
271
|
-
|
|
252
|
+
byX,
|
|
253
|
+
paint: { colorId, value: s.stroke },
|
|
272
254
|
curve: seriesCurve,
|
|
273
255
|
showPoints: seriesShowPoints,
|
|
274
256
|
};
|
|
275
257
|
});
|
|
258
|
+
// Every x at which some series has a value, ascending: the crosshair's stops.
|
|
259
|
+
const xValues = Array.from(new Set(resolved.flatMap((rs) => Array.from(rs.byX.keys()))))
|
|
260
|
+
.filter((x) => x >= effectiveXMin && x <= effectiveXMax)
|
|
261
|
+
.sort((a, b) => a - b);
|
|
262
|
+
const activeIndex = activeXValue === null ? -1 : xValues.indexOf(activeXValue);
|
|
263
|
+
/** Index into `xValues` the crosshair sits on; null when its x is gone. */
|
|
264
|
+
const active = activeIndex >= 0 ? activeIndex : null;
|
|
265
|
+
const setActive = (index) => {
|
|
266
|
+
setActiveXValue(index === null ? null : (xValues[index] ?? null));
|
|
267
|
+
};
|
|
268
|
+
// Points without an explicit x are plotted by index: ticks must be whole.
|
|
269
|
+
const isIndexMode = series.every((s) => s.points.every((p) => !isFiniteNumber(p.x)));
|
|
270
|
+
const niceXTicks = niceTicks ?? typeof xTickFormatter !== "function";
|
|
271
|
+
const niceYTicks = niceTicks ?? typeof yTickFormatter !== "function";
|
|
276
272
|
const baselineYClamped = effectiveYMin <= 0 && effectiveYMax >= 0
|
|
277
273
|
? projectY(0)
|
|
278
274
|
: projectY(effectiveYMin);
|
|
@@ -285,9 +281,26 @@ function LineChart({ series, label, size, width, height, yMin, yMax, xMin, xMax,
|
|
|
285
281
|
}
|
|
286
282
|
return lines;
|
|
287
283
|
})();
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
284
|
+
const isCategorical = categories !== undefined && categories.length > 0;
|
|
285
|
+
/** How the tooltip heading and the table name an x. */
|
|
286
|
+
const describeX = (x) => {
|
|
287
|
+
if (isCategorical && Number.isInteger(x)) {
|
|
288
|
+
const category = categories[x];
|
|
289
|
+
if (category)
|
|
290
|
+
return category;
|
|
291
|
+
}
|
|
292
|
+
if (formatX)
|
|
293
|
+
return formatX(x);
|
|
294
|
+
for (const rs of resolved) {
|
|
295
|
+
const pointLabel = rs.byX.get(x)?.point.label;
|
|
296
|
+
if (pointLabel)
|
|
297
|
+
return pointLabel;
|
|
298
|
+
}
|
|
299
|
+
return formatChartNumber(x);
|
|
300
|
+
};
|
|
301
|
+
// X-axis tick set, thinned so labels never run together.
|
|
302
|
+
const xTicks = thinLabels((() => {
|
|
303
|
+
if (isCategorical) {
|
|
291
304
|
const ticks = [];
|
|
292
305
|
categories.forEach((text, idx) => {
|
|
293
306
|
if (text === null)
|
|
@@ -299,105 +312,231 @@ function LineChart({ series, label, size, width, height, yMin, yMax, xMin, xMax,
|
|
|
299
312
|
});
|
|
300
313
|
return ticks;
|
|
301
314
|
}
|
|
302
|
-
if (
|
|
315
|
+
if (hasCategoryLabels && hasData && xTickValues) {
|
|
316
|
+
const ticks = [];
|
|
317
|
+
xTickValues.forEach((domainX, i) => {
|
|
318
|
+
if (domainX < effectiveXMin || domainX > effectiveXMax)
|
|
319
|
+
return;
|
|
320
|
+
const text = typeof xTickFormatter === "function"
|
|
321
|
+
? xTickFormatter(domainX, i)
|
|
322
|
+
: formatX
|
|
323
|
+
? formatX(domainX)
|
|
324
|
+
: formatChartNumber(domainX);
|
|
325
|
+
if (typeof text !== "string" || text === "")
|
|
326
|
+
return;
|
|
327
|
+
ticks.push({ x: projectX(domainX), text });
|
|
328
|
+
});
|
|
329
|
+
return ticks;
|
|
330
|
+
}
|
|
331
|
+
if (hasCategoryLabels && xTickCount > 0 && hasData) {
|
|
303
332
|
const ticks = [];
|
|
304
333
|
const count = Math.max(2, xTickCount);
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
334
|
+
const values = niceXTicks
|
|
335
|
+
? niceTicksWithin(effectiveXMin, effectiveXMax, count - 1, isIndexMode ? 1 : 0)
|
|
336
|
+
: Array.from({ length: count }, (_, i) => effectiveXMin + (i / (count - 1)) * xSpan).filter((x) => !isIndexMode || Number.isInteger(x));
|
|
337
|
+
values.forEach((domainX, i) => {
|
|
338
|
+
const text = typeof xTickFormatter === "function"
|
|
339
|
+
? xTickFormatter(domainX, i)
|
|
340
|
+
: formatX
|
|
341
|
+
? formatX(domainX)
|
|
342
|
+
: formatChartNumber(domainX);
|
|
343
|
+
if (typeof text !== "string" || text === "")
|
|
344
|
+
return;
|
|
311
345
|
ticks.push({ x: projectX(domainX), text });
|
|
312
|
-
}
|
|
346
|
+
});
|
|
313
347
|
return ticks;
|
|
314
348
|
}
|
|
315
349
|
return [];
|
|
316
|
-
})();
|
|
350
|
+
})(), SIZE_TO_FONT_PX[resolvedSize]);
|
|
317
351
|
// Y-axis tick set. Always includes both endpoints when active.
|
|
318
352
|
const yTicks = (() => {
|
|
319
353
|
if (!hasYAxisLabels || !hasData)
|
|
320
354
|
return [];
|
|
321
355
|
const count = Math.max(2, yTickCount);
|
|
356
|
+
// Top-down (the largest value first), so the first call to a custom
|
|
357
|
+
// formatter receives the largest value -- the order most consumers
|
|
358
|
+
// expect.
|
|
359
|
+
const values = niceYTicks
|
|
360
|
+
? niceTicksWithin(effectiveYMin, effectiveYMax, count - 1).reverse()
|
|
361
|
+
: Array.from({ length: count }, (_, i) => effectiveYMax - (i / (count - 1)) * ySpan);
|
|
322
362
|
const ticks = [];
|
|
323
|
-
|
|
324
|
-
// Iterate top-down (i = 0 -> yMax, i = count - 1 -> yMin) so the first
|
|
325
|
-
// call to a custom formatter receives the largest value -- the order
|
|
326
|
-
// most consumers expect.
|
|
327
|
-
const t = i / (count - 1);
|
|
328
|
-
const value = effectiveYMax - t * ySpan;
|
|
363
|
+
values.forEach((value, i) => {
|
|
329
364
|
const text = typeof yTickFormatter === "function"
|
|
330
365
|
? yTickFormatter(value, i)
|
|
331
|
-
:
|
|
332
|
-
if (text
|
|
333
|
-
|
|
366
|
+
: formatY(value);
|
|
367
|
+
if (typeof text !== "string" || text === "")
|
|
368
|
+
return;
|
|
334
369
|
ticks.push({ y: projectY(value), value, text });
|
|
335
|
-
}
|
|
370
|
+
});
|
|
336
371
|
return ticks;
|
|
337
372
|
})();
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
373
|
+
// Gridlines follow the tick labels when they are shown, so every line has
|
|
374
|
+
// its value; otherwise they split the plot evenly.
|
|
375
|
+
const gridLineYs = gridLineCount <= 0
|
|
376
|
+
? []
|
|
377
|
+
: yTicks.length > 0
|
|
378
|
+
? yTicks
|
|
379
|
+
.map((tick) => tick.y)
|
|
380
|
+
.filter((y) => y > plotY0 + 0.5 && y < plotY0 + plotH - 0.5)
|
|
381
|
+
: gridLines.map((t) => plotY0 + plotH - t * plotH);
|
|
382
|
+
const hasInteractivePoints = resolved.some((rs) => rs.showPoints &&
|
|
383
|
+
typeof (rs.series.onPointClick ?? onPointClick) === "function");
|
|
384
|
+
const readoutEnabled = showTooltip && hasData && xValues.length > 0;
|
|
385
|
+
const legendVisible = showLegend ?? series.length > 1;
|
|
386
|
+
const activeX = active === null ? undefined : xValues[active];
|
|
387
|
+
const activeValues = activeX === undefined
|
|
388
|
+
? []
|
|
389
|
+
: resolved.map((rs) => {
|
|
390
|
+
const rp = rs.byX.get(activeX);
|
|
391
|
+
const value = rp ? rp.domainY : null;
|
|
392
|
+
return {
|
|
393
|
+
series: rs.series,
|
|
394
|
+
point: rp?.point,
|
|
395
|
+
value,
|
|
396
|
+
formattedValue: value === null ? "—" : formatY(value),
|
|
397
|
+
paint: rs.paint,
|
|
398
|
+
};
|
|
399
|
+
});
|
|
400
|
+
const onPointerMove = (event) => {
|
|
401
|
+
if (!readoutEnabled || W === null)
|
|
402
|
+
return;
|
|
403
|
+
const bounds = event.currentTarget.getBoundingClientRect();
|
|
404
|
+
const scale = bounds.width > 0 ? W / bounds.width : 1;
|
|
405
|
+
const px = (event.clientX - bounds.left) * scale;
|
|
406
|
+
const domainX = plotW > 0 ? effectiveXMin + ((px - plotX0) / plotW) * xSpan : effectiveXMin;
|
|
407
|
+
const nearest = nearestSortedIndex(xValues, domainX);
|
|
408
|
+
setActive(nearest < 0 ? null : nearest);
|
|
409
|
+
setAnnounce(false);
|
|
410
|
+
};
|
|
411
|
+
const onKeyDown = (event) => {
|
|
412
|
+
if (!readoutEnabled || event.target !== event.currentTarget)
|
|
413
|
+
return;
|
|
414
|
+
const last = xValues.length - 1;
|
|
415
|
+
let next = active;
|
|
416
|
+
if (event.key === "ArrowRight" || event.key === "ArrowUp") {
|
|
417
|
+
next = active === null ? 0 : Math.min(last, active + 1);
|
|
418
|
+
}
|
|
419
|
+
else if (event.key === "ArrowLeft" || event.key === "ArrowDown") {
|
|
420
|
+
next = active === null ? last : Math.max(0, active - 1);
|
|
421
|
+
}
|
|
422
|
+
else if (event.key === "Home") {
|
|
423
|
+
next = 0;
|
|
424
|
+
}
|
|
425
|
+
else if (event.key === "End") {
|
|
426
|
+
next = last;
|
|
427
|
+
}
|
|
428
|
+
else if (event.key === "Escape") {
|
|
429
|
+
next = null;
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
event.preventDefault();
|
|
435
|
+
setActive(next);
|
|
436
|
+
setAnnounce(true);
|
|
437
|
+
};
|
|
438
|
+
const onBlur = (event) => {
|
|
439
|
+
if (!event.currentTarget.contains(event.relatedTarget)) {
|
|
440
|
+
setActive(null);
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
const activeDots = activeX === undefined
|
|
444
|
+
? []
|
|
445
|
+
: resolved.flatMap((rs) => {
|
|
446
|
+
const rp = rs.byX.get(activeX);
|
|
447
|
+
return rp ? [{ rs, rp }] : [];
|
|
448
|
+
});
|
|
449
|
+
const plot = W === null ? null : (_jsxs("svg", { width: W, height: H, viewBox: `0 0 ${W} ${H}`, role: hasInteractivePoints ? "group" : "img", "aria-label": label, "aria-describedby": readoutEnabled ? hintId : undefined, tabIndex: readoutEnabled ? 0 : undefined, onKeyDown: onKeyDown, onBlur: onBlur, onPointerMove: onPointerMove, onPointerLeave: () => setActive(null), className: "block h-full w-full overflow-visible rounded-sm focus:outline-none focus-visible:ring-2 focus-visible:ring-ring", children: [gridLineYs.length > 0 ? (_jsx("g", { "aria-hidden": "true", "data-slot": "line-chart-gridlines", className: "pointer-events-none", children: gridLineYs.map((y) => {
|
|
450
|
+
return (_jsx("line", { x1: plotX0, y1: y, x2: plotX0 + plotW, y2: y, strokeWidth: 1, className: cn("stroke-border/60", axisClassName) }, `grid-${y}`));
|
|
451
|
+
}) })) : null, showAxis ? (_jsxs("g", { "aria-hidden": "true", "data-slot": "line-chart-axis", className: "pointer-events-none", children: [_jsx("line", { x1: plotX0, y1: plotY0, x2: plotX0, y2: plotY0 + plotH, strokeWidth: 1, className: cn("stroke-border", axisClassName) }), _jsx("line", { x1: plotX0, y1: plotY0 + plotH, x2: plotX0 + plotW, y2: plotY0 + plotH, strokeWidth: 1, className: cn("stroke-border", axisClassName) })] })) : null, !hasData ? (_jsx("text", { x: W / 2, y: H / 2, textAnchor: "middle", dominantBaseline: "central", className: "fill-muted-foreground", children: "No data" })) : (resolved.map((rs, seriesIndex) => {
|
|
452
|
+
const strokeWidth = rs.series.strokeWidth ?? 2;
|
|
453
|
+
const linePath = buildCurvePath(rs.points, rs.curve);
|
|
454
|
+
const areaPath = rs.series.area
|
|
455
|
+
? buildAreaPath(rs.points, rs.curve, baselineYClamped)
|
|
456
|
+
: "";
|
|
457
|
+
const gradientId = `${gradientIdPrefix}-grad-${seriesIndex}`;
|
|
458
|
+
const markerRadius = Math.max(4, strokeWidth + 2);
|
|
459
|
+
return (_jsxs("g", { "data-slot": "line-chart-series", "data-series-id": rs.series.id, children: [rs.series.area && areaPath !== "" ? (_jsxs(_Fragment, { children: [_jsx("defs", { children: _jsxs("linearGradient", { id: gradientId, x1: "0", y1: "0", x2: "0", y2: "1", className: chartPaintClass(rs.paint, "text"), children: [_jsx("stop", { offset: "0%", stopColor: rs.series.stroke ?? "currentColor", stopOpacity: "0.16" }), _jsx("stop", { offset: "100%", stopColor: rs.series.stroke ?? "currentColor", stopOpacity: "0.04" })] }) }), _jsx("path", { d: areaPath, fill: `url(#${gradientId})`, stroke: "none", className: "pointer-events-none" })] })) : null, _jsx("path", { d: linePath, fill: "none", stroke: rs.series.stroke, strokeWidth: strokeWidth, strokeLinecap: "round", strokeLinejoin: "round", strokeDasharray: rs.series.strokeDasharray, className: cn("pointer-events-none", chartPaintClass(rs.paint, "stroke"), rs.series.className) }), rs.showPoints
|
|
460
|
+
? rs.points.map((rp) => {
|
|
461
|
+
if (rp.isGap)
|
|
462
|
+
return null;
|
|
463
|
+
const handler = rs.series.onPointClick ?? onPointClick;
|
|
464
|
+
const isInteractive = typeof handler === "function";
|
|
465
|
+
const ariaLabel = `${rs.series.label ?? rs.series.id} at ${describeX(rp.domainX)}: ${formatY(rp.point.y)}`;
|
|
466
|
+
const pointKey = rp.point.id ?? `${rs.series.id}-${rp.index}`;
|
|
467
|
+
const xIndex = xValues.indexOf(rp.domainX);
|
|
468
|
+
return (_jsx("circle", { cx: rp.x, cy: rp.y, r: markerRadius, fill: rs.series.stroke, stroke: "hsl(var(--background))", strokeWidth: 2, "data-point-index": rp.index, role: isInteractive ? "button" : undefined, tabIndex: isInteractive ? 0 : undefined, "aria-label": isInteractive ? ariaLabel : undefined, "aria-hidden": isInteractive ? undefined : "true", onFocus: isInteractive && readoutEnabled && xIndex >= 0
|
|
469
|
+
? () => {
|
|
470
|
+
setActive(xIndex);
|
|
471
|
+
setAnnounce(false);
|
|
472
|
+
}
|
|
473
|
+
: undefined, onClick: isInteractive
|
|
474
|
+
? (event) => {
|
|
475
|
+
handler(rp.point, rs.series, event);
|
|
476
|
+
}
|
|
477
|
+
: undefined, onKeyDown: isInteractive
|
|
478
|
+
? (event) => {
|
|
479
|
+
if (event.key === "Enter" ||
|
|
480
|
+
event.key === " ") {
|
|
481
|
+
event.preventDefault();
|
|
482
|
+
handler(rp.point, rs.series, event);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
: undefined, className: cn("transition-opacity", chartPaintClass(rs.paint, "fill"), isInteractive
|
|
486
|
+
? "cursor-pointer hover:opacity-80 focus:outline-none focus-visible:opacity-80"
|
|
487
|
+
: "pointer-events-none") }, pointKey));
|
|
488
|
+
})
|
|
489
|
+
: null, showValueLabels
|
|
490
|
+
? rs.points.map((rp) => {
|
|
491
|
+
if (rp.isGap)
|
|
492
|
+
return null;
|
|
493
|
+
const text = valueLabelFormatter
|
|
494
|
+
? valueLabelFormatter({
|
|
495
|
+
series: rs.series,
|
|
496
|
+
point: rp.point,
|
|
497
|
+
value: rp.point.y,
|
|
498
|
+
index: rp.index,
|
|
398
499
|
})
|
|
399
|
-
:
|
|
400
|
-
|
|
500
|
+
: formatY(rp.point.y);
|
|
501
|
+
if (text === null || text === "")
|
|
502
|
+
return null;
|
|
503
|
+
const labelKey = (rp.point.id ?? `${rs.series.id}-${rp.index}`) +
|
|
504
|
+
"-val";
|
|
505
|
+
return (_jsx("text", { x: rp.x, y: rp.y - 8, textAnchor: "middle", className: cn("pointer-events-none select-none fill-foreground font-medium tabular-nums", valueLabelClassName), children: text }, labelKey));
|
|
506
|
+
})
|
|
507
|
+
: null] }, rs.series.id));
|
|
508
|
+
})), xTicks.length > 0 ? (_jsx("g", { "aria-hidden": "true", "data-slot": "line-chart-x-ticks", className: "pointer-events-none select-none", children: xTicks.map((tick, idx) => (_jsx("text", { x: tick.x, y: plotY0 + plotH + 14, textAnchor: "middle", className: cn("fill-muted-foreground tabular-nums", categoryLabelClassName), children: tick.text }, `xtick-${idx}-${tick.text}`))) })) : null, yTicks.length > 0 ? (_jsx("g", { "aria-hidden": "true", "data-slot": "line-chart-y-ticks", className: "pointer-events-none select-none", children: yTicks.map((tick, idx) => (_jsxs("g", { children: [_jsx("line", { x1: plotX0 - 3, x2: plotX0, y1: tick.y, y2: tick.y, strokeWidth: 1, className: cn("stroke-border", axisClassName) }), _jsx("text", { x: plotX0 - 6, y: tick.y, textAnchor: "end", dominantBaseline: "central", className: cn("fill-muted-foreground tabular-nums", yAxisLabelClassName), children: tick.text })] }, `ytick-${idx}-${tick.text}`))) })) : null, activeX !== undefined ? (_jsxs("g", { "aria-hidden": "true", "data-slot": "line-chart-crosshair", className: "pointer-events-none", children: [_jsx("line", { x1: projectX(activeX), x2: projectX(activeX), y1: plotY0, y2: plotY0 + plotH, strokeWidth: 1, className: "stroke-muted-foreground/60" }), activeDots.map(({ rs, rp }) => (_jsx("circle", { cx: rp.x, cy: rp.y, r: 4, fill: rs.series.stroke, stroke: "hsl(var(--background))", strokeWidth: 2, className: chartPaintClass(rs.paint, "fill") }, rs.series.id)))] })) : null] }));
|
|
509
|
+
const tooltipY = activeDots.length > 0
|
|
510
|
+
? Math.min(...activeDots.map(({ rp }) => rp.y))
|
|
511
|
+
: plotY0;
|
|
512
|
+
return (_jsxs("div", { ref: chart.ref, "data-slot": "line-chart", "aria-busy": loading || undefined, className: cn(lineChartVariants({ size }), "flex-col items-stretch justify-start gap-2 transition-opacity", chart.rootClassName, loading && "opacity-60", className), style: { ...chart.rootStyle, ...style }, ...props, children: [legendVisible && series.length > 0 && W !== null ? (_jsx(ChartLegend, { items: resolved.map((rs) => ({
|
|
513
|
+
id: rs.series.id,
|
|
514
|
+
label: rs.series.label ?? rs.series.id,
|
|
515
|
+
paint: rs.paint,
|
|
516
|
+
swatch: "line",
|
|
517
|
+
})) })) : null, _jsxs("div", { "data-slot": "line-chart-plot", className: "relative w-full shrink-0", style: { height: H }, children: [plot, readoutEnabled && W !== null ? (_jsxs(_Fragment, { children: [_jsx("span", { id: hintId, hidden: true, children: "Use the left and right arrow keys to read the values at each point." }), _jsx(ChartLiveRegion, { message: announce && activeX !== undefined
|
|
518
|
+
? `${describeX(activeX)}: ${activeValues
|
|
519
|
+
.map((entry) => `${entry.series.label ?? entry.series.id} ${entry.formattedValue}`)
|
|
520
|
+
.join(", ")}`
|
|
521
|
+
: "" })] })) : null, showTooltip && activeX !== undefined && W !== null ? (_jsx(ChartTooltip, { x: projectX(activeX), y: tooltipY, containerWidth: W, children: formatTooltip ? (formatTooltip({
|
|
522
|
+
x: activeX,
|
|
523
|
+
xLabel: describeX(activeX),
|
|
524
|
+
values: activeValues,
|
|
525
|
+
})) : (_jsxs(_Fragment, { children: [_jsx("div", { className: "mb-0.5 text-muted-foreground", children: describeX(activeX) }), activeValues.map((entry) => (_jsx(ChartTooltipRow, { value: entry.formattedValue, label: entry.series.label ?? entry.series.id, color: series.length > 1 ? entry.paint.value : undefined, colorClassName: series.length > 1
|
|
526
|
+
? chartPaintClass(entry.paint, "bg")
|
|
527
|
+
: undefined }, entry.series.id)))] })) })) : null, children ? (_jsx("div", { className: cn("pointer-events-none absolute inset-0 flex items-center justify-center text-center", overlayClassName), children: children })) : null] }), W !== null && hasData ? (_jsx(ChartDataTable, { caption: label, columns: [
|
|
528
|
+
"x",
|
|
529
|
+
...series.map((s) => s.label ?? s.id),
|
|
530
|
+
], rows: xValues.map((x) => ({
|
|
531
|
+
key: String(x),
|
|
532
|
+
cells: [
|
|
533
|
+
describeX(x),
|
|
534
|
+
...resolved.map((rs) => {
|
|
535
|
+
const rp = rs.byX.get(x);
|
|
536
|
+
return rp ? formatY(rp.domainY) : "—";
|
|
537
|
+
}),
|
|
538
|
+
],
|
|
539
|
+
})) })) : null] }));
|
|
401
540
|
}
|
|
402
541
|
LineChart.displayName = "LineChart";
|
|
403
542
|
export { LineChart };
|