p1-charts 0.1.0 → 0.2.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 (51) hide show
  1. package/README.md +15 -1
  2. package/dist/CategoryBarChart-YEJ9VB8h.js +453 -0
  3. package/dist/CategoryBarChart-YEJ9VB8h.js.map +1 -0
  4. package/dist/{PolarChart-D_ULWctY.js → PolarChart-BUEhn3CI.js} +135 -83
  5. package/dist/PolarChart-BUEhn3CI.js.map +1 -0
  6. package/dist/{TimeSeriesChart-tYmzRNzV.js → TimeSeriesChart-BIwJxgnD.js} +483 -418
  7. package/dist/TimeSeriesChart-BIwJxgnD.js.map +1 -0
  8. package/dist/category.d.ts +2 -2
  9. package/dist/category.d.ts.map +1 -1
  10. package/dist/category.js +1 -1
  11. package/dist/charts/CategoryBarChart/index.d.ts +1 -1
  12. package/dist/charts/CategoryBarChart/index.d.ts.map +1 -1
  13. package/dist/charts/PolarChart/index.d.ts.map +1 -1
  14. package/dist/charts/ScatterChart/index.d.ts +1 -1
  15. package/dist/charts/ScatterChart/index.d.ts.map +1 -1
  16. package/dist/charts/TimeSeriesChart/index.d.ts +1 -1
  17. package/dist/charts/TimeSeriesChart/index.d.ts.map +1 -1
  18. package/dist/index.d.ts +3 -3
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +3 -3
  21. package/dist/scatter.js +1 -1
  22. package/dist/series.d.ts +1 -1
  23. package/dist/series.d.ts.map +1 -1
  24. package/dist/series.js +1 -1
  25. package/dist/types/category.d.ts +41 -2
  26. package/dist/types/category.d.ts.map +1 -1
  27. package/dist/types/polar.d.ts +7 -1
  28. package/dist/types/polar.d.ts.map +1 -1
  29. package/dist/types/scatter.d.ts +13 -0
  30. package/dist/types/scatter.d.ts.map +1 -1
  31. package/dist/types/timeSeries.d.ts +31 -0
  32. package/dist/types/timeSeries.d.ts.map +1 -1
  33. package/dist/utils/category.utils.d.ts +46 -7
  34. package/dist/utils/category.utils.d.ts.map +1 -1
  35. package/package.json +1 -1
  36. package/src/category.ts +13 -2
  37. package/src/charts/CategoryBarChart/index.tsx +301 -50
  38. package/src/charts/PolarChart/index.tsx +63 -18
  39. package/src/charts/ScatterChart/index.tsx +51 -7
  40. package/src/charts/TimeSeriesChart/index.tsx +117 -2
  41. package/src/index.ts +13 -1
  42. package/src/series.ts +4 -0
  43. package/src/types/category.ts +47 -2
  44. package/src/types/polar.ts +11 -1
  45. package/src/types/scatter.ts +13 -0
  46. package/src/types/timeSeries.ts +35 -0
  47. package/src/utils/category.utils.ts +321 -43
  48. package/dist/CategoryBarChart-CV58kvIr.js +0 -182
  49. package/dist/CategoryBarChart-CV58kvIr.js.map +0 -1
  50. package/dist/PolarChart-D_ULWctY.js.map +0 -1
  51. package/dist/TimeSeriesChart-tYmzRNzV.js.map +0 -1
@@ -1,25 +1,69 @@
1
1
  import { useState, type KeyboardEvent } from "react";
2
- import type { CategoryBarChartProps } from "../../types/category.js";
2
+ import type {
3
+ CategoryBand,
4
+ CategoryBarDatum,
5
+ CategoryBarChartProps,
6
+ CategoryBarSeries,
7
+ CategoryReference,
8
+ } from "../../types/category.js";
3
9
  import { useChartTheme } from "../../hooks/useChartTheme.js";
