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
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { useId, type CSSProperties } from "react";
|
|
2
|
+
import type { ChartStoryPart, ChartStoryProps } from "../../types/story.js";
|
|
3
|
+
import { useSteps } from "../../hooks/useSteps.js";
|
|
4
|
+
import { useChartTheme } from "../../hooks/useChartTheme.js";
|
|
5
|
+
import { classes } from "../../utils/dom.utils.js";
|
|
6
|
+
|
|
7
|
+
const defaultLabel = (_step: unknown, index: number) => `Step ${index + 1}`;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A chart that steps through states, like slides or a time slider. The chart
|
|
11
|
+
* stays mounted between steps, so its `animate` motion carries the change.
|
|
12
|
+
*/
|
|
13
|
+
export function ChartStory<S>({
|
|
14
|
+
steps: list,
|
|
15
|
+
children,
|
|
16
|
+
caption,
|
|
17
|
+
controls = "dots",
|
|
18
|
+
stepLabel = defaultLabel,
|
|
19
|
+
ariaLabel = "Chart story",
|
|
20
|
+
className,
|
|
21
|
+
style,
|
|
22
|
+
classNames,
|
|
23
|
+
styles,
|
|
24
|
+
theme,
|
|
25
|
+
unstyled,
|
|
26
|
+
...options
|
|
27
|
+
}: ChartStoryProps<S>) {
|
|
28
|
+
const steps = useSteps({ ...options, count: list.length });
|
|
29
|
+
const resolved = useChartTheme({ theme, unstyled });
|
|
30
|
+
const captionId = useId();
|
|
31
|
+
const step = list[steps.index];
|
|
32
|
+
const text = resolved?.palette.text;
|
|
33
|
+
const border = resolved?.palette.border;
|
|
34
|
+
const look = (part: ChartStoryPart, base?: CSSProperties) =>
|
|
35
|
+
resolved ? { ...base, ...styles?.[part] } : styles?.[part];
|
|
36
|
+
const button: CSSProperties = {
|
|
37
|
+
font: "inherit",
|
|
38
|
+
color: text,
|
|
39
|
+
background: "transparent",
|
|
40
|
+
border: `1px solid ${border}`,
|
|
41
|
+
padding: "2px 10px",
|
|
42
|
+
cursor: "pointer",
|
|
43
|
+
};
|
|
44
|
+
const label = step === undefined ? "" : stepLabel(step, steps.index);
|
|
45
|
+
|
|
46
|
+
if (step === undefined) return null;
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<section
|
|
50
|
+
data-part="root"
|
|
51
|
+
aria-roledescription="carousel"
|
|
52
|
+
aria-label={ariaLabel}
|
|
53
|
+
className={classes(className, classNames?.root)}
|
|
54
|
+
style={{ ...look("root", { color: text }), ...style }}
|
|
55
|
+
>
|
|
56
|
+
{caption && (
|
|
57
|
+
<div
|
|
58
|
+
id={captionId}
|
|
59
|
+
data-part="caption"
|
|
60
|
+
aria-live={steps.playing ? "off" : "polite"}
|
|
61
|
+
className={classNames?.caption}
|
|
62
|
+
style={look("caption", { marginBottom: 8 })}
|
|
63
|
+
>
|
|
64
|
+
{caption(step, steps.index)}
|
|
65
|
+
</div>
|
|
66
|
+
)}
|
|
67
|
+
<div
|
|
68
|
+
data-part="stage"
|
|
69
|
+
role="group"
|
|
70
|
+
aria-roledescription="slide"
|
|
71
|
+
aria-label={`${label}, ${steps.index + 1} of ${steps.count}`}
|
|
72
|
+
aria-describedby={caption ? captionId : undefined}
|
|
73
|
+
className={classNames?.stage}
|
|
74
|
+
style={styles?.stage}
|
|
75
|
+
>
|
|
76
|
+
{children(step, steps.index, steps)}
|
|
77
|
+
</div>
|
|
78
|
+
{controls !== "none" && steps.count > 1 && (
|
|
79
|
+
<div
|
|
80
|
+
data-part="controls"
|
|
81
|
+
className={classNames?.controls}
|
|
82
|
+
style={look("controls", {
|
|
83
|
+
display: "flex",
|
|
84
|
+
alignItems: "center",
|
|
85
|
+
gap: 8,
|
|
86
|
+
marginTop: 8,
|
|
87
|
+
flexWrap: "wrap",
|
|
88
|
+
})}
|
|
89
|
+
>
|
|
90
|
+
<button
|
|
91
|
+
type="button"
|
|
92
|
+
data-part="play"
|
|
93
|
+
aria-pressed={steps.playing}
|
|
94
|
+
onClick={steps.toggle}
|
|
95
|
+
className={classNames?.play}
|
|
96
|
+
style={look("play", button)}
|
|
97
|
+
>
|
|
98
|
+
{steps.playing ? "Pause" : "Play"}
|
|
99
|
+
</button>
|
|
100
|
+
<button
|
|
101
|
+
type="button"
|
|
102
|
+
data-part="prev"
|
|
103
|
+
aria-label="Previous step"
|
|
104
|
+
disabled={steps.isFirst && !options.loop}
|
|
105
|
+
onClick={steps.prev}
|
|
106
|
+
className={classNames?.prev}
|
|
107
|
+
style={look("prev", button)}
|
|
108
|
+
>
|
|
109
|
+
‹
|
|
110
|
+
</button>
|
|
111
|
+
{controls === "slider" ? (
|
|
112
|
+
<input
|
|
113
|
+
type="range"
|
|
114
|
+
data-part="slider"
|
|
115
|
+
min={0}
|
|
116
|
+
max={steps.count - 1}
|
|
117
|
+
step={1}
|
|
118
|
+
value={steps.index}
|
|
119
|
+
aria-label={ariaLabel}
|
|
120
|
+
aria-valuetext={label}
|
|
121
|
+
onChange={(event) => {
|
|
122
|
+
steps.pause();
|
|
123
|
+
steps.goTo(Number(event.currentTarget.value));
|
|
124
|
+
}}
|
|
125
|
+
className={classNames?.slider}
|
|
126
|
+
style={look("slider", { flex: "1 1 120px", accentColor: text })}
|
|
127
|
+
/>
|
|
128
|
+
) : (
|
|
129
|
+
<div
|
|
130
|
+
data-part="dots"
|
|
131
|
+
role="group"
|
|
132
|
+
aria-label="Steps"
|
|
133
|
+
className={classNames?.dots}
|
|
134
|
+
style={look("dots", { display: "flex", gap: 4 })}
|
|
135
|
+
>
|
|
136
|
+
{list.map((item, index) => (
|
|
137
|
+
<button
|
|
138
|
+
key={index}
|
|
139
|
+
type="button"
|
|
140
|
+
data-part="dot"
|
|
141
|
+
aria-label={stepLabel(item, index)}
|
|
142
|
+
aria-current={index === steps.index ? "step" : undefined}
|
|
143
|
+
onClick={() => {
|
|
144
|
+
steps.pause();
|
|
145
|
+
steps.goTo(index);
|
|
146
|
+
}}
|
|
147
|
+
className={classNames?.dot}
|
|
148
|
+
style={look("dot", {
|
|
149
|
+
width: 10,
|
|
150
|
+
height: 10,
|
|
151
|
+
padding: 0,
|
|
152
|
+
borderRadius: "50%",
|
|
153
|
+
border: `1px solid ${text}`,
|
|
154
|
+
background: index === steps.index ? text : "transparent",
|
|
155
|
+
cursor: "pointer",
|
|
156
|
+
})}
|
|
157
|
+
/>
|
|
158
|
+
))}
|
|
159
|
+
</div>
|
|
160
|
+
)}
|
|
161
|
+
<button
|
|
162
|
+
type="button"
|
|
163
|
+
data-part="next"
|
|
164
|
+
aria-label="Next step"
|
|
165
|
+
disabled={steps.isLast && !options.loop}
|
|
166
|
+
onClick={steps.next}
|
|
167
|
+
className={classNames?.next}
|
|
168
|
+
style={look("next", button)}
|
|
169
|
+
>
|
|
170
|
+
›
|
|
171
|
+
</button>
|
|
172
|
+
<span
|
|
173
|
+
data-part="position"
|
|
174
|
+
aria-hidden="true"
|
|
175
|
+
className={classNames?.position}
|
|
176
|
+
style={look("position", { fontVariantNumeric: "tabular-nums" })}
|
|
177
|
+
>
|
|
178
|
+
{label}
|
|
179
|
+
</span>
|
|
180
|
+
</div>
|
|
181
|
+
)}
|
|
182
|
+
</section>
|
|
183
|
+
);
|
|
184
|
+
}
|
|
@@ -22,6 +22,7 @@ export function HeatmapCells<R, C>({
|
|
|
22
22
|
selectable,
|
|
23
23
|
stop,
|
|
24
24
|
handlers,
|
|
25
|
+
transition,
|
|
25
26
|
classNames,
|
|
26
27
|
styles,
|
|
27
28
|
}: HeatmapCellsProps<R, C>) {
|
|
@@ -91,7 +92,9 @@ export function HeatmapCells<R, C>({
|
|
|
91
92
|
opacity={fade(cell)}
|
|
92
93
|
className={classNames?.cell}
|
|
93
94
|
style={
|
|
94
|
-
theme
|
|
95
|
+
theme
|
|
96
|
+
? { outline: "none", transition, ...styles?.cell }
|
|
97
|
+
: styles?.cell
|
|
95
98
|
}
|
|
96
99
|
onFocus={(event) => handlers.focus(cell, event.currentTarget)}
|
|
97
100
|
onBlur={handlers.blur}
|
|
@@ -125,7 +128,7 @@ export function HeatmapCells<R, C>({
|
|
|
125
128
|
theme ? (ink(cell.step) ?? theme.palette.text) : undefined
|
|
126
129
|
}
|
|
127
130
|
className={classNames?.value}
|
|
128
|
-
style={{ ...HEATMAP_TEXT, ...styles?.value }}
|
|
131
|
+
style={{ ...HEATMAP_TEXT, ...(theme && { transition }), ...styles?.value }}
|
|
129
132
|
>
|
|
130
133
|
{text}
|
|
131
134
|
</text>
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef } from "react";
|
|
2
|
+
import type { MotionFrames } from "../types/motion.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a chart last knew about each mark (its colour, series, angles), so a
|
|
6
|
+
* mark that left the data can still be drawn while `useTween` moves it out.
|
|
7
|
+
* A key is forgotten once it is in neither this frame nor the last committed
|
|
8
|
+
* one, which keeps it for the render that starts its exit.
|
|
9
|
+
*/
|
|
10
|
+
export function useMarkMemory<T>(
|
|
11
|
+
current: ReadonlyMap<string, T>,
|
|
12
|
+
drawn: MotionFrames,
|
|
13
|
+
): ReadonlyMap<string, T> {
|
|
14
|
+
const memory = useRef(new Map<string, T>());
|
|
15
|
+
const committed = useRef<MotionFrames>(drawn);
|
|
16
|
+
|
|
17
|
+
current.forEach((value, key) => memory.current.set(key, value));
|
|
18
|
+
for (const key of memory.current.keys()) {
|
|
19
|
+
if (!(key in drawn) && !(key in committed.current) && !current.has(key)) {
|
|
20
|
+
memory.current.delete(key);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
useLayoutEffect(() => {
|
|
25
|
+
committed.current = drawn;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return memory.current;
|
|
29
|
+
}
|
package/src/hooks/usePieTween.ts
CHANGED
|
@@ -1,56 +1,21 @@
|
|
|
1
|
-
import type { PieDatum, PieSlice, PieAnimate } from "../types/pie.js";
|
|
1
|
+
import type { PieDatum, PieSlice, PieAnimate, PieAngles } from "../types/pie.js";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
applyAngles,
|
|
7
|
-
tweenAngles,
|
|
8
|
-
easeInOutCubic,
|
|
9
|
-
} from "../utils/pieTween.utils.js";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { useTween } from "./useTween.js";
|
|
5
|
+
import { applyAngles, pieAngles } from "../utils/pieTween.utils.js";
|
|
10
6
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const DURATION = 240;
|
|
7
|
+
/** A new slice opens from its own start angle. */
|
|
8
|
+
const fromStart = (_key: string, [start]: readonly number[]) => [start!, start!];
|
|
15
9
|
|
|
16
10
|
export function usePieTween<T extends PieDatum>(
|
|
17
11
|
slices: PieSlice<T>[],
|
|
18
12
|
animate: PieAnimate = true,
|
|
19
13
|
) {
|
|
20
|
-
const { duration = DURATION, easing = easeInOutCubic } =
|
|
21
|
-
typeof animate === "object" ? animate : {};
|
|
22
14
|
const target = useMemo(() => pieAngles(slices), [slices]);
|
|
23
|
-
const
|
|
24
|
-
const [frame, setFrame] = useState<typeof target | null>(null);
|
|
25
|
-
|
|
26
|
-
useLayoutEffect(() => {
|
|
27
|
-
const from = shown.current;
|
|
28
|
-
|
|
29
|
-
if (sameAngles(from, target)) return;
|
|
30
|
-
if (animate === false || duration <= 0 || prefersReducedMotion()) {
|
|
31
|
-
shown.current = target;
|
|
32
|
-
setFrame(null);
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const begin = performance.now();
|
|
37
|
-
let request = 0;
|
|
38
|
-
const step = () => {
|
|
39
|
-
const t = Math.min(1, (performance.now() - begin) / duration);
|
|
40
|
-
const next = tweenAngles(from, target, easing(t));
|
|
41
|
-
|
|
42
|
-
shown.current = next;
|
|
43
|
-
setFrame(t < 1 ? next : null);
|
|
44
|
-
if (t < 1) request = requestAnimationFrame(step);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
setFrame(tweenAngles(from, target, 0));
|
|
48
|
-
request = requestAnimationFrame(step);
|
|
49
|
-
return () => cancelAnimationFrame(request);
|
|
50
|
-
}, [target, animate, duration, easing]);
|
|
15
|
+
const frame = useTween(target, animate, { enter: fromStart });
|
|
51
16
|
|
|
52
17
|
return useMemo(
|
|
53
|
-
() => (frame ? applyAngles(slices, frame
|
|
54
|
-
[frame, slices],
|
|
18
|
+
() => (frame === target ? slices : applyAngles(slices, frame as PieAngles)),
|
|
19
|
+
[frame, target, slices],
|
|
55
20
|
);
|
|
56
21
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import type { Steps, StepsOptions } from "../types/story.js";
|
|
3
|
+
import { useControllable } from "./useControllable.js";
|
|
4
|
+
import { prefersReducedMotion } from "../utils/motion.utils.js";
|
|
5
|
+
|
|
6
|
+
const INTERVAL = 3000;
|
|
7
|
+
|
|
8
|
+
const clampStep = (index: number, count: number) =>
|
|
9
|
+
Math.max(0, Math.min(Math.max(0, count - 1), Math.round(index)));
|
|
10
|
+
|
|
11
|
+
/** Step through chart states, by hand or on a timer. */
|
|
12
|
+
export function useSteps({
|
|
13
|
+
count,
|
|
14
|
+
index: controlled,
|
|
15
|
+
defaultIndex = 0,
|
|
16
|
+
onIndexChange,
|
|
17
|
+
autoplay = false,
|
|
18
|
+
interval = INTERVAL,
|
|
19
|
+
loop = false,
|
|
20
|
+
}: StepsOptions): Steps {
|
|
21
|
+
const [raw, setOwn] = useControllable(controlled, defaultIndex);
|
|
22
|
+
const index = clampStep(raw, count);
|
|
23
|
+
const [playing, setPlaying] = useState(
|
|
24
|
+
() => autoplay && count > 1 && !prefersReducedMotion(),
|
|
25
|
+
);
|
|
26
|
+
const isLast = index >= count - 1;
|
|
27
|
+
|
|
28
|
+
const goTo = useCallback(
|
|
29
|
+
(next: number) => {
|
|
30
|
+
const target = clampStep(next, count);
|
|
31
|
+
|
|
32
|
+
setOwn(target);
|
|
33
|
+
if (target !== index) onIndexChange?.(target);
|
|
34
|
+
},
|
|
35
|
+
[count, index, onIndexChange, setOwn],
|
|
36
|
+
);
|
|
37
|
+
const next = useCallback(
|
|
38
|
+
() => goTo(isLast && loop ? 0 : index + 1),
|
|
39
|
+
[goTo, index, isLast, loop],
|
|
40
|
+
);
|
|
41
|
+
const prev = useCallback(
|
|
42
|
+
() => goTo(index === 0 && loop ? count - 1 : index - 1),
|
|
43
|
+
[count, goTo, index, loop],
|
|
44
|
+
);
|
|
45
|
+
const play = useCallback(() => {
|
|
46
|
+
if (count < 2) return;
|
|
47
|
+
if (isLast && !loop) goTo(0);
|
|
48
|
+
setPlaying(true);
|
|
49
|
+
}, [count, goTo, isLast, loop]);
|
|
50
|
+
const pause = useCallback(() => setPlaying(false), []);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (!playing) return;
|
|
54
|
+
if (isLast && !loop) {
|
|
55
|
+
setPlaying(false);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const timer = setTimeout(next, Math.max(16, interval));
|
|
60
|
+
|
|
61
|
+
return () => clearTimeout(timer);
|
|
62
|
+
}, [playing, index, isLast, loop, interval, next]);
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
index,
|
|
66
|
+
count,
|
|
67
|
+
playing,
|
|
68
|
+
isFirst: index === 0,
|
|
69
|
+
isLast,
|
|
70
|
+
goTo,
|
|
71
|
+
next,
|
|
72
|
+
prev,
|
|
73
|
+
play,
|
|
74
|
+
pause,
|
|
75
|
+
toggle: playing ? pause : play,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef, useState } from "react";
|
|
2
|
+
import type { ChartAnimate, MotionFrames, TweenMarks } from "../types/motion.js";
|
|
3
|
+
import {
|
|
4
|
+
motionSettings,
|
|
5
|
+
prefersReducedMotion,
|
|
6
|
+
sameFrames,
|
|
7
|
+
staggeredProgress,
|
|
8
|
+
totalDuration,
|
|
9
|
+
tweenFrames,
|
|
10
|
+
} from "../utils/motion.utils.js";
|
|
11
|
+
|
|
12
|
+
/** Keeps the previous object while the values are equal, so effects key on content. */
|
|
13
|
+
function useStable(target: MotionFrames): MotionFrames {
|
|
14
|
+
const ref = useRef(target);
|
|
15
|
+
|
|
16
|
+
if (ref.current !== target && !sameFrames(ref.current, target)) {
|
|
17
|
+
ref.current = target;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return ref.current;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** `target` plus each key that left it, parked at its exit value. */
|
|
24
|
+
function withLeaving(
|
|
25
|
+
from: MotionFrames,
|
|
26
|
+
target: MotionFrames,
|
|
27
|
+
exit: TweenMarks["exit"],
|
|
28
|
+
): MotionFrames {
|
|
29
|
+
if (!exit) return target;
|
|
30
|
+
|
|
31
|
+
const out: Record<string, readonly number[]> = { ...target };
|
|
32
|
+
|
|
33
|
+
Object.keys(from).forEach((key) => {
|
|
34
|
+
if (!(key in target)) out[key] = exit(key, from[key]!);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The frame to draw while `target` changes. Marks are matched by key. A new key
|
|
42
|
+
* grows from `enter`. A removed key tweens to `exit` and is then dropped, or is
|
|
43
|
+
* dropped at once without `exit`. While a key is leaving it is still in the
|
|
44
|
+
* returned frame, so draw it from what you remember about it.
|
|
45
|
+
*
|
|
46
|
+
* The first render is final unless `appear` is set. Reduced motion and
|
|
47
|
+
* `animate={false}` jump straight to `target`.
|
|
48
|
+
*/
|
|
49
|
+
export function useTween(
|
|
50
|
+
target: MotionFrames,
|
|
51
|
+
animate: ChartAnimate = true,
|
|
52
|
+
marks: TweenMarks = {},
|
|
53
|
+
): MotionFrames {
|
|
54
|
+
const stable = useStable(target);
|
|
55
|
+
const settings = motionSettings(animate);
|
|
56
|
+
const marksRef = useRef(marks);
|
|
57
|
+
marksRef.current = marks;
|
|
58
|
+
const shown = useRef<MotionFrames | null>(null);
|
|
59
|
+
const [frame, setFrame] = useState<MotionFrames | null>(() =>
|
|
60
|
+
settings.appear && settings.duration > 0 && !prefersReducedMotion()
|
|
61
|
+
? tweenFrames({}, stable, 0, marks.enter)
|
|
62
|
+
: null,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
if (shown.current === null) shown.current = frame ?? stable;
|
|
66
|
+
|
|
67
|
+
const { duration, stagger } = settings;
|
|
68
|
+
const easingRef = useRef(settings.easing);
|
|
69
|
+
easingRef.current = settings.easing;
|
|
70
|
+
|
|
71
|
+
useLayoutEffect(() => {
|
|
72
|
+
const from = shown.current ?? stable;
|
|
73
|
+
|
|
74
|
+
if (sameFrames(from, stable) || duration <= 0 || prefersReducedMotion()) {
|
|
75
|
+
shown.current = stable;
|
|
76
|
+
setFrame(null);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const { enter, exit } = marksRef.current;
|
|
81
|
+
const to = withLeaving(from, stable, exit);
|
|
82
|
+
const timing = { duration, easing: easingRef.current, stagger };
|
|
83
|
+
const total = totalDuration(Object.keys(to).length, timing);
|
|
84
|
+
const begin = performance.now();
|
|
85
|
+
let request = 0;
|
|
86
|
+
const step = () => {
|
|
87
|
+
const elapsed = performance.now() - begin;
|
|
88
|
+
const done = elapsed >= total;
|
|
89
|
+
const next = done
|
|
90
|
+
? stable
|
|
91
|
+
: tweenFrames(
|
|
92
|
+
from,
|
|
93
|
+
to,
|
|
94
|
+
(_key, index) => staggeredProgress(elapsed, index, timing),
|
|
95
|
+
enter,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
shown.current = next;
|
|
99
|
+
setFrame(done ? null : next);
|
|
100
|
+
if (!done) request = requestAnimationFrame(step);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const first = tweenFrames(from, to, 0, enter);
|
|
104
|
+
shown.current = first;
|
|
105
|
+
setFrame(first);
|
|
106
|
+
request = requestAnimationFrame(step);
|
|
107
|
+
|
|
108
|
+
return () => cancelAnimationFrame(request);
|
|
109
|
+
}, [stable, duration, stagger]);
|
|
110
|
+
|
|
111
|
+
return frame ?? stable;
|
|
112
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -238,3 +238,22 @@ export type { ScatterChartProps, ScatterSeries } from "./types/scatter.js";
|
|
|
238
238
|
export type { PolarChartProps } from "./types/polar.js";
|
|
239
239
|
export type { SparklineProps } from "./types/sparkline.js";
|
|
240
240
|
export type { Mode } from "./themes/p1.js";
|
|
241
|
+
export { ChartStory } from "./components/ChartStory/index.js";
|
|
242
|
+
export { ChartFrame } from "./components/ChartFrame/index.js";
|
|
243
|
+
export { useSteps } from "./hooks/useSteps.js";
|
|
244
|
+
export { useTween } from "./hooks/useTween.js";
|
|
245
|
+
export { useMarkMemory } from "./hooks/useMarkMemory.js";
|
|
246
|
+
export { toCsv, downloadCsv } from "./utils/csv.utils.js";
|
|
247
|
+
export { readableInk, contrastRatio } from "./utils/color.utils.js";
|
|
248
|
+
export { easeInOutCubic, easeOutCubic, linear } from "./utils/motion.utils.js";
|
|
249
|
+
export type {
|
|
250
|
+
Steps,
|
|
251
|
+
FrameData,
|
|
252
|
+
StepsOptions,
|
|
253
|
+
ChartFramePart,
|
|
254
|
+
ChartFrameProps,
|
|
255
|
+
ChartStoryPart,
|
|
256
|
+
ChartStoryProps,
|
|
257
|
+
} from "./types/story.js";
|
|
258
|
+
export type { ChartAnimate, Easing, MotionFrames, MotionOptions, TweenMarks } from "./types/motion.js";
|
|
259
|
+
export type { CategorySort } from "./types/category.js";
|
package/src/sparkline.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Sparkline } from "./charts/Sparkline/index.js";
|
|
2
|
-
export { sparklinePaths } from "./utils/sparkline.utils.js";
|
|
2
|
+
export { sparklinePaths, sparklineShapes, shiftOffset } from "./utils/sparkline.utils.js";
|
|
3
3
|
|
|
4
4
|
export type { SparklineProps } from "./types/sparkline.js";
|
|
5
5
|
export type { SparklineDatum } from "./utils/sparkline.utils.js";
|
package/src/story.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export { ChartStory } from "./components/ChartStory/index.js";
|
|
2
|
+
export { ChartFrame } from "./components/ChartFrame/index.js";
|
|
3
|
+
export { useSteps } from "./hooks/useSteps.js";
|
|
4
|
+
export { useTween } from "./hooks/useTween.js";
|
|
5
|
+
export { useMarkMemory } from "./hooks/useMarkMemory.js";
|
|
6
|
+
export { toCsv, downloadCsv } from "./utils/csv.utils.js";
|
|
7
|
+
export { readableInk, contrastRatio } from "./utils/color.utils.js";
|
|
8
|
+
export { easeInOutCubic, easeOutCubic, linear } from "./utils/motion.utils.js";
|
|
9
|
+
|
|
10
|
+
export type {
|
|
11
|
+
Steps,
|
|
12
|
+
FrameData,
|
|
13
|
+
StepsOptions,
|
|
14
|
+
ChartFramePart,
|
|
15
|
+
ChartFrameProps,
|
|
16
|
+
ChartStoryPart,
|
|
17
|
+
ChartStoryProps,
|
|
18
|
+
} from "./types/story.js";
|
|
19
|
+
export type { ChartAnimate, Easing, MotionFrames, MotionOptions, TweenMarks } from "./types/motion.js";
|
package/src/types/category.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
CategoryScale,
|
|
7
7
|
} from "../utils/category.utils.js";
|
|
8
8
|
import type { ThemeProps } from "./theme.js";
|
|
9
|
+
import type { ChartAnimate } from "./motion.js";
|
|
9
10
|
|
|
10
11
|
/** A bar. A number is the value. An object can colour and label that one bar. */
|
|
11
12
|
export type CategoryBarDatum =
|
|
@@ -72,5 +73,15 @@ export interface CategoryBarChartProps extends ThemeProps {
|
|
|
72
73
|
category: string,
|
|
73
74
|
rows: readonly CategoryTooltipRow[],
|
|
74
75
|
) => ReactNode;
|
|
76
|
+
/** Index is the position in `categories`, whatever `sort` does. */
|
|
75
77
|
onSelect?: (category: string, index: number) => void;
|
|
78
|
+
/**
|
|
79
|
+
* Order categories by their total across series. With `animate`, bars glide
|
|
80
|
+
* to their new rank when the data changes: a racing bar chart.
|
|
81
|
+
*/
|
|
82
|
+
sort?: CategorySort;
|
|
83
|
+
/** Tween bars between data states. Default true. Reduced motion jumps. */
|
|
84
|
+
animate?: ChartAnimate;
|
|
76
85
|
}
|
|
86
|
+
|
|
87
|
+
export type CategorySort = "none" | "ascending" | "descending";
|
package/src/types/heatmap.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ChartAnimate } from "./motion.js";
|
|
1
2
|
import type { ThemeProps } from "./theme.js";
|
|
2
3
|
import type { QuantizeScale } from "./layout.js";
|
|
3
4
|
import type { CSSProperties, ReactNode } from "react";
|
|
@@ -155,6 +156,8 @@ export interface HeatmapProps<R = unknown, C = unknown> extends ThemeProps {
|
|
|
155
156
|
onCellHover?: (cell: HeatmapCell<R, C>) => void;
|
|
156
157
|
onCellLeave?: (cell: HeatmapCell<R, C>) => void;
|
|
157
158
|
ariaLabel?: string;
|
|
159
|
+
/** Fade cell colours between data states. Default true. Reduced motion jumps. Unstyled leaves motion to your CSS. */
|
|
160
|
+
animate?: ChartAnimate;
|
|
158
161
|
describeCell?: (cell: HeatmapCell<R, C>) => string;
|
|
159
162
|
className?: string;
|
|
160
163
|
style?: CSSProperties;
|
|
@@ -37,6 +37,8 @@ export interface HeatmapCellsProps<R, C> extends HeatmapPartsBase<R, C> {
|
|
|
37
37
|
selectable: boolean;
|
|
38
38
|
stop: HeatmapPosition;
|
|
39
39
|
handlers: HeatmapCellHandlers;
|
|
40
|
+
/** CSS transition for cell fill and opacity. */
|
|
41
|
+
transition?: string;
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
export interface HeatmapCellHandlers {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type Easing = (t: number) => number;
|
|
2
|
+
|
|
3
|
+
export interface MotionOptions {
|
|
4
|
+
/** Milliseconds per mark. Default 240. */
|
|
5
|
+
duration?: number;
|
|
6
|
+
easing?: Easing;
|
|
7
|
+
/** Delay between successive marks, in milliseconds. Default 0. */
|
|
8
|
+
stagger?: number;
|
|
9
|
+
/** Grow in from the baseline on first mount. Default false: the first frame is final. */
|
|
10
|
+
appear?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Motion between data states. `true` (the default) tweens marks when data
|
|
15
|
+
* changes, `false` jumps. Reduced motion always jumps.
|
|
16
|
+
*/
|
|
17
|
+
export type ChartAnimate = boolean | MotionOptions;
|
|
18
|
+
|
|
19
|
+
export interface MotionSettings {
|
|
20
|
+
duration: number;
|
|
21
|
+
easing: Easing;
|
|
22
|
+
stagger: number;
|
|
23
|
+
appear: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Numbers to tween, keyed by a stable mark id such as `series:category`. */
|
|
27
|
+
export type MotionFrames = Readonly<Record<string, readonly number[]>>;
|
|
28
|
+
|
|
29
|
+
/** How marks come and go in `useTween`. */
|
|
30
|
+
export interface TweenMarks {
|
|
31
|
+
/** Where a new key starts. Default: at its target. */
|
|
32
|
+
enter?: (key: string, value: readonly number[]) => readonly number[];
|
|
33
|
+
/** Where a removed key goes before it is dropped. Without it, the key is dropped at once. */
|
|
34
|
+
exit?: (key: string, value: readonly number[]) => readonly number[];
|
|
35
|
+
}
|
package/src/types/pie.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ChartAnimate } from "./motion.js";
|
|
1
2
|
import type { PieArc } from "./layout.js";
|
|
2
3
|
import type { ThemeProps } from "./theme.js";
|
|
3
4
|
import type { CSSProperties, ReactNode } from "react";
|
|
@@ -29,9 +30,8 @@ export type PieSelectionMode = "none" | "single" | "multiple";
|
|
|
29
30
|
export type PiePoint = [number, number];
|
|
30
31
|
export type PieBox = [number, number, number, number];
|
|
31
32
|
export type PieAngles = Record<string, [number, number]>;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
| { duration?: number; easing?: (t: number) => number };
|
|
33
|
+
/** Same as `ChartAnimate`. Kept as a name for existing imports. */
|
|
34
|
+
export type PieAnimate = ChartAnimate;
|
|
35
35
|
|
|
36
36
|
export interface PieLabelLayout<T extends PieDatum = PieDatum> {
|
|
37
37
|
slice: PieSlice<T>;
|
package/src/types/polar.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
2
|
import type { PolarDirection } from "../utils/polar.utils.js";
|
|
3
3
|
import type { ThemeProps } from "./theme.js";
|
|
4
|
+
import type { ChartAnimate } from "./motion.js";
|
|
4
5
|
|
|
5
6
|
export interface PolarBarSeries {
|
|
6
7
|
id: string;
|
|
@@ -23,6 +24,8 @@ interface PolarCommon extends ThemeProps {
|
|
|
23
24
|
ariaLabel?: string;
|
|
24
25
|
direction?: PolarDirection;
|
|
25
26
|
unit?: string;
|
|
27
|
+
/** Tween wedges, vertices and points between data states. Default true. */
|
|
28
|
+
animate?: ChartAnimate;
|
|
26
29
|
renderTooltip?: (row: { series: string; label: string; value: string }) => ReactNode;
|
|
27
30
|
}
|
|
28
31
|
|
package/src/types/scatter.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
2
|
import type { ThemeProps } from "./theme.js";
|
|
3
|
+
import type { ChartAnimate } from "./motion.js";
|
|
3
4
|
|
|
4
5
|
export interface ScatterPoint {
|
|
5
6
|
x: number;
|
|
@@ -29,6 +30,8 @@ export interface ScatterChartProps extends ThemeProps {
|
|
|
29
30
|
ariaLabel?: string;
|
|
30
31
|
xLabel?: string;
|
|
31
32
|
yLabel?: string;
|
|
33
|
+
/** Tween points between data states, matched by series and index. Default true. */
|
|
34
|
+
animate?: ChartAnimate;
|
|
32
35
|
onSelect?: (point: {
|
|
33
36
|
series: ScatterSeries;
|
|
34
37
|
point: ScatterPoint;
|