p1-charts 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/README.md +100 -2
- package/dist/CategoryBarChart-1Kn3Vozc.js +580 -0
- package/dist/CategoryBarChart-1Kn3Vozc.js.map +1 -0
- package/dist/ChartFrame-B1bVFM2E.js +403 -0
- package/dist/ChartFrame-B1bVFM2E.js.map +1 -0
- package/dist/{Heatmap-C1XcTUg1.js → Heatmap-CFC753mV.js} +278 -272
- package/dist/Heatmap-CFC753mV.js.map +1 -0
- package/dist/{PieLegend-BZsv9IbF.js → PieLegend-CbpjaOJH.js} +94 -111
- package/dist/PieLegend-CbpjaOJH.js.map +1 -0
- package/dist/PolarChart-hcNTWKfJ.js +450 -0
- package/dist/PolarChart-hcNTWKfJ.js.map +1 -0
- package/dist/Sparkline-BApLVWfg.js +110 -0
- package/dist/Sparkline-BApLVWfg.js.map +1 -0
- package/dist/{TimeSeriesChart-BIwJxgnD.js → TimeSeriesChart-BzeHEtQl.js} +120 -119
- package/dist/{TimeSeriesChart-BIwJxgnD.js.map → TimeSeriesChart-BzeHEtQl.js.map} +1 -1
- package/dist/category.d.ts +1 -1
- package/dist/category.d.ts.map +1 -1
- package/dist/category.js +1 -1
- package/dist/charts/CategoryBarChart/index.d.ts +1 -1
- package/dist/charts/CategoryBarChart/index.d.ts.map +1 -1
- package/dist/charts/Heatmap/index.d.ts.map +1 -1
- package/dist/charts/PolarChart/index.d.ts.map +1 -1
- package/dist/charts/ScatterChart/index.d.ts +1 -1
- package/dist/charts/ScatterChart/index.d.ts.map +1 -1
- package/dist/charts/Sparkline/index.d.ts +5 -2
- package/dist/charts/Sparkline/index.d.ts.map +1 -1
- package/dist/components/ChartFrame/index.d.ts +7 -0
- package/dist/components/ChartFrame/index.d.ts.map +1 -0
- package/dist/components/ChartStory/index.d.ts +7 -0
- package/dist/components/ChartStory/index.d.ts.map +1 -0
- package/dist/components/HeatmapCells/index.d.ts +1 -1
- package/dist/components/HeatmapCells/index.d.ts.map +1 -1
- package/dist/{css-C-rsk7-l.js → css-BtFQ2bM4.js} +2 -2
- package/dist/{css-C-rsk7-l.js.map → css-BtFQ2bM4.js.map} +1 -1
- package/dist/heatmap.js +1 -1
- package/dist/hooks/useMarkMemory.d.ts +9 -0
- package/dist/hooks/useMarkMemory.d.ts.map +1 -0
- package/dist/hooks/usePieTween.d.ts.map +1 -1
- package/dist/hooks/useSteps.d.ts +4 -0
- package/dist/hooks/useSteps.d.ts.map +1 -0
- package/dist/hooks/useTween.d.ts +12 -0
- package/dist/hooks/useTween.d.ts.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +122 -118
- package/dist/index.js.map +1 -1
- package/dist/motion.utils-DFoqptDi.js +55 -0
- package/dist/motion.utils-DFoqptDi.js.map +1 -0
- package/dist/pie.js +1 -1
- package/dist/scatter.js +1 -1
- package/dist/series.js +1 -1
- package/dist/{tooltip.utils-D7xaBSUK.js → shared-C1pGJV4T.js} +3 -7
- package/dist/shared-C1pGJV4T.js.map +1 -0
- package/dist/sparkline.d.ts +1 -1
- package/dist/sparkline.d.ts.map +1 -1
- package/dist/sparkline.js +2 -2
- package/dist/story.d.ts +11 -0
- package/dist/story.d.ts.map +1 -0
- package/dist/story.js +7 -0
- package/dist/theme.js +3 -3
- package/dist/{theme.utils-CP1XtGIe.js → theme.utils-DgmWROoe.js} +40 -20
- package/dist/theme.utils-DgmWROoe.js.map +1 -0
- package/dist/tooltip.utils-CXD_NapA.js +9 -0
- package/dist/tooltip.utils-CXD_NapA.js.map +1 -0
- package/dist/types/category.d.ts +10 -0
- package/dist/types/category.d.ts.map +1 -1
- package/dist/types/heatmap.d.ts +3 -0
- package/dist/types/heatmap.d.ts.map +1 -1
- package/dist/types/heatmapParts.d.ts +2 -0
- package/dist/types/heatmapParts.d.ts.map +1 -1
- package/dist/types/motion.d.ts +31 -0
- package/dist/types/motion.d.ts.map +1 -0
- package/dist/types/pie.d.ts +3 -4
- package/dist/types/pie.d.ts.map +1 -1
- package/dist/types/polar.d.ts +3 -0
- package/dist/types/polar.d.ts.map +1 -1
- package/dist/types/scatter.d.ts +3 -0
- package/dist/types/scatter.d.ts.map +1 -1
- package/dist/types/sparkline.d.ts +3 -0
- package/dist/types/sparkline.d.ts.map +1 -1
- package/dist/types/story.d.ts +73 -0
- package/dist/types/story.d.ts.map +1 -0
- package/dist/{useChartTheme-B06p_jvu.js → useChartTheme-KKtc1C4K.js} +2 -2
- package/dist/{useChartTheme-B06p_jvu.js.map → useChartTheme-KKtc1C4K.js.map} +1 -1
- package/dist/useControllable-Crsr0byh.js +11 -0
- package/dist/useControllable-Crsr0byh.js.map +1 -0
- package/dist/useMarkMemory-CLNY6TtT.js +15 -0
- package/dist/useMarkMemory-CLNY6TtT.js.map +1 -0
- package/dist/useTween-B0G0CcvM.js +46 -0
- package/dist/useTween-B0G0CcvM.js.map +1 -0
- package/dist/utils/category.utils.d.ts +2 -0
- package/dist/utils/category.utils.d.ts.map +1 -1
- package/dist/utils/color.utils.d.ts +9 -0
- package/dist/utils/color.utils.d.ts.map +1 -1
- package/dist/utils/csv.utils.d.ts +6 -0
- package/dist/utils/csv.utils.d.ts.map +1 -0
- package/dist/utils/motion.utils.d.ts +25 -0
- package/dist/utils/motion.utils.d.ts.map +1 -1
- package/dist/utils/pieTween.utils.d.ts +1 -1
- package/dist/utils/pieTween.utils.d.ts.map +1 -1
- package/dist/utils/sparkline.utils.d.ts +15 -0
- package/dist/utils/sparkline.utils.d.ts.map +1 -1
- package/dist/{visibility.utils-nMI5a_Et.js → visibility.utils-wuc3ZS8D.js} +18 -25
- package/dist/visibility.utils-wuc3ZS8D.js.map +1 -0
- package/package.json +10 -3
- package/src/category.ts +1 -0
- package/src/charts/CategoryBarChart/index.tsx +203 -44
- package/src/charts/Heatmap/index.tsx +4 -0
- package/src/charts/PolarChart/index.tsx +98 -5
- package/src/charts/ScatterChart/index.tsx +96 -13
- package/src/charts/Sparkline/index.tsx +54 -7
- package/src/components/ChartFrame/index.tsx +180 -0
- package/src/components/ChartStory/index.tsx +184 -0
- package/src/components/HeatmapCells/index.tsx +5 -2
- package/src/hooks/useMarkMemory.ts +29 -0
- package/src/hooks/usePieTween.ts +9 -44
- package/src/hooks/useSteps.ts +77 -0
- package/src/hooks/useTween.ts +112 -0
- package/src/index.ts +19 -0
- package/src/sparkline.ts +1 -1
- package/src/story.ts +19 -0
- package/src/types/category.ts +11 -0
- package/src/types/heatmap.ts +3 -0
- package/src/types/heatmapParts.ts +2 -0
- package/src/types/motion.ts +35 -0
- package/src/types/pie.ts +3 -3
- package/src/types/polar.ts +3 -0
- package/src/types/scatter.ts +3 -0
- package/src/types/sparkline.ts +3 -0
- package/src/types/story.ts +103 -0
- package/src/utils/category.utils.ts +22 -0
- package/src/utils/color.utils.ts +48 -0
- package/src/utils/csv.utils.ts +28 -0
- package/src/utils/motion.utils.ts +124 -0
- package/src/utils/pieTween.utils.ts +1 -2
- package/src/utils/sparkline.utils.ts +69 -0
- package/dist/CategoryBarChart-YEJ9VB8h.js +0 -453
- package/dist/CategoryBarChart-YEJ9VB8h.js.map +0 -1
- package/dist/Heatmap-C1XcTUg1.js.map +0 -1
- package/dist/PieLegend-BZsv9IbF.js.map +0 -1
- package/dist/PolarChart-BUEhn3CI.js +0 -334
- package/dist/PolarChart-BUEhn3CI.js.map +0 -1
- package/dist/Sparkline-DtNhxGVo.js +0 -67
- package/dist/Sparkline-DtNhxGVo.js.map +0 -1
- package/dist/motion.utils-C1eaZJdQ.js +0 -9
- package/dist/motion.utils-C1eaZJdQ.js.map +0 -1
- package/dist/theme.utils-CP1XtGIe.js.map +0 -1
- package/dist/tooltip.utils-D7xaBSUK.js.map +0 -1
- package/dist/visibility.utils-nMI5a_Et.js.map +0 -1
|
@@ -4,6 +4,15 @@ import { useChartTheme } from "../../hooks/useChartTheme.js";
|
|
|
4
4
|
import { barRadius, polarPoint, sectorPath, wedgeAngles } from "../../utils/polar.utils.js";
|
|
5
5
|
import { classes } from "../../utils/dom.utils.js";
|
|
6
6
|
import { HEATMAP_SVG } from "../../styles/heatmap.js";
|
|
7
|
+
import { useTween } from "../../hooks/useTween.js";
|
|
8
|
+
import { useMarkMemory } from "../../hooks/useMarkMemory.js";
|
|
9
|
+
|
|
10
|
+
interface PolarMemory {
|
|
11
|
+
series: string;
|
|
12
|
+
start: number;
|
|
13
|
+
end: number;
|
|
14
|
+
color: string | undefined;
|
|
15
|
+
}
|
|
7
16
|
|
|
8
17
|
const SIZE = 320;
|
|
9
18
|
const CENTER = SIZE / 2;
|
|
@@ -15,6 +24,7 @@ export function PolarChart(props: PolarChartProps) {
|
|
|
15
24
|
ariaLabel = "Polar chart",
|
|
16
25
|
direction = "clockwise",
|
|
17
26
|
unit,
|
|
27
|
+
animate = true,
|
|
18
28
|
renderTooltip,
|
|
19
29
|
theme,
|
|
20
30
|
unstyled,
|
|
@@ -43,6 +53,61 @@ export function PolarChart(props: PolarChartProps) {
|
|
|
43
53
|
setActive((current) => (current + step + Math.max(count, 1)) % Math.max(count, 1));
|
|
44
54
|
};
|
|
45
55
|
|
|
56
|
+
const target: Record<string, number[]> = {};
|
|
57
|
+
|
|
58
|
+
rose?.series.forEach((item) =>
|
|
59
|
+
rose.categories.forEach((label, index) => {
|
|
60
|
+
const value = item.data[index];
|
|
61
|
+
|
|
62
|
+
if (typeof value === "number" && value > 0) {
|
|
63
|
+
target[`r:${item.id}:${label}`] = [barRadius(value, max, RADIUS)];
|
|
64
|
+
}
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
scatter?.series.forEach((item) =>
|
|
68
|
+
item.data.forEach((point, index) => {
|
|
69
|
+
const turn = (direction === "counterclockwise" ? -1 : 1) * (point.angle * Math.PI) / 180;
|
|
70
|
+
|
|
71
|
+
target[`d:${item.id}:${index}`] = polarPoint(CENTER, CENTER, barRadius(point.radius, max, RADIUS), turn);
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
const shapes = new Map<string, PolarMemory>();
|
|
75
|
+
|
|
76
|
+
if (mode === "bar") {
|
|
77
|
+
rose?.series.forEach((item, seriesIndex) =>
|
|
78
|
+
rose.categories.forEach((label, index) => {
|
|
79
|
+
const [start, end] = wedgeAngles(index, rose.categories.length, direction);
|
|
80
|
+
|
|
81
|
+
shapes.set(`r:${item.id}:${label}`, {
|
|
82
|
+
series: item.id,
|
|
83
|
+
start,
|
|
84
|
+
end,
|
|
85
|
+
color: item.color ?? categorical[seriesIndex % Math.max(1, categorical.length)],
|
|
86
|
+
});
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
scatter?.series.forEach((item, seriesIndex) =>
|
|
91
|
+
item.data.forEach((_point, index) => {
|
|
92
|
+
shapes.set(`d:${item.id}:${index}`, {
|
|
93
|
+
series: item.id,
|
|
94
|
+
start: 0,
|
|
95
|
+
end: 0,
|
|
96
|
+
color: item.color ?? categorical[seriesIndex % Math.max(1, categorical.length)],
|
|
97
|
+
});
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
const toCentre = (key: string, value: readonly number[]) =>
|
|
101
|
+
key.startsWith("r:") ? [0] : value.map(() => CENTER);
|
|
102
|
+
const drawn = useTween(target, animate, { enter: toCentre, exit: toCentre });
|
|
103
|
+
const memory = useMarkMemory(
|
|
104
|
+
new Map([...shapes].filter(([key]) => key in target)),
|
|
105
|
+
drawn,
|
|
106
|
+
);
|
|
107
|
+
const leaving = Object.keys(drawn).filter((key) => !(key in target) && memory.has(key));
|
|
108
|
+
const radiusOf = (id: string, label: string, value: number) =>
|
|
109
|
+
drawn[`r:${id}:${label}`]?.[0] ?? barRadius(value, max, RADIUS);
|
|
110
|
+
|
|
46
111
|
const sectors = mode === "bar" && rose
|
|
47
112
|
? rose.series.flatMap((item, seriesIndex) =>
|
|
48
113
|
rose.categories.flatMap((label, index) => {
|
|
@@ -58,7 +123,7 @@ export function PolarChart(props: PolarChartProps) {
|
|
|
58
123
|
series: item.id,
|
|
59
124
|
label,
|
|
60
125
|
value: String(value),
|
|
61
|
-
d: sectorPath(CENTER, CENTER, 0,
|
|
126
|
+
d: sectorPath(CENTER, CENTER, 0, radiusOf(item.id, label, value), start, end),
|
|
62
127
|
color: item.color ?? categorical[seriesIndex % Math.max(1, categorical.length)],
|
|
63
128
|
index,
|
|
64
129
|
},
|
|
@@ -69,8 +134,7 @@ export function PolarChart(props: PolarChartProps) {
|
|
|
69
134
|
const dots = scatter
|
|
70
135
|
? scatter.series.flatMap((item, seriesIndex) =>
|
|
71
136
|
item.data.map((point, index) => {
|
|
72
|
-
const
|
|
73
|
-
const [x, y] = polarPoint(CENTER, CENTER, barRadius(point.radius, max, RADIUS), turn);
|
|
137
|
+
const [x, y] = drawn[`d:${item.id}:${index}`] ?? [CENTER, CENTER];
|
|
74
138
|
|
|
75
139
|
return {
|
|
76
140
|
key: `${item.id}:${index}`,
|
|
@@ -86,7 +150,7 @@ export function PolarChart(props: PolarChartProps) {
|
|
|
86
150
|
: [];
|
|
87
151
|
const polygons = mode === "line" && rose
|
|
88
152
|
? rose.series.flatMap((item, seriesIndex) => {
|
|
89
|
-
const points = rose.categories.flatMap((
|
|
153
|
+
const points = rose.categories.flatMap((label, index) => {
|
|
90
154
|
const value = item.data[index];
|
|
91
155
|
|
|
92
156
|
if (typeof value !== "number" || !(value > 0)) return [];
|
|
@@ -94,7 +158,7 @@ export function PolarChart(props: PolarChartProps) {
|
|
|
94
158
|
const [start, end] = wedgeAngles(index, rose.categories.length, direction);
|
|
95
159
|
const mid = (start + end) / 2;
|
|
96
160
|
|
|
97
|
-
return [polarPoint(CENTER, CENTER,
|
|
161
|
+
return [polarPoint(CENTER, CENTER, radiusOf(item.id, label, value), mid)];
|
|
98
162
|
});
|
|
99
163
|
|
|
100
164
|
if (points.length < 2) return [];
|
|
@@ -191,6 +255,35 @@ export function PolarChart(props: PolarChartProps) {
|
|
|
191
255
|
onPointerEnter={() => setActive(index)}
|
|
192
256
|
/>
|
|
193
257
|
))}
|
|
258
|
+
{leaving.map((key) => {
|
|
259
|
+
const shape = memory.get(key)!;
|
|
260
|
+
const value = drawn[key]!;
|
|
261
|
+
|
|
262
|
+
return key.startsWith("r:") ? (
|
|
263
|
+
<path
|
|
264
|
+
key={key}
|
|
265
|
+
data-part="sector"
|
|
266
|
+
data-leaving=""
|
|
267
|
+
data-series={shape.series}
|
|
268
|
+
d={sectorPath(CENTER, CENTER, 0, Math.max(0, value[0]!), shape.start, shape.end)}
|
|
269
|
+
fill={shape.color}
|
|
270
|
+
opacity={0.72}
|
|
271
|
+
pointerEvents="none"
|
|
272
|
+
/>
|
|
273
|
+
) : (
|
|
274
|
+
<circle
|
|
275
|
+
key={key}
|
|
276
|
+
data-part="point"
|
|
277
|
+
data-leaving=""
|
|
278
|
+
data-series={shape.series}
|
|
279
|
+
cx={value[0]}
|
|
280
|
+
cy={value[1]}
|
|
281
|
+
r={4}
|
|
282
|
+
fill={shape.color}
|
|
283
|
+
pointerEvents="none"
|
|
284
|
+
/>
|
|
285
|
+
);
|
|
286
|
+
})}
|
|
194
287
|
</svg>
|
|
195
288
|
{current &&
|
|
196
289
|
(renderTooltip?.({ series: current.series, label: current.label, value: current.value }) ?? (
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { useState, type KeyboardEvent } from "react";
|
|
1
|
+
import { useLayoutEffect, useRef, useState, type KeyboardEvent } from "react";
|
|
2
2
|
import type { ScatterChartProps } from "../../types/scatter.js";
|
|
3
3
|
import { useChartTheme } from "../../hooks/useChartTheme.js";
|
|
4
4
|
import { scatterLayout } from "../../utils/scatter.utils.js";
|
|
5
5
|
import { classes } from "../../utils/dom.utils.js";
|
|
6
6
|
import { HEATMAP_SVG } from "../../styles/heatmap.js";
|
|
7
|
+
import { useTween } from "../../hooks/useTween.js";
|
|
8
|
+
import { useMarkMemory } from "../../hooks/useMarkMemory.js";
|
|
7
9
|
|
|
8
10
|
const WIDTH = 640;
|
|
9
11
|
const HEIGHT = 360;
|
|
@@ -18,6 +20,7 @@ export function ScatterChart({
|
|
|
18
20
|
ariaLabel = "Scatter chart",
|
|
19
21
|
xLabel,
|
|
20
22
|
yLabel,
|
|
23
|
+
animate = true,
|
|
21
24
|
onSelect,
|
|
22
25
|
renderTooltip,
|
|
23
26
|
theme,
|
|
@@ -28,6 +31,63 @@ export function ScatterChart({
|
|
|
28
31
|
const text = resolved?.palette.text;
|
|
29
32
|
const grid = resolved?.palette.grid;
|
|
30
33
|
const frame = scatterLayout(series, width, height);
|
|
34
|
+
const target: Record<string, number[]> = {};
|
|
35
|
+
const colors = new Map<string, string | undefined>();
|
|
36
|
+
|
|
37
|
+
frame.points.forEach((item) => {
|
|
38
|
+
const seriesIndex = series.findIndex((entry) => entry.id === item.series);
|
|
39
|
+
const spec = series[seriesIndex];
|
|
40
|
+
const source = spec?.data[item.index];
|
|
41
|
+
const key = `p:${item.series}:${item.index}`;
|
|
42
|
+
|
|
43
|
+
target[key] = [item.px, item.py, source?.radius ?? 3.5];
|
|
44
|
+
if (spec?.markers !== false) {
|
|
45
|
+
colors.set(
|
|
46
|
+
key,
|
|
47
|
+
source?.color ?? spec?.color ?? categorical[seriesIndex % Math.max(1, categorical.length)],
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
frame.xTicks.forEach((tick) => {
|
|
52
|
+
target[`x:${tick}`] = [frame.xOf(tick), 1];
|
|
53
|
+
});
|
|
54
|
+
frame.yTicks.forEach((tick) => {
|
|
55
|
+
target[`y:${tick}`] = [frame.yOf(tick), 1];
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const previous = useRef(frame);
|
|
59
|
+
const tickAt = (layout: typeof frame, key: string) => {
|
|
60
|
+
const value = Number(key.slice(2));
|
|
61
|
+
|
|
62
|
+
return key.startsWith("x:") ? layout.xOf(value) : layout.yOf(value);
|
|
63
|
+
};
|
|
64
|
+
const drawn = useTween(target, animate, {
|
|
65
|
+
enter: (key, [x, y]) =>
|
|
66
|
+
key.startsWith("p:") ? [x!, y!, 0] : [tickAt(previous.current, key), 0],
|
|
67
|
+
exit: (key, [x, y]) => (key.startsWith("p:") ? [x!, y!, 0] : [tickAt(frame, key), 0]),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const memory = useMarkMemory(colors, drawn);
|
|
71
|
+
useLayoutEffect(() => {
|
|
72
|
+
previous.current = frame;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const ticks = (axis: "x" | "y") =>
|
|
76
|
+
Object.keys(drawn)
|
|
77
|
+
.filter((key) => key.startsWith(`${axis}:`))
|
|
78
|
+
.map((key) => {
|
|
79
|
+
const [at, opacity] = drawn[key]!;
|
|
80
|
+
|
|
81
|
+
return { key, value: Number(key.slice(2)), at: at!, opacity: opacity! < 1 ? Math.max(0, opacity!) : undefined };
|
|
82
|
+
});
|
|
83
|
+
const leaving = Object.keys(drawn).filter(
|
|
84
|
+
(key) => key.startsWith("p:") && !(key in target) && memory.has(key),
|
|
85
|
+
);
|
|
86
|
+
const at = (item: { series: string; index: number; px: number; py: number }) => {
|
|
87
|
+
const [x, y, r] = drawn[`p:${item.series}:${item.index}`] ?? [];
|
|
88
|
+
|
|
89
|
+
return { x: x ?? item.px, y: y ?? item.py, r };
|
|
90
|
+
};
|
|
31
91
|
const [active, setActive] = useState(0);
|
|
32
92
|
const point = frame.points[active];
|
|
33
93
|
const spec = series.find((item) => item.id === point?.series);
|
|
@@ -53,17 +113,17 @@ export function ScatterChart({
|
|
|
53
113
|
style={HEATMAP_SVG}
|
|
54
114
|
onKeyDown={onKey}
|
|
55
115
|
>
|
|
56
|
-
{
|
|
57
|
-
<g key={
|
|
58
|
-
<line data-part="grid" x1={40} x2={width - 16} y1={
|
|
59
|
-
<text data-part="tick" x={36} y={
|
|
60
|
-
{tick}
|
|
116
|
+
{ticks("y").map((tick) => (
|
|
117
|
+
<g key={tick.key} opacity={tick.opacity}>
|
|
118
|
+
<line data-part="grid" x1={40} x2={width - 16} y1={tick.at} y2={tick.at} stroke={grid} />
|
|
119
|
+
<text data-part="tick" x={36} y={tick.at} textAnchor="end" fill={text} fontSize={11}>
|
|
120
|
+
{tick.value}
|
|
61
121
|
</text>
|
|
62
122
|
</g>
|
|
63
123
|
))}
|
|
64
|
-
{
|
|
65
|
-
<text key={
|
|
66
|
-
{tick}
|
|
124
|
+
{ticks("x").map((tick) => (
|
|
125
|
+
<text key={tick.key} data-part="tick" x={tick.at} y={height - 12} opacity={tick.opacity} textAnchor="middle" fill={text} fontSize={11}>
|
|
126
|
+
{tick.value}
|
|
67
127
|
</text>
|
|
68
128
|
))}
|
|
69
129
|
{xLabel && (
|
|
@@ -86,7 +146,11 @@ export function ScatterChart({
|
|
|
86
146
|
if (placed.length < 2) return null;
|
|
87
147
|
|
|
88
148
|
const d = placed
|
|
89
|
-
.map((item, index) =>
|
|
149
|
+
.map((item, index) => {
|
|
150
|
+
const { x, y } = at(item);
|
|
151
|
+
|
|
152
|
+
return `${index === 0 ? "M" : "L"} ${x} ${y}`;
|
|
153
|
+
})
|
|
90
154
|
.join(" ");
|
|
91
155
|
|
|
92
156
|
return (
|
|
@@ -113,15 +177,18 @@ export function ScatterChart({
|
|
|
113
177
|
spec?.color ??
|
|
114
178
|
categorical[seriesIndex % Math.max(1, categorical.length)];
|
|
115
179
|
|
|
180
|
+
const place = at(item);
|
|
181
|
+
const radius = place.r ?? source?.radius ?? 3.5;
|
|
182
|
+
|
|
116
183
|
return (
|
|
117
184
|
<circle
|
|
118
185
|
key={`${item.series}:${item.index}`}
|
|
119
186
|
data-part="point"
|
|
120
187
|
data-series={item.series}
|
|
121
188
|
data-active={index === active ? "" : undefined}
|
|
122
|
-
cx={
|
|
123
|
-
cy={
|
|
124
|
-
r={index === active ? 5 :
|
|
189
|
+
cx={place.x}
|
|
190
|
+
cy={place.y}
|
|
191
|
+
r={index === active ? 5 : radius}
|
|
125
192
|
fill={color}
|
|
126
193
|
onPointerEnter={() => setActive(index)}
|
|
127
194
|
onClick={() => {
|
|
@@ -133,6 +200,22 @@ export function ScatterChart({
|
|
|
133
200
|
/>
|
|
134
201
|
);
|
|
135
202
|
})}
|
|
203
|
+
{leaving.map((key) => {
|
|
204
|
+
const [x, y, r] = drawn[key]!;
|
|
205
|
+
|
|
206
|
+
return (
|
|
207
|
+
<circle
|
|
208
|
+
key={key}
|
|
209
|
+
data-part="point"
|
|
210
|
+
data-leaving=""
|
|
211
|
+
cx={x}
|
|
212
|
+
cy={y}
|
|
213
|
+
r={Math.max(0, r!)}
|
|
214
|
+
fill={memory.get(key)}
|
|
215
|
+
pointerEvents="none"
|
|
216
|
+
/>
|
|
217
|
+
);
|
|
218
|
+
})}
|
|
136
219
|
</svg>
|
|
137
220
|
{point && spec &&
|
|
138
221
|
(renderTooltip?.({ series: spec, point, index: point.index }) ?? (
|
|
@@ -1,18 +1,62 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef } from "react";
|
|
1
2
|
import type { SparklineProps } from "../../types/sparkline.js";
|
|
2
|
-
import {
|
|
3
|
+
import { useTween } from "../../hooks/useTween.js";
|
|
4
|
+
import {
|
|
5
|
+
shiftOffset,
|
|
6
|
+
sparklineShapes,
|
|
7
|
+
sparklineValue,
|
|
8
|
+
} from "../../utils/sparkline.utils.js";
|
|
3
9
|
|
|
4
|
-
|
|
10
|
+
const sameValues = (a: readonly (number | null)[], b: readonly (number | null)[]) =>
|
|
11
|
+
a.length === b.length && a.every((value, index) => value === b[index]);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A tile-sized trend. It does not mount a Lightweight Charts instance. When new
|
|
15
|
+
* data is the old data slid along (a live window), the line scrolls.
|
|
16
|
+
*/
|
|
5
17
|
export function Sparkline({
|
|
6
18
|
data,
|
|
7
19
|
width = 80,
|
|
8
20
|
height = 22,
|
|
9
21
|
color = "currentColor",
|
|
10
22
|
area = false,
|
|
23
|
+
animate = true,
|
|
11
24
|
className,
|
|
12
25
|
ariaLabel = "Sparkline",
|
|
13
26
|
}: SparklineProps) {
|
|
14
|
-
const
|
|
15
|
-
|
|
27
|
+
const values = data.map(sparklineValue);
|
|
28
|
+
// Absolute slot of values[0]. Idempotent per values, so a double render is safe.
|
|
29
|
+
const window = useRef({ values, start: 0 });
|
|
30
|
+
|
|
31
|
+
if (!sameValues(window.current.values, values)) {
|
|
32
|
+
window.current = {
|
|
33
|
+
values,
|
|
34
|
+
start: window.current.start + shiftOffset(window.current.values, values),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const start = window.current.start;
|
|
39
|
+
const target: Record<string, number[]> = {};
|
|
40
|
+
|
|
41
|
+
values.forEach((value, index) => {
|
|
42
|
+
if (value !== null) target[start + index] = [index, value];
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const previousStart = useRef(start);
|
|
46
|
+
const drawn = useTween(target, animate, {
|
|
47
|
+
// A new slot slides in from where it sat on the old axis (off the right
|
|
48
|
+
// edge); a dropped slot slides out to its place on the new one (off the left).
|
|
49
|
+
enter: (key, [, value]) => [Number(key) - previousStart.current, value!],
|
|
50
|
+
exit: (key, [, value]) => [Number(key) - start, value!],
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
useLayoutEffect(() => {
|
|
54
|
+
previousStart.current = start;
|
|
55
|
+
});
|
|
56
|
+
const points = Object.entries(drawn)
|
|
57
|
+
.map(([key, [position, value]]) => [position!, value!, Number(key)] as const)
|
|
58
|
+
.sort((a, b) => a[2] - b[2]);
|
|
59
|
+
const shapes = sparklineShapes(points, values.length, width, height);
|
|
16
60
|
|
|
17
61
|
return (
|
|
18
62
|
<svg
|
|
@@ -20,13 +64,16 @@ export function Sparkline({
|
|
|
20
64
|
width={width}
|
|
21
65
|
height={height}
|
|
22
66
|
viewBox={`0 0 ${width} ${height}`}
|
|
67
|
+
overflow="hidden"
|
|
23
68
|
role="img"
|
|
24
69
|
aria-label={ariaLabel}
|
|
25
70
|
>
|
|
26
|
-
{
|
|
27
|
-
|
|
71
|
+
{area && shapes.area && (
|
|
72
|
+
<path data-part="area" d={shapes.area} fill={color} opacity={0.12} />
|
|
73
|
+
)}
|
|
74
|
+
{shapes.lines.map((path, index) => (
|
|
28
75
|
<path
|
|
29
|
-
key={
|
|
76
|
+
key={index}
|
|
30
77
|
data-part="line"
|
|
31
78
|
d={path}
|
|
32
79
|
fill="none"
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { useId, useState, type CSSProperties, type ElementType } from "react";
|
|
2
|
+
import type { ChartFramePart, ChartFrameProps } from "../../types/story.js";
|
|
3
|
+
import { useChartTheme } from "../../hooks/useChartTheme.js";
|
|
4
|
+
import { classes } from "../../utils/dom.utils.js";
|
|
5
|
+
import { downloadCsv } from "../../utils/csv.utils.js";
|
|
6
|
+
import { SR_ONLY } from "../../styles/shared.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Title, subtitle, source and notes around a chart, with the numbers behind it
|
|
10
|
+
* as a table and a CSV download. The chart is the child.
|
|
11
|
+
*/
|
|
12
|
+
export function ChartFrame({
|
|
13
|
+
title,
|
|
14
|
+
subtitle,
|
|
15
|
+
summary,
|
|
16
|
+
source,
|
|
17
|
+
note,
|
|
18
|
+
data,
|
|
19
|
+
filename = "data.csv",
|
|
20
|
+
level = 3,
|
|
21
|
+
children,
|
|
22
|
+
className,
|
|
23
|
+
style,
|
|
24
|
+
classNames,
|
|
25
|
+
styles,
|
|
26
|
+
theme,
|
|
27
|
+
unstyled,
|
|
28
|
+
}: ChartFrameProps) {
|
|
29
|
+
const resolved = useChartTheme({ theme, unstyled });
|
|
30
|
+
const [table, setTable] = useState(false);
|
|
31
|
+
const id = useId();
|
|
32
|
+
const titleId = `${id}-title`;
|
|
33
|
+
const subtitleId = `${id}-subtitle`;
|
|
34
|
+
const summaryId = `${id}-summary`;
|
|
35
|
+
const tableId = `${id}-table`;
|
|
36
|
+
const Heading = `h${level}` as ElementType;
|
|
37
|
+
const text = resolved?.palette.text;
|
|
38
|
+
const grid = resolved?.palette.grid;
|
|
39
|
+
const titleType = resolved?.typography.title;
|
|
40
|
+
const look = (part: ChartFramePart, base?: CSSProperties) =>
|
|
41
|
+
resolved ? { ...base, ...styles?.[part] } : styles?.[part];
|
|
42
|
+
const quiet: CSSProperties = { fontSize: 12, opacity: 0.72, margin: 0 };
|
|
43
|
+
const button: CSSProperties = {
|
|
44
|
+
font: "inherit",
|
|
45
|
+
fontSize: 12,
|
|
46
|
+
color: text,
|
|
47
|
+
background: "transparent",
|
|
48
|
+
border: `1px solid ${resolved?.palette.border}`,
|
|
49
|
+
padding: "2px 8px",
|
|
50
|
+
cursor: "pointer",
|
|
51
|
+
};
|
|
52
|
+
const described = [subtitle && subtitleId, summary && summaryId].filter(Boolean).join(" ");
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<figure
|
|
56
|
+
data-part="root"
|
|
57
|
+
aria-labelledby={title ? titleId : undefined}
|
|
58
|
+
aria-describedby={described || undefined}
|
|
59
|
+
className={classes(className, classNames?.root)}
|
|
60
|
+
style={{ ...look("root", { margin: 0, color: text }), ...style }}
|
|
61
|
+
>
|
|
62
|
+
{(title || subtitle) && (
|
|
63
|
+
<header data-part="header" className={classNames?.header} style={look("header", { marginBottom: 8 })}>
|
|
64
|
+
{title && (
|
|
65
|
+
<Heading
|
|
66
|
+
id={titleId}
|
|
67
|
+
data-part="title"
|
|
68
|
+
className={classNames?.title}
|
|
69
|
+
style={look("title", {
|
|
70
|
+
margin: 0,
|
|
71
|
+
fontFamily: titleType?.family,
|
|
72
|
+
fontSize: titleType?.size ?? 16,
|
|
73
|
+
fontWeight: titleType?.weight ?? 600,
|
|
74
|
+
})}
|
|
75
|
+
>
|
|
76
|
+
{title}
|
|
77
|
+
</Heading>
|
|
78
|
+
)}
|
|
79
|
+
{subtitle && (
|
|
80
|
+
<p id={subtitleId} data-part="subtitle" className={classNames?.subtitle} style={look("subtitle", { ...quiet, fontSize: 13 })}>
|
|
81
|
+
{subtitle}
|
|
82
|
+
</p>
|
|
83
|
+
)}
|
|
84
|
+
</header>
|
|
85
|
+
)}
|
|
86
|
+
{summary && (
|
|
87
|
+
<p id={summaryId} data-part="summary" className={classNames?.summary} style={{ ...SR_ONLY, ...styles?.summary }}>
|
|
88
|
+
{summary}
|
|
89
|
+
</p>
|
|
90
|
+
)}
|
|
91
|
+
<div data-part="body" className={classNames?.body} style={styles?.body}>
|
|
92
|
+
{children}
|
|
93
|
+
</div>
|
|
94
|
+
{data && table && (
|
|
95
|
+
<div style={look("table", { overflowX: "auto", marginTop: 8 })}>
|
|
96
|
+
<table id={tableId} data-part="table" className={classNames?.table} style={resolved ? { borderCollapse: "collapse", fontSize: 12 } : undefined}>
|
|
97
|
+
{title && <caption style={SR_ONLY}>{title}</caption>}
|
|
98
|
+
<thead>
|
|
99
|
+
<tr>
|
|
100
|
+
{data.columns.map((column) => (
|
|
101
|
+
<th key={column} scope="col" style={resolved ? { textAlign: "left", padding: "2px 8px", borderBottom: `1px solid ${grid}` } : undefined}>
|
|
102
|
+
{column}
|
|
103
|
+
</th>
|
|
104
|
+
))}
|
|
105
|
+
</tr>
|
|
106
|
+
</thead>
|
|
107
|
+
<tbody>
|
|
108
|
+
{data.rows.map((row, r) => (
|
|
109
|
+
<tr key={r}>
|
|
110
|
+
{row.map((value, c) =>
|
|
111
|
+
c === 0 ? (
|
|
112
|
+
<th key={c} scope="row" style={resolved ? { textAlign: "left", padding: "2px 8px", fontWeight: 400 } : undefined}>
|
|
113
|
+
{value ?? ""}
|
|
114
|
+
</th>
|
|
115
|
+
) : (
|
|
116
|
+
<td key={c} style={resolved ? { padding: "2px 8px", fontVariantNumeric: "tabular-nums" } : undefined}>
|
|
117
|
+
{value ?? ""}
|
|
118
|
+
</td>
|
|
119
|
+
),
|
|
120
|
+
)}
|
|
121
|
+
</tr>
|
|
122
|
+
))}
|
|
123
|
+
</tbody>
|
|
124
|
+
</table>
|
|
125
|
+
</div>
|
|
126
|
+
)}
|
|
127
|
+
{(source || note || data) && (
|
|
128
|
+
<footer
|
|
129
|
+
data-part="footer"
|
|
130
|
+
className={classNames?.footer}
|
|
131
|
+
style={look("footer", {
|
|
132
|
+
display: "flex",
|
|
133
|
+
flexWrap: "wrap",
|
|
134
|
+
alignItems: "baseline",
|
|
135
|
+
justifyContent: "space-between",
|
|
136
|
+
gap: 8,
|
|
137
|
+
marginTop: 8,
|
|
138
|
+
})}
|
|
139
|
+
>
|
|
140
|
+
<div>
|
|
141
|
+
{source && (
|
|
142
|
+
<p data-part="source" className={classNames?.source} style={look("source", quiet)}>
|
|
143
|
+
Source: {source}
|
|
144
|
+
</p>
|
|
145
|
+
)}
|
|
146
|
+
{note && (
|
|
147
|
+
<p data-part="note" className={classNames?.note} style={look("note", quiet)}>
|
|
148
|
+
{note}
|
|
149
|
+
</p>
|
|
150
|
+
)}
|
|
151
|
+
</div>
|
|
152
|
+
{data && (
|
|
153
|
+
<div data-part="actions" className={classNames?.actions} style={look("actions", { display: "flex", gap: 6 })}>
|
|
154
|
+
<button
|
|
155
|
+
type="button"
|
|
156
|
+
data-part="table-toggle"
|
|
157
|
+
aria-expanded={table}
|
|
158
|
+
aria-controls={table ? tableId : undefined}
|
|
159
|
+
onClick={() => setTable((open) => !open)}
|
|
160
|
+
className={classNames?.["table-toggle"]}
|
|
161
|
+
style={look("table-toggle", button)}
|
|
162
|
+
>
|
|
163
|
+
{table ? "Hide data" : "Show data"}
|
|
164
|
+
</button>
|
|
165
|
+
<button
|
|
166
|
+
type="button"
|
|
167
|
+
data-part="download"
|
|
168
|
+
onClick={() => downloadCsv(data, filename)}
|
|
169
|
+
className={classNames?.download}
|
|
170
|
+
style={look("download", button)}
|
|
171
|
+
>
|
|
172
|
+
Download CSV
|
|
173
|
+
</button>
|
|
174
|
+
</div>
|
|
175
|
+
)}
|
|
176
|
+
</footer>
|
|
177
|
+
)}
|
|
178
|
+
</figure>
|
|
179
|
+
);
|
|
180
|
+
}
|