p1-charts 0.2.0 → 0.4.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 +54 -0
- package/README.md +158 -4
- package/dist/CategoryBarChart-KYzsjGwg.js +715 -0
- package/dist/CategoryBarChart-KYzsjGwg.js.map +1 -0
- package/dist/ChartFrame-DOEsou-R.js +403 -0
- package/dist/ChartFrame-DOEsou-R.js.map +1 -0
- package/dist/ChartLegend-B8ZmIG2y.js +46 -0
- package/dist/ChartLegend-B8ZmIG2y.js.map +1 -0
- package/dist/{Heatmap-C1XcTUg1.js → Heatmap-Bwhf305C.js} +278 -272
- package/dist/Heatmap-Bwhf305C.js.map +1 -0
- package/dist/{PieLegend-BZsv9IbF.js → PieLegend-16E6W10x.js} +94 -111
- package/dist/PieLegend-16E6W10x.js.map +1 -0
- package/dist/PolarChart-C_vPz-3K.js +489 -0
- package/dist/PolarChart-C_vPz-3K.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-BZMLERDZ.js} +530 -529
- package/dist/{TimeSeriesChart-BIwJxgnD.js.map → TimeSeriesChart-BZMLERDZ.js.map} +1 -1
- package/dist/category.d.ts +5 -2
- package/dist/category.d.ts.map +1 -1
- package/dist/category.js +4 -3
- package/dist/charts/CategoryBarChart/index.d.ts +3 -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/ChartLegend/index.d.ts +13 -0
- package/dist/components/ChartLegend/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-BTZA9hea.js} +2 -2
- package/dist/{css-C-rsk7-l.js.map → css-BTZA9hea.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 +16 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +122 -117
- 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.d.ts +4 -1
- package/dist/scatter.d.ts.map +1 -1
- package/dist/scatter.js +3 -2
- 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/standalone/p1-charts.standalone.js +13 -0
- package/dist/standalone.d.ts +27 -0
- package/dist/standalone.d.ts.map +1 -0
- 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-DF_e8LxQ.js} +132 -113
- package/dist/theme.utils-DF_e8LxQ.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 +26 -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 +34 -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-D0bQ3LQq.js} +2 -2
- package/dist/{useChartTheme-B06p_jvu.js.map → useChartTheme-D0bQ3LQq.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 +15 -1
- 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/scatter.utils.d.ts +17 -1
- package/dist/utils/scatter.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/utils/theme.utils.d.ts +1 -1
- package/dist/utils/theme.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 +16 -5
- package/src/category.ts +5 -1
- package/src/charts/CategoryBarChart/index.tsx +310 -62
- package/src/charts/Heatmap/index.tsx +4 -0
- package/src/charts/PolarChart/index.tsx +98 -5
- package/src/charts/ScatterChart/index.tsx +164 -18
- package/src/charts/Sparkline/index.tsx +54 -7
- package/src/components/ChartFrame/index.tsx +180 -0
- package/src/components/ChartLegend/index.tsx +46 -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 +24 -1
- package/src/scatter.ts +4 -1
- package/src/sparkline.ts +1 -1
- package/src/standalone.tsx +68 -0
- package/src/story.ts +19 -0
- package/src/types/category.ts +27 -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 +23 -0
- package/src/types/sparkline.ts +3 -0
- package/src/types/story.ts +103 -0
- package/src/utils/category.utils.ts +111 -2
- 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/scatter.utils.ts +22 -5
- package/src/utils/sparkline.utils.ts +69 -0
- package/src/utils/theme.utils.ts +18 -15
- 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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState, type KeyboardEvent } from "react";
|
|
1
|
+
import { useLayoutEffect, useMemo, useRef, useState, type KeyboardEvent } from "react";
|
|
2
2
|
import type {
|
|
3
3
|
CategoryBand,
|
|
4
4
|
CategoryBarDatum,
|
|
@@ -10,10 +10,15 @@ import { useChartTheme } from "../../hooks/useChartTheme.js";
|
|
|
10
10
|
import {
|
|
11
11
|
axisTicks,
|
|
12
12
|
categoryFrame,
|
|
13
|
+
type CategoryFrame,
|
|
13
14
|
type CategoryScale,
|
|
14
15
|
} from "../../utils/category.utils.js";
|
|
15
16
|
import { classes } from "../../utils/dom.utils.js";
|
|
17
|
+
import { useTween } from "../../hooks/useTween.js";
|
|
18
|
+
import { useMarkMemory } from "../../hooks/useMarkMemory.js";
|
|
19
|
+
import { sortOrder } from "../../utils/category.utils.js";
|
|
16
20
|
import { HEATMAP_SVG } from "../../styles/heatmap.js";
|
|
21
|
+
import { ChartLegend } from "../../components/ChartLegend/index.js";
|
|
17
22
|
|
|
18
23
|
const WIDTH = 640;
|
|
19
24
|
const HEIGHT = 320;
|
|
@@ -24,6 +29,13 @@ function ink(categorical: readonly string[], index: number, color?: string) {
|
|
|
24
29
|
return color ?? categorical[index % Math.max(1, categorical.length)] ?? "currentColor";
|
|
25
30
|
}
|
|
26
31
|
|
|
32
|
+
/** Room for 11px category labels: about 6.2 units a character, never under the old 108. */
|
|
33
|
+
export function labelRoom(labels: readonly string[]): number {
|
|
34
|
+
const longest = labels.reduce((most, label) => Math.max(most, label.length), 0);
|
|
35
|
+
|
|
36
|
+
return Math.max(108, Math.ceil(longest * 6.2) + 14);
|
|
37
|
+
}
|
|
38
|
+
|
|
27
39
|
function readDatum(datum: CategoryBarDatum | undefined): {
|
|
28
40
|
value: number | undefined;
|
|
29
41
|
color?: string;
|
|
@@ -40,13 +52,22 @@ function readDatum(datum: CategoryBarDatum | undefined): {
|
|
|
40
52
|
return { value: undefined };
|
|
41
53
|
}
|
|
42
54
|
|
|
55
|
+
interface BarMemory {
|
|
56
|
+
series: string;
|
|
57
|
+
label: string;
|
|
58
|
+
scale: CategoryScale;
|
|
59
|
+
color: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
43
62
|
function seriesColor(item: CategoryBarSeries | undefined, categorical: readonly string[], index: number) {
|
|
44
63
|
return ink(categorical, index, item?.color);
|
|
45
64
|
}
|
|
46
65
|
|
|
47
66
|
export function CategoryBarChart({
|
|
48
|
-
categories,
|
|
49
|
-
series,
|
|
67
|
+
categories: sourceCategories,
|
|
68
|
+
series: sourceSeries,
|
|
69
|
+
sort = "none",
|
|
70
|
+
animate = true,
|
|
50
71
|
mode = "group",
|
|
51
72
|
orientation = "vertical",
|
|
52
73
|
domain,
|
|
@@ -56,6 +77,9 @@ export function CategoryBarChart({
|
|
|
56
77
|
bands = NO_BANDS,
|
|
57
78
|
activeIndex,
|
|
58
79
|
valueLabel,
|
|
80
|
+
labelWidth,
|
|
81
|
+
nice = true,
|
|
82
|
+
legend = false,
|
|
59
83
|
width = WIDTH,
|
|
60
84
|
height = HEIGHT,
|
|
61
85
|
className,
|
|
@@ -67,6 +91,27 @@ export function CategoryBarChart({
|
|
|
67
91
|
theme,
|
|
68
92
|
unstyled,
|
|
69
93
|
}: CategoryBarChartProps) {
|
|
94
|
+
const order = useMemo(
|
|
95
|
+
() =>
|
|
96
|
+
sortOrder(
|
|
97
|
+
sourceCategories.length,
|
|
98
|
+
sourceSeries.map((item) => item.data.map((datum) => readDatum(datum).value ?? Number.NaN)),
|
|
99
|
+
sort,
|
|
100
|
+
),
|
|
101
|
+
[sourceCategories.length, sourceSeries, sort],
|
|
102
|
+
);
|
|
103
|
+
const categories = useMemo(
|
|
104
|
+
() => order.map((index) => sourceCategories[index]!),
|
|
105
|
+
[order, sourceCategories],
|
|
106
|
+
);
|
|
107
|
+
const series = useMemo(
|
|
108
|
+
() =>
|
|
109
|
+
sort === "none"
|
|
110
|
+
? sourceSeries
|
|
111
|
+
: sourceSeries.map((item) => ({ ...item, data: order.map((index) => item.data[index]!) })),
|
|
112
|
+
[order, sourceSeries, sort],
|
|
113
|
+
);
|
|
114
|
+
const shownActive = activeIndex === undefined ? undefined : order.indexOf(activeIndex);
|
|
70
115
|
const resolved = useChartTheme({ theme, unstyled });
|
|
71
116
|
const categorical = resolved?.palette.categorical ?? [];
|
|
72
117
|
const text = resolved?.palette.text;
|
|
@@ -74,10 +119,11 @@ export function CategoryBarChart({
|
|
|
74
119
|
const hasRight = series.some((item) => item.scale === "right");
|
|
75
120
|
const pad =
|
|
76
121
|
orientation === "horizontal"
|
|
77
|
-
? { top: 12, right: hasRight ? 48 : 64, bottom: valueLabel ? 36 : 28, left:
|
|
78
|
-
: { top: 16, right: hasRight ? 48 : 12, bottom: valueLabel ? 40 : 28, left: 44 };
|
|
122
|
+
? { top: 12, right: hasRight ? 48 : 64, bottom: valueLabel ? 36 : 28, left: labelWidth ?? labelRoom(categories) }
|
|
123
|
+
: { top: 16, right: hasRight ? 48 : 12, bottom: valueLabel ? 40 : 28, left: labelWidth ?? 44 };
|
|
79
124
|
const lines = reference ? [reference, ...references] : references;
|
|
80
125
|
const frame = categoryFrame({
|
|
126
|
+
nice,
|
|
81
127
|
mode,
|
|
82
128
|
orientation,
|
|
83
129
|
domain,
|
|
@@ -100,7 +146,7 @@ export function CategoryBarChart({
|
|
|
100
146
|
})),
|
|
101
147
|
});
|
|
102
148
|
const [focus, setFocus] = useState<number | null>(null);
|
|
103
|
-
const active = focus ??
|
|
149
|
+
const active = focus ?? (shownActive !== undefined && shownActive >= 0 ? shownActive : 0);
|
|
104
150
|
const category = categories[active] ?? categories[0];
|
|
105
151
|
const rows = series.flatMap((item, index) => {
|
|
106
152
|
const datum = category === undefined ? undefined : readDatum(item.data[active]);
|
|
@@ -115,12 +161,118 @@ export function CategoryBarChart({
|
|
|
115
161
|
},
|
|
116
162
|
];
|
|
117
163
|
});
|
|
164
|
+
const horizontal = orientation === "horizontal";
|
|
165
|
+
const slot = horizontal
|
|
166
|
+
? (height - pad.top - pad.bottom) / Math.max(1, categories.length)
|
|
167
|
+
: (width - pad.left - pad.right) / Math.max(1, categories.length);
|
|
168
|
+
const slotAt = (index: number) =>
|
|
169
|
+
(horizontal ? pad.top : pad.left) + (index + 0.5) * slot;
|
|
170
|
+
const ticksFor = (extent: { min: number; max: number }, scale: CategoryScale) =>
|
|
171
|
+
axisTicks(extent.min, extent.max).map((value) => ({
|
|
172
|
+
value,
|
|
173
|
+
at: frame.across(value, scale),
|
|
174
|
+
}));
|
|
175
|
+
const valuePos = (across: CategoryFrame["across"], value: number, scale: CategoryScale) => {
|
|
176
|
+
const line = across(value, scale);
|
|
177
|
+
|
|
178
|
+
return horizontal ? line.x1 : line.y1;
|
|
179
|
+
};
|
|
180
|
+
const target: Record<string, number[]> = {};
|
|
181
|
+
const bars = new Map<string, BarMemory>();
|
|
182
|
+
const labels = new Map<string, string>();
|
|
183
|
+
|
|
184
|
+
frame.rects.forEach((rect) => {
|
|
185
|
+
const label = categories[rect.category]!;
|
|
186
|
+
const key = `bar:${rect.series}:${label}`;
|
|
187
|
+
const index = series.findIndex((entry) => entry.id === rect.series);
|
|
188
|
+
const item = series[index];
|
|
189
|
+
|
|
190
|
+
target[key] = [rect.x, rect.y, rect.width, rect.height];
|
|
191
|
+
bars.set(key, {
|
|
192
|
+
series: rect.series,
|
|
193
|
+
label,
|
|
194
|
+
scale: item?.scale ?? "left",
|
|
195
|
+
color: readDatum(item?.data[rect.category]).color ?? seriesColor(item, categorical, index),
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
categories.forEach((label, index) => {
|
|
199
|
+
target[`tick:${label}`] = [slotAt(index), 1];
|
|
200
|
+
labels.set(`tick:${label}`, label);
|
|
201
|
+
});
|
|
202
|
+
ticksFor(frame.left, "left").forEach((tick) => {
|
|
203
|
+
target[`v:left:${tick.value}`] = [horizontal ? tick.at.x1 : tick.at.y1, 1];
|
|
204
|
+
});
|
|
205
|
+
if (frame.right) {
|
|
206
|
+
ticksFor(frame.right, "right").forEach((tick) => {
|
|
207
|
+
target[`v:right:${tick.value}`] = [tick.at.y1, 1];
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const baseline = (scale: CategoryScale) => {
|
|
212
|
+
const extent = scale === "right" && frame.right ? frame.right : frame.left;
|
|
213
|
+
|
|
214
|
+
return frame.across(Math.max(extent.min, Math.min(extent.max, 0)), scale);
|
|
215
|
+
};
|
|
216
|
+
const dot = mode === "dot";
|
|
217
|
+
const toBaseline = (memory: BarMemory | undefined, [x, y, w, h]: readonly number[]) => {
|
|
218
|
+
if (dot) return [x!, y!, w!, h!]; // a dot fades where it is; it does not grow from zero
|
|
219
|
+
const base = baseline(memory?.scale ?? "left");
|
|
220
|
+
|
|
221
|
+
return horizontal ? [base.x1, y!, 0, h!] : [x!, base.y1, w!, 0];
|
|
222
|
+
};
|
|
223
|
+
const previousAcross = useRef(frame.across);
|
|
224
|
+
const memory = useRef<ReadonlyMap<string, BarMemory>>(bars);
|
|
225
|
+
const valueTick = (key: string) => {
|
|
226
|
+
const [, scale, raw] = key.split(":") as [string, CategoryScale, string];
|
|
227
|
+
|
|
228
|
+
return { scale, value: Number(raw) };
|
|
229
|
+
};
|
|
230
|
+
const drawn = useTween(target, animate, {
|
|
231
|
+
enter: (key, value) => {
|
|
232
|
+
if (key.startsWith("bar:")) return toBaseline(bars.get(key), value);
|
|
233
|
+
if (key.startsWith("v:")) {
|
|
234
|
+
const { scale, value: at } = valueTick(key);
|
|
235
|
+
|
|
236
|
+
return [valuePos(previousAcross.current, at, scale), 0];
|
|
237
|
+
}
|
|
238
|
+
return [value[0]!, 0];
|
|
239
|
+
},
|
|
240
|
+
exit: (key, value) => {
|
|
241
|
+
if (key.startsWith("bar:")) return toBaseline(memory.current.get(key), value);
|
|
242
|
+
if (key.startsWith("v:")) {
|
|
243
|
+
const { scale, value: at } = valueTick(key);
|
|
244
|
+
|
|
245
|
+
return [valuePos(frame.across, at, scale), 0];
|
|
246
|
+
}
|
|
247
|
+
return [value[0]!, 0];
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
memory.current = useMarkMemory(bars, drawn);
|
|
252
|
+
const tickMemory = useMarkMemory(labels, drawn);
|
|
253
|
+
useLayoutEffect(() => {
|
|
254
|
+
previousAcross.current = frame.across;
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
const centre = (x: number, y: number, w: number, h: number) =>
|
|
258
|
+
horizontal ? { cx: x, cy: y + h / 2 } : { cx: x + w / 2, cy: y };
|
|
259
|
+
const fade = (key: string) => {
|
|
260
|
+
const opacity = drawn[key]?.[1] ?? 1;
|
|
261
|
+
|
|
262
|
+
return opacity < 1 ? Math.max(0, opacity) : undefined;
|
|
263
|
+
};
|
|
264
|
+
const leavingBars = Object.keys(drawn).filter((key) => key.startsWith("bar:") && !(key in target));
|
|
265
|
+
const categoryTicks = Object.keys(drawn).filter((key) => key.startsWith("tick:"));
|
|
266
|
+
const valueTicks = (scale: CategoryScale) =>
|
|
267
|
+
Object.keys(drawn)
|
|
268
|
+
.filter((key) => key.startsWith(`v:${scale}:`))
|
|
269
|
+
.map((key) => ({ key, value: valueTick(key).value, at: drawn[key]![0]!, opacity: fade(key) }));
|
|
118
270
|
const tickLabel = (value: number) => formatTick?.(value) ?? String(value);
|
|
119
271
|
const choose = (index: number) => {
|
|
120
272
|
setFocus(index);
|
|
121
273
|
const label = categories[index];
|
|
122
274
|
|
|
123
|
-
if (label !== undefined) onSelect?.(label, index);
|
|
275
|
+
if (label !== undefined) onSelect?.(label, order[index] ?? index);
|
|
124
276
|
};
|
|
125
277
|
|
|
126
278
|
const onKey = (event: KeyboardEvent<SVGSVGElement>) => {
|
|
@@ -135,12 +287,6 @@ export function CategoryBarChart({
|
|
|
135
287
|
choose(Math.max(0, Math.min(categories.length - 1, next)));
|
|
136
288
|
};
|
|
137
289
|
|
|
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
|
-
|
|
144
290
|
return (
|
|
145
291
|
<div
|
|
146
292
|
className={classes(className)}
|
|
@@ -148,6 +294,17 @@ export function CategoryBarChart({
|
|
|
148
294
|
data-orientation={orientation}
|
|
149
295
|
style={{ position: "relative" }}
|
|
150
296
|
>
|
|
297
|
+
{legend && (
|
|
298
|
+
<ChartLegend
|
|
299
|
+
unstyled={unstyled}
|
|
300
|
+
color={text}
|
|
301
|
+
items={
|
|
302
|
+
legend === true
|
|
303
|
+
? series.map((item, index) => ({ label: item.name, color: seriesColor(item, categorical, index) }))
|
|
304
|
+
: legend
|
|
305
|
+
}
|
|
306
|
+
/>
|
|
307
|
+
)}
|
|
151
308
|
<svg
|
|
152
309
|
role="img"
|
|
153
310
|
aria-label={ariaLabel}
|
|
@@ -176,22 +333,17 @@ export function CategoryBarChart({
|
|
|
176
333
|
stroke={grid}
|
|
177
334
|
/>
|
|
178
335
|
)}
|
|
179
|
-
{
|
|
180
|
-
const
|
|
181
|
-
|
|
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;
|
|
336
|
+
{categoryTicks.map((key) => {
|
|
337
|
+
const label = tickMemory.get(key) ?? key.slice(5);
|
|
338
|
+
const at = drawn[key]![0]!;
|
|
188
339
|
|
|
189
340
|
return orientation === "horizontal" ? (
|
|
190
341
|
<text
|
|
191
|
-
key={
|
|
342
|
+
key={key}
|
|
192
343
|
data-part="tick"
|
|
193
344
|
x={pad.left - 8}
|
|
194
345
|
y={at}
|
|
346
|
+
opacity={fade(key)}
|
|
195
347
|
textAnchor="end"
|
|
196
348
|
dominantBaseline="middle"
|
|
197
349
|
fill={text}
|
|
@@ -201,10 +353,11 @@ export function CategoryBarChart({
|
|
|
201
353
|
</text>
|
|
202
354
|
) : (
|
|
203
355
|
<text
|
|
204
|
-
key={
|
|
356
|
+
key={key}
|
|
205
357
|
data-part="tick"
|
|
206
358
|
x={at}
|
|
207
359
|
y={height - pad.bottom + 14}
|
|
360
|
+
opacity={fade(key)}
|
|
208
361
|
textAnchor="middle"
|
|
209
362
|
fill={text}
|
|
210
363
|
fontSize={11}
|
|
@@ -213,13 +366,14 @@ export function CategoryBarChart({
|
|
|
213
366
|
</text>
|
|
214
367
|
);
|
|
215
368
|
})}
|
|
216
|
-
{
|
|
369
|
+
{valueTicks("left").map((tick) =>
|
|
217
370
|
orientation === "horizontal" ? (
|
|
218
371
|
<text
|
|
219
|
-
key={
|
|
372
|
+
key={tick.key}
|
|
220
373
|
data-part="value-tick"
|
|
221
|
-
x={tick.at
|
|
374
|
+
x={tick.at}
|
|
222
375
|
y={height - pad.bottom + 14}
|
|
376
|
+
opacity={tick.opacity}
|
|
223
377
|
textAnchor="middle"
|
|
224
378
|
fill={text}
|
|
225
379
|
fontSize={11}
|
|
@@ -228,10 +382,11 @@ export function CategoryBarChart({
|
|
|
228
382
|
</text>
|
|
229
383
|
) : (
|
|
230
384
|
<text
|
|
231
|
-
key={
|
|
385
|
+
key={tick.key}
|
|
232
386
|
data-part="value-tick"
|
|
233
387
|
x={pad.left - 6}
|
|
234
|
-
y={tick.at
|
|
388
|
+
y={tick.at}
|
|
389
|
+
opacity={tick.opacity}
|
|
235
390
|
textAnchor="end"
|
|
236
391
|
dominantBaseline="middle"
|
|
237
392
|
fill={text}
|
|
@@ -241,21 +396,21 @@ export function CategoryBarChart({
|
|
|
241
396
|
</text>
|
|
242
397
|
),
|
|
243
398
|
)}
|
|
244
|
-
{
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
399
|
+
{valueTicks("right").map((tick) => (
|
|
400
|
+
<text
|
|
401
|
+
key={tick.key}
|
|
402
|
+
data-part="value-tick"
|
|
403
|
+
data-scale="right"
|
|
404
|
+
x={width - pad.right + 6}
|
|
405
|
+
y={tick.at}
|
|
406
|
+
opacity={tick.opacity}
|
|
407
|
+
dominantBaseline="middle"
|
|
408
|
+
fill={text}
|
|
409
|
+
fontSize={11}
|
|
410
|
+
>
|
|
411
|
+
{tickLabel(tick.value)}
|
|
412
|
+
</text>
|
|
413
|
+
))}
|
|
259
414
|
{valueLabel && (
|
|
260
415
|
<text
|
|
261
416
|
data-part="axis-label"
|
|
@@ -318,33 +473,89 @@ export function CategoryBarChart({
|
|
|
318
473
|
</g>
|
|
319
474
|
);
|
|
320
475
|
})}
|
|
321
|
-
{
|
|
476
|
+
{dot &&
|
|
477
|
+
categories.map((label, category) => {
|
|
478
|
+
const placed = frame.rects
|
|
479
|
+
.filter((rect) => rect.category === category)
|
|
480
|
+
.map((rect) => {
|
|
481
|
+
const [x, y, w, h] = drawn[`bar:${rect.series}:${label}`] ?? [];
|
|
482
|
+
|
|
483
|
+
return centre(x ?? rect.x, y ?? rect.y, w ?? rect.width, h ?? rect.height);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
if (placed.length < 2) return null;
|
|
487
|
+
|
|
488
|
+
const along = placed.map((point) => (horizontal ? point.cx : point.cy));
|
|
489
|
+
const at = horizontal ? placed[0]!.cy : placed[0]!.cx;
|
|
490
|
+
const lo = Math.min(...along);
|
|
491
|
+
const hi = Math.max(...along);
|
|
492
|
+
|
|
493
|
+
return (
|
|
494
|
+
<line
|
|
495
|
+
key={`span:${label}`}
|
|
496
|
+
data-part="dot-span"
|
|
497
|
+
data-key={label}
|
|
498
|
+
x1={horizontal ? lo : at}
|
|
499
|
+
x2={horizontal ? hi : at}
|
|
500
|
+
y1={horizontal ? at : lo}
|
|
501
|
+
y2={horizontal ? at : hi}
|
|
502
|
+
stroke={grid}
|
|
503
|
+
strokeWidth={2}
|
|
504
|
+
/>
|
|
505
|
+
);
|
|
506
|
+
})}
|
|
507
|
+
{frame.rects.map((placed) => {
|
|
508
|
+
const label = categories[placed.category];
|
|
509
|
+
const [x, y, w, h] = drawn[`bar:${placed.series}:${label}`] ?? [];
|
|
510
|
+
const rect = {
|
|
511
|
+
...placed,
|
|
512
|
+
x: x ?? placed.x,
|
|
513
|
+
y: y ?? placed.y,
|
|
514
|
+
width: w ?? placed.width,
|
|
515
|
+
height: h ?? placed.height,
|
|
516
|
+
};
|
|
322
517
|
const item = series.find((entry) => entry.id === rect.series);
|
|
323
518
|
const index = series.findIndex((entry) => entry.id === rect.series);
|
|
324
519
|
const datum = readDatum(item?.data[rect.category]);
|
|
325
520
|
|
|
326
521
|
return (
|
|
327
|
-
<g key={`${rect.series}:${
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
522
|
+
<g key={`${rect.series}:${label}`}>
|
|
523
|
+
{dot ? (
|
|
524
|
+
<circle
|
|
525
|
+
data-part="dot"
|
|
526
|
+
data-key={label}
|
|
527
|
+
data-series={rect.series}
|
|
528
|
+
data-category={rect.category}
|
|
529
|
+
data-active={rect.category === active ? "" : undefined}
|
|
530
|
+
{...centre(rect.x, rect.y, rect.width, rect.height)}
|
|
531
|
+
r={rect.category === active ? 5 : 4}
|
|
532
|
+
fill={datum?.color ?? seriesColor(item, categorical, index)}
|
|
533
|
+
onPointerEnter={() => setFocus(rect.category)}
|
|
534
|
+
onClick={() => choose(rect.category)}
|
|
535
|
+
/>
|
|
536
|
+
) : (
|
|
537
|
+
<rect
|
|
538
|
+
data-part="bar"
|
|
539
|
+
data-key={label}
|
|
540
|
+
data-series={rect.series}
|
|
541
|
+
data-category={rect.category}
|
|
542
|
+
data-active={rect.category === active ? "" : undefined}
|
|
543
|
+
x={rect.x}
|
|
544
|
+
y={rect.y}
|
|
545
|
+
width={rect.width}
|
|
546
|
+
height={Math.max(rect.height, rect.value === 0 ? 0 : 1)}
|
|
547
|
+
fill={datum?.color ?? seriesColor(item, categorical, index)}
|
|
548
|
+
opacity={mode === "overlay" ? 0.72 : 1}
|
|
549
|
+
onPointerEnter={() => setFocus(rect.category)}
|
|
550
|
+
onClick={() => choose(rect.category)}
|
|
551
|
+
/>
|
|
552
|
+
)}
|
|
342
553
|
{datum?.label && (
|
|
343
554
|
<text
|
|
344
555
|
data-part="bar-label"
|
|
345
556
|
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}
|
|
557
|
+
x={orientation === "horizontal" ? rect.x + rect.width + (dot ? 8 : 4) : rect.x + rect.width / 2}
|
|
558
|
+
y={orientation === "horizontal" ? rect.y + rect.height / 2 : rect.y - (dot ? 8 : 4)}
|
|
348
559
|
textAnchor={orientation === "horizontal" ? "start" : "middle"}
|
|
349
560
|
dominantBaseline={orientation === "horizontal" ? "middle" : "auto"}
|
|
350
561
|
fill={text}
|
|
@@ -356,6 +567,43 @@ export function CategoryBarChart({
|
|
|
356
567
|
</g>
|
|
357
568
|
);
|
|
358
569
|
})}
|
|
570
|
+
{leavingBars.map((key) => {
|
|
571
|
+
const [x, y, w, h] = drawn[key]!;
|
|
572
|
+
const gone = memory.current.get(key);
|
|
573
|
+
|
|
574
|
+
if (dot) {
|
|
575
|
+
return (
|
|
576
|
+
<circle
|
|
577
|
+
key={key}
|
|
578
|
+
data-part="dot"
|
|
579
|
+
data-leaving=""
|
|
580
|
+
data-key={gone?.label}
|
|
581
|
+
data-series={gone?.series}
|
|
582
|
+
{...centre(x!, y!, w!, h!)}
|
|
583
|
+
r={4}
|
|
584
|
+
fill={gone?.color}
|
|
585
|
+
pointerEvents="none"
|
|
586
|
+
/>
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
return (
|
|
591
|
+
<rect
|
|
592
|
+
key={key}
|
|
593
|
+
data-part="bar"
|
|
594
|
+
data-leaving=""
|
|
595
|
+
data-key={gone?.label}
|
|
596
|
+
data-series={gone?.series}
|
|
597
|
+
x={x}
|
|
598
|
+
y={y}
|
|
599
|
+
width={Math.max(0, w!)}
|
|
600
|
+
height={Math.max(0, h!)}
|
|
601
|
+
fill={gone?.color}
|
|
602
|
+
opacity={mode === "overlay" ? 0.72 : 1}
|
|
603
|
+
pointerEvents="none"
|
|
604
|
+
/>
|
|
605
|
+
);
|
|
606
|
+
})}
|
|
359
607
|
</svg>
|
|
360
608
|
{category !== undefined &&
|
|
361
609
|
(renderTooltip?.(category, rows) ?? (
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
import { useRef } from "react";
|
|
8
8
|
import { SR_ONLY } from "../../styles/shared.js";
|
|
9
9
|
import { classes } from "../../utils/dom.utils.js";
|
|
10
|
+
import { cssTransition } from "../../utils/motion.utils.js";
|
|
10
11
|
import { useHeatmap } from "../../hooks/useHeatmap.js";
|
|
11
12
|
import { isDimmed } from "../../utils/heatmap.utils.js";
|
|
12
13
|
import type { HeatmapProps } from "../../types/heatmap.js";
|
|
@@ -38,7 +39,9 @@ export function Heatmap<R, C>(props: HeatmapProps<R, C>) {
|
|
|
38
39
|
style,
|
|
39
40
|
classNames,
|
|
40
41
|
styles,
|
|
42
|
+
animate = true,
|
|
41
43
|
} = props;
|
|
44
|
+
const transition = cssTransition(animate, ["fill", "opacity"]);
|
|
42
45
|
const map = useHeatmap(props);
|
|
43
46
|
const { layout } = map;
|
|
44
47
|
const rootRef = useRef<HTMLDivElement>(null);
|
|
@@ -107,6 +110,7 @@ export function Heatmap<R, C>(props: HeatmapProps<R, C>) {
|
|
|
107
110
|
selectable={selectionMode !== "none"}
|
|
108
111
|
stop={state.stop}
|
|
109
112
|
handlers={handlers}
|
|
113
|
+
transition={transition}
|
|
110
114
|
/>
|
|
111
115
|
<HeatmapAxis
|
|
112
116
|
{...shared}
|