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