4
- import { categoryLayout } from "../../utils/category.utils.js";
10
+ import {
11
+ axisTicks,
12
+ categoryFrame,
13
+ type CategoryScale,
14
+ } from "../../utils/category.utils.js";
5
15
  import { classes } from "../../utils/dom.utils.js";
6
16
  import { HEATMAP_SVG } from "../../styles/heatmap.js";
7
17
 
8
18
  const WIDTH = 640;
9
19
  const HEIGHT = 320;
20
+ const NO_REFERENCES: readonly CategoryReference[] = [];
21
+ const NO_BANDS: readonly CategoryBand[] = [];
10
22
 
11
23
  function ink(categorical: readonly string[], index: number, color?: string) {
12
24
  return color ?? categorical[index % Math.max(1, categorical.length)] ?? "currentColor";
13
25
  }
14
26
 
27
+ function readDatum(datum: CategoryBarDatum | undefined): {
28
+ value: number | undefined;
29
+ color?: string;
30
+ label?: string;
31
+ } {
32
+ if (typeof datum === "number") {
33
+ return { value: Number.isFinite(datum) ? datum : undefined };
34
+ }
35
+
36
+ if (datum && typeof datum.value === "number" && Number.isFinite(datum.value)) {
37
+ return { value: datum.value, color: datum.color, label: datum.label };
38
+ }
39
+
40
+ return { value: undefined };
41
+ }
42
+
43
+ function seriesColor(item: CategoryBarSeries | undefined, categorical: readonly string[], index: number) {
44
+ return ink(categorical, index, item?.color);
45
+ }
46
+
15
47
  export function CategoryBarChart({
16
48
  categories,
17
49
  series,
18
50
  mode = "group",
51
+ orientation = "vertical",
52
+ domain,
53
+ rightDomain,
54
+ reference,
55
+ references = NO_REFERENCES,
56
+ bands = NO_BANDS,
57
+ activeIndex,
58
+ valueLabel,
59
+ width = WIDTH,
60
+ height = HEIGHT,
19
61
  className,
20
62
  ariaLabel = "Bar chart",
21
63
  formatValue,
64
+ formatTick,
22
65
  renderTooltip,
66
+ onSelect,
23
67
  theme,
24
68
  unstyled,
25
69
  }: CategoryBarChartProps) {
@@ -27,84 +71,291 @@ export function CategoryBarChart({
27
71
  const categorical = resolved?.palette.categorical ?? [];
28
72
  const text = resolved?.palette.text;
29
73
  const grid = resolved?.palette.grid;
30
- const rects = categoryLayout({
74
+ const hasRight = series.some((item) => item.scale === "right");
75
+ const pad =
76
+ orientation === "horizontal"
77
+ ? { top: 12, right: hasRight ? 48 : 64, bottom: valueLabel ? 36 : 28, left: 108 }
78
+ : { top: 16, right: hasRight ? 48 : 12, bottom: valueLabel ? 40 : 28, left: 44 };
79
+ const lines = reference ? [reference, ...references] : references;
80
+ const frame = categoryFrame({
31
81
  mode,
32
- series,
82
+ orientation,
83
+ domain,
84
+ rightDomain,
85
+ pad,
86
+ width,
87
+ height,
33
88
  categories: categories.length,
34
- width: WIDTH,
35
- height: HEIGHT,
89
+ pins: [
90
+ ...lines.map((line) => ({ value: line.value, scale: line.scale })),
91
+ ...bands.flatMap((band) => [
92
+ { value: band.from, scale: band.scale },
93
+ { value: band.to, scale: band.scale },
94
+ ]),
95
+ ],
96
+ series: series.map((item) => ({
97
+ id: item.id,
98
+ scale: item.scale,
99
+ data: item.data.map((datum) => readDatum(datum).value ?? Number.NaN),
100
+ })),
36
101
  });
37
- const [active, setActive] = useState(0);
102
+ const [focus, setFocus] = useState<number | null>(null);
103
+ const active = focus ?? activeIndex ?? 0;
38
104
  const category = categories[active] ?? categories[0];
39
105
  const rows = series.flatMap((item, index) => {
40
- const value = category === undefined ? undefined : item.data[active];
106
+ const datum = category === undefined ? undefined : readDatum(item.data[active]);
41
107
 
42
- return typeof value === "number" && Number.isFinite(value)
43
- ? [
108
+ return datum?.value === undefined
109
+ ? []
110
+ : [
44
111
  {
45
112
  label: item.name,
46
- value: formatValue?.(item.id, value) ?? String(value),
47
- color: ink(categorical, index, item.color),
113
+ value: formatValue?.(item.id, datum.value) ?? String(datum.value),
114
+ color: datum.color ?? seriesColor(item, categorical, index),
48
115
  },
49
- ]
50
- : [];
116
+ ];
51
117
  });
118
+ const tickLabel = (value: number) => formatTick?.(value) ?? String(value);
119
+ const choose = (index: number) => {
120
+ setFocus(index);
121
+ const label = categories[index];
122
+
123
+ if (label !== undefined) onSelect?.(label, index);
124
+ };
52
125
 
53
126
  const onKey = (event: KeyboardEvent<SVGSVGElement>) => {
54
- if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
127
+ const forward = orientation === "horizontal" ? "ArrowDown" : "ArrowRight";
128
+ const back = orientation === "horizontal" ? "ArrowUp" : "ArrowLeft";
129
+
130
+ if (event.key !== forward && event.key !== back) return;
55
131
 
56
132
  event.preventDefault();
57
- setActive((current) => {
58
- const next = event.key === "ArrowRight" ? current + 1 : current - 1;
133
+ const next = event.key === forward ? active + 1 : active - 1;
59
134
 
60
- return Math.max(0, Math.min(categories.length - 1, next));
61
- });
135
+ choose(Math.max(0, Math.min(categories.length - 1, next)));
62
136
  };
63
137
 
138
+ const ticksFor = (extent: { min: number; max: number }, scale: CategoryScale) =>
139
+ axisTicks(extent.min, extent.max).map((value) => ({
140
+ value,
141
+ at: frame.across(value, scale),
142
+ }));
143
+
64
144
  return (
65
- <div className={classes(className)} data-mode={mode} style={{ position: "relative" }}>
145
+ <div
146
+ className={classes(className)}
147
+ data-mode={mode}
148
+ data-orientation={orientation}
149
+ style={{ position: "relative" }}
150
+ >
66
151
  <svg
67
152
  role="img"
68
153
  aria-label={ariaLabel}
69
154
  tabIndex={0}
70
- viewBox={`0 0 ${WIDTH} ${HEIGHT}`}
155
+ viewBox={`0 0 ${width} ${height}`}
71
156
  style={HEATMAP_SVG}
72
157
  onKeyDown={onKey}
158
+ onPointerLeave={() => setFocus(null)}
73
159
  >
74
- <line data-part="axis" x1={36} y1={HEIGHT - 28} x2={WIDTH - 12} y2={HEIGHT - 28} stroke={grid} />
75
- {categories.map((label, index) => (
160
+ {orientation === "horizontal" ? (
161
+ <line
162
+ data-part="axis"
163
+ x1={pad.left}
164
+ y1={pad.top}
165
+ x2={pad.left}
166
+ y2={height - pad.bottom}
167
+ stroke={grid}
168
+ />
169
+ ) : (
170
+ <line
171
+ data-part="axis"
172
+ x1={pad.left}
173
+ y1={height - pad.bottom}
174
+ x2={width - pad.right}
175
+ y2={height - pad.bottom}
176
+ stroke={grid}
177
+ />
178
+ )}
179
+ {categories.map((label, index) => {
180
+ const slot =
181
+ orientation === "horizontal"
182
+ ? (height - pad.top - pad.bottom) / categories.length
183
+ : (width - pad.left - pad.right) / categories.length;
184
+ const at =
185
+ orientation === "horizontal"
186
+ ? pad.top + (index + 0.5) * slot
187
+ : pad.left + (index + 0.5) * slot;
188
+
189
+ return orientation === "horizontal" ? (
190
+ <text
191
+ key={label}
192
+ data-part="tick"
193
+ x={pad.left - 8}
194
+ y={at}
195
+ textAnchor="end"
196
+ dominantBaseline="middle"
197
+ fill={text}
198
+ fontSize={11}
199
+ >
200
+ {label}
201
+ </text>
202
+ ) : (
203
+ <text
204
+ key={label}
205
+ data-part="tick"
206
+ x={at}
207
+ y={height - pad.bottom + 14}
208
+ textAnchor="middle"
209
+ fill={text}
210
+ fontSize={11}
211
+ >
212
+ {label}
213
+ </text>
214
+ );
215
+ })}
216
+ {ticksFor(frame.left, "left").map((tick) =>
217
+ orientation === "horizontal" ? (
218
+ <text
219
+ key={`l-${tick.value}`}
220
+ data-part="value-tick"
221
+ x={tick.at.x1}
222
+ y={height - pad.bottom + 14}
223
+ textAnchor="middle"
224
+ fill={text}
225
+ fontSize={11}
226
+ >
227
+ {tickLabel(tick.value)}
228
+ </text>
229
+ ) : (
230
+ <text
231
+ key={`l-${tick.value}`}
232
+ data-part="value-tick"
233
+ x={pad.left - 6}
234
+ y={tick.at.y1}
235
+ textAnchor="end"
236
+ dominantBaseline="middle"
237
+ fill={text}
238
+ fontSize={11}
239
+ >
240
+ {tickLabel(tick.value)}
241
+ </text>
242
+ ),
243
+ )}
244
+ {frame.right &&
245
+ ticksFor(frame.right, "right").map((tick) => (
246
+ <text
247
+ key={`r-${tick.value}`}
248
+ data-part="value-tick"
249
+ data-scale="right"
250
+ x={width - pad.right + 6}
251
+ y={tick.at.y1}
252
+ dominantBaseline="middle"
253
+ fill={text}
254
+ fontSize={11}
255
+ >
256
+ {tickLabel(tick.value)}
257
+ </text>
258
+ ))}
259
+ {valueLabel && (
76
260
  <text
77
- key={label}
78
- data-part="tick"
79
- x={36 + ((WIDTH - 48) / categories.length) * (index + 0.5)}
80
- y={HEIGHT - 10}
81
- textAnchor="middle"
261
+ data-part="axis-label"
262
+ x={orientation === "horizontal" ? (pad.left + width - pad.right) / 2 : pad.left}
263
+ y={height - 4}
264
+ textAnchor={orientation === "horizontal" ? "middle" : "start"}
82
265
  fill={text}
83
266
  fontSize={11}
84
267
  >
85
- {label}
268
+ {valueLabel}
86
269
  </text>
87
- ))}
88
- {rects.map((rect) => (
89
- <rect
90
- key={`${rect.series}:${rect.category}`}
91
- data-part="bar"
92
- data-series={rect.series}
93
- data-category={rect.category}
94
- data-active={rect.category === active ? "" : undefined}
95
- x={rect.x}
96
- y={rect.y}
97
- width={rect.width}
98
- height={Math.max(rect.height, rect.value === 0 ? 0 : 1)}
99
- fill={ink(
100
- categorical,
101
- series.findIndex((item) => item.id === rect.series),
102
- series.find((item) => item.id === rect.series)?.color,
103
- )}
104
- opacity={mode === "overlay" ? 0.72 : 1}
105
- onPointerEnter={() => setActive(rect.category)}
106
- />
107
- ))}
270
+ )}
271
+ {bands.map((band, index) => {
272
+ const start = frame.across(band.from, band.scale);
273
+ const end = frame.across(band.to, band.scale);
274
+ const horizontal = orientation === "horizontal";
275
+ const x = horizontal ? Math.min(start.x1, end.x1) : pad.left;
276
+ const y = horizontal ? pad.top : Math.min(start.y1, end.y1);
277
+ const bandWidth = horizontal
278
+ ? Math.abs(start.x1 - end.x1)
279
+ : width - pad.left - pad.right;
280
+ const bandHeight = horizontal
281
+ ? height - pad.top - pad.bottom
282
+ : Math.abs(start.y1 - end.y1);
283
+
284
+ return (
285
+ <rect
286
+ key={`band-${index}`}
287
+ data-part="band"
288
+ x={x}
289
+ y={y}
290
+ width={bandWidth}
291
+ height={bandHeight}
292
+ fill={band.color ?? grid}
293
+ />
294
+ );
295
+ })}
296
+ {lines.map((line, index) => {
297
+ const at = frame.across(line.value, line.scale);
298
+
299
+ return (
300
+ <g key={`ref-${line.label ?? line.value}-${index}`}>
301
+ <line
302
+ data-part="reference"
303
+ {...at}
304
+ stroke={line.color ?? grid}
305
+ strokeDasharray="4 3"
306
+ />
307
+ {line.label && (
308
+ <text
309
+ data-part="reference-label"
310
+ x={at.x1 + 4}
311
+ y={pad.top + 10 + index * 12}
312
+ fill={line.color ?? text}
313
+ fontSize={10}
314
+ >
315
+ {line.label}
316
+ </text>
317
+ )}
318
+ </g>
319
+ );
320
+ })}
321
+ {frame.rects.map((rect) => {
322
+ const item = series.find((entry) => entry.id === rect.series);
323
+ const index = series.findIndex((entry) => entry.id === rect.series);
324
+ const datum = readDatum(item?.data[rect.category]);
325
+
326
+ return (
327
+ <g key={`${rect.series}:${rect.category}`}>
328
+ <rect
329
+ data-part="bar"
330
+ data-series={rect.series}
331
+ data-category={rect.category}
332
+ data-active={rect.category === active ? "" : undefined}
333
+ x={rect.x}
334
+ y={rect.y}
335
+ width={rect.width}
336
+ height={Math.max(rect.height, rect.value === 0 ? 0 : 1)}
337
+ fill={datum?.color ?? seriesColor(item, categorical, index)}
338
+ opacity={mode === "overlay" ? 0.72 : 1}
339
+ onPointerEnter={() => setFocus(rect.category)}
340
+ onClick={() => choose(rect.category)}
341
+ />
342
+ {datum?.label && (
343
+ <text
344
+ data-part="bar-label"
345
+ data-series={rect.series}
346
+ x={orientation === "horizontal" ? rect.x + rect.width + 4 : rect.x + rect.width / 2}
347
+ y={orientation === "horizontal" ? rect.y + rect.height / 2 : rect.y - 4}
348
+ textAnchor={orientation === "horizontal" ? "start" : "middle"}
349
+ dominantBaseline={orientation === "horizontal" ? "middle" : "auto"}
350
+ fill={text}
351
+ fontSize={10}
352
+ >
353
+ {datum.label}
354
+ </text>
355
+ )}
356
+ </g>
357
+ );
358
+ })}
108
359
  </svg>
109
360
  {category !== undefined &&
110
361
  (renderTooltip?.(category, rows) ?? (
@@ -24,11 +24,15 @@ export function PolarChart(props: PolarChartProps) {
24
24
  const text = resolved?.palette.text;
25
25
  const grid = resolved?.palette.grid;
26
26
  const [active, setActive] = useState(0);
27
- const bar = props.mode !== "scatter";
28
- const count = bar ? props.categories.length : props.series.reduce((sum, item) => sum + item.data.length, 0);
29
- const max = bar
30
- ? Math.max(0, ...props.series.flatMap((item) => item.data.filter((value) => Number.isFinite(value))))
31
- : Math.max(0, ...props.series.flatMap((item) => item.data.map((point) => point.radius)));
27
+ const mode = props.mode ?? "bar";
28
+ const scatter = props.mode === "scatter" ? props : null;
29
+ const rose = props.mode === "scatter" ? null : props;
30
+ const count = scatter
31
+ ? scatter.series.reduce((sum, item) => sum + item.data.length, 0)
32
+ : (rose?.categories.length ?? 0);
33
+ const max = scatter
34
+ ? Math.max(0, ...scatter.series.flatMap((item) => item.data.map((point) => point.radius)))
35
+ : Math.max(0, ...(rose?.series.flatMap((item) => item.data.filter((value) => Number.isFinite(value))) ?? []));
32
36
 
33
37
  const onKey = (event: KeyboardEvent<SVGSVGElement>) => {
34
38
  if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
@@ -39,14 +43,14 @@ export function PolarChart(props: PolarChartProps) {
39
43
  setActive((current) => (current + step + Math.max(count, 1)) % Math.max(count, 1));
40
44
  };
41
45
 
42
- const sectors = bar
43
- ? props.series.flatMap((item, seriesIndex) =>
44
- props.categories.flatMap((label, index) => {
46
+ const sectors = mode === "bar" && rose
47
+ ? rose.series.flatMap((item, seriesIndex) =>
48
+ rose.categories.flatMap((label, index) => {
45
49
  const value = item.data[index];
46
50
 
47
51
  if (typeof value !== "number" || !(value > 0)) return [];
48
52
 
49
- const [start, end] = wedgeAngles(index, props.categories.length, direction);
53
+ const [start, end] = wedgeAngles(index, rose.categories.length, direction);
50
54
 
51
55
  return [
52
56
  {
@@ -62,8 +66,8 @@ export function PolarChart(props: PolarChartProps) {
62
66
  }),
63
67
  )
64
68
  : [];
65
- const dots = !bar
66
- ? props.series.flatMap((item, seriesIndex) =>
69
+ const dots = scatter
70
+ ? scatter.series.flatMap((item, seriesIndex) =>
67
71
  item.data.map((point, index) => {
68
72
  const turn = (direction === "counterclockwise" ? -1 : 1) * (point.angle * Math.PI) / 180;
69
73
  const [x, y] = polarPoint(CENTER, CENTER, barRadius(point.radius, max, RADIUS), turn);
@@ -80,12 +84,41 @@ export function PolarChart(props: PolarChartProps) {
80
84
  }),
81
85
  )
82
86
  : [];
83
- const current = bar
87
+ const polygons = mode === "line" && rose
88
+ ? rose.series.flatMap((item, seriesIndex) => {
89
+ const points = rose.categories.flatMap((_label, index) => {
90
+ const value = item.data[index];
91
+
92
+ if (typeof value !== "number" || !(value > 0)) return [];
93
+
94
+ const [start, end] = wedgeAngles(index, rose.categories.length, direction);
95
+ const mid = (start + end) / 2;
96
+
97
+ return [polarPoint(CENTER, CENTER, barRadius(value, max, RADIUS), mid)];
98
+ });
99
+
100
+ if (points.length < 2) return [];
101
+
102
+ return [
103
+ {
104
+ key: item.id,
105
+ series: item.id,
106
+ label: rose.categories[active] ?? item.name,
107
+ value: String(item.data[active] ?? ""),
108
+ d: `M ${points.map((point) => point.join(" ")).join(" L ")} Z`,
109
+ color: item.color ?? categorical[seriesIndex % Math.max(1, categorical.length)],
110
+ },
111
+ ];
112
+ })
113
+ : [];
114
+ const current = mode === "bar"
84
115
  ? sectors.find((sector) => sector.index === active)
85
- : dots[active];
116
+ : mode === "line"
117
+ ? polygons[0]
118
+ : dots[active];
86
119
 
87
120
  return (
88
- <div className={classes(className)} data-mode={props.mode ?? "bar"} style={{ position: "relative" }}>
121
+ <div className={classes(className)} data-mode={mode} style={{ position: "relative" }}>
89
122
  <svg
90
123
  role="img"
91
124
  aria-label={ariaLabel}
@@ -105,10 +138,10 @@ export function PolarChart(props: PolarChartProps) {
105
138
  stroke={grid}
106
139
  />
107
140
  ))}
108
- {bar &&
109
- props.categories.map((label, index) => {
110
- const [start] = wedgeAngles(index, props.categories.length, direction);
111
- const [x, y] = polarPoint(CENTER, CENTER, RADIUS + 16, start + (direction === "counterclockwise" ? -1 : 1) * (Math.PI / props.categories.length));
141
+ {rose &&
142
+ rose.categories.map((label, index) => {
143
+ const [start] = wedgeAngles(index, rose.categories.length, direction);
144
+ const [x, y] = polarPoint(CENTER, CENTER, RADIUS + 16, start + (direction === "counterclockwise" ? -1 : 1) * (Math.PI / rose.categories.length));
112
145
 
113
146
  return (
114
147
  <text key={label} data-part="tick" x={x} y={y} textAnchor="middle" fill={text} fontSize={11}>
@@ -121,6 +154,18 @@ export function PolarChart(props: PolarChartProps) {
121
154
  {unit}
122
155
  </text>
123
156
  )}
157
+ {polygons.map((polygon) => (
158
+ <path
159
+ key={polygon.key}
160
+ data-part="line"
161
+ data-series={polygon.series}
162
+ d={polygon.d}
163
+ fill={polygon.color}
164
+ fillOpacity={0.16}
165
+ stroke={polygon.color}
166
+ strokeWidth={1.5}
167
+ />
168
+ ))}
124
169
  {sectors.map((sector) => (
125
170
  <path
126
171
  key={sector.key}
@@ -8,12 +8,17 @@ import { HEATMAP_SVG } from "../../styles/heatmap.js";
8
8
  const WIDTH = 640;
9
9
  const HEIGHT = 360;
10
10
 
11
+ const DASH = { solid: undefined, dashed: "5 4", dotted: "2 3" } as const;
12
+
11
13
  export function ScatterChart({
12
14
  series,
15
+ width = WIDTH,
16
+ height = HEIGHT,
13
17
  className,
14
18
  ariaLabel = "Scatter chart",
15
19
  xLabel,
16
20
  yLabel,
21
+ onSelect,
17
22
  renderTooltip,
18
23
  theme,
19
24
  unstyled,
@@ -22,7 +27,7 @@ export function ScatterChart({
22
27
  const categorical = resolved?.palette.categorical ?? [];
23
28
  const text = resolved?.palette.text;
24
29
  const grid = resolved?.palette.grid;
25
- const frame = scatterLayout(series, WIDTH, HEIGHT);
30
+ const frame = scatterLayout(series, width, height);
26
31
  const [active, setActive] = useState(0);
27
32
  const point = frame.points[active];
28
33
  const spec = series.find((item) => item.id === point?.series);
@@ -44,25 +49,25 @@ export function ScatterChart({
44
49
  role="img"
45
50
  aria-label={ariaLabel}
46
51
  tabIndex={0}
47
- viewBox={`0 0 ${WIDTH} ${HEIGHT}`}
52
+ viewBox={`0 0 ${width} ${height}`}
48
53
  style={HEATMAP_SVG}
49
54
  onKeyDown={onKey}
50
55
  >
51
56
  {frame.yTicks.map((tick) => (
52
57
  <g key={`y-${tick}`}>
53
- <line data-part="grid" x1={40} x2={WIDTH - 16} y1={frame.yOf(tick)} y2={frame.yOf(tick)} stroke={grid} />
58
+ <line data-part="grid" x1={40} x2={width - 16} y1={frame.yOf(tick)} y2={frame.yOf(tick)} stroke={grid} />
54
59
  <text data-part="tick" x={36} y={frame.yOf(tick)} textAnchor="end" fill={text} fontSize={11}>
55
60
  {tick}
56
61
  </text>
57
62
  </g>
58
63
  ))}
59
64
  {frame.xTicks.map((tick) => (
60
- <text key={`x-${tick}`} data-part="tick" x={frame.xOf(tick)} y={HEIGHT - 12} textAnchor="middle" fill={text} fontSize={11}>
65
+ <text key={`x-${tick}`} data-part="tick" x={frame.xOf(tick)} y={height - 12} textAnchor="middle" fill={text} fontSize={11}>
61
66
  {tick}
62
67
  </text>
63
68
  ))}
64
69
  {xLabel && (
65
- <text data-part="axis-label" x={WIDTH / 2} y={HEIGHT - 1} textAnchor="middle" fill={text} fontSize={11}>
70
+ <text data-part="axis-label" x={width / 2} y={height - 1} textAnchor="middle" fill={text} fontSize={11}>
66
71
  {xLabel}
67
72
  </text>
68
73
  )}
@@ -71,9 +76,42 @@ export function ScatterChart({
71
76
  {yLabel}
72
77
  </text>
73
78
  )}
79
+ {series.map((entry) => {
80
+ if (!entry.line) return null;
81
+
82
+ const placed = frame.points
83
+ .filter((item) => item.series === entry.id)
84
+ .sort((a, b) => a.index - b.index);
85
+
86
+ if (placed.length < 2) return null;
87
+
88
+ const d = placed
89
+ .map((item, index) => `${index === 0 ? "M" : "L"} ${item.px} ${item.py}`)
90
+ .join(" ");
91
+
92
+ return (
93
+ <path
94
+ key={`line-${entry.id}`}
95
+ data-part="line"
96
+ data-series={entry.id}
97
+ d={d}
98
+ fill="none"
99
+ stroke={entry.color ?? text}
100
+ strokeDasharray={DASH[entry.lineStyle ?? "solid"]}
101
+ />
102
+ );
103
+ })}
74
104
  {frame.points.map((item, index) => {
75
105
  const seriesIndex = series.findIndex((entry) => entry.id === item.series);
76
- const color = series[seriesIndex]?.color ?? categorical[seriesIndex % Math.max(1, categorical.length)];
106
+ const spec = series[seriesIndex];
107
+
108
+ if (spec?.markers === false) return null;
109
+
110
+ const source = spec?.data[item.index];
111
+ const color =
112
+ source?.color ??
113
+ spec?.color ??
114
+ categorical[seriesIndex % Math.max(1, categorical.length)];
77
115
 
78
116
  return (
79
117
  <circle
@@ -83,9 +121,15 @@ export function ScatterChart({
83
121
  data-active={index === active ? "" : undefined}
84
122
  cx={item.px}
85
123
  cy={item.py}
86
- r={index === active ? 5 : 3.5}
124
+ r={index === active ? 5 : (source?.radius ?? 3.5)}
87
125
  fill={color}
88
126
  onPointerEnter={() => setActive(index)}
127
+ onClick={() => {
128
+ const chosen = series.find((entry) => entry.id === item.series);
129
+ const point = chosen?.data[item.index];
130
+
131
+ if (chosen && point) onSelect?.({ series: chosen, point, index: item.index });
132
+ }}
89
133
  />
90
134
  );
91
135
  })}