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
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { CSSProperties } from "react";
|
|
2
|
+
|
|
3
|
+
export interface ChartLegendItem {
|
|
4
|
+
label: string;
|
|
5
|
+
color: string;
|
|
6
|
+
/** Draw the swatch as a dashed line, for a reference such as a diagonal. */
|
|
7
|
+
dashed?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const ROW: CSSProperties = { display: "flex", flexWrap: "wrap", gap: "4px 14px", fontSize: 11, marginBottom: 6 };
|
|
11
|
+
const ITEM: CSSProperties = { display: "inline-flex", alignItems: "center", gap: 5 };
|
|
12
|
+
|
|
13
|
+
/** Swatch and name per item, above a chart. `data-part="legend"`, items `legend-item`, swatches `swatch`. */
|
|
14
|
+
export function ChartLegend({
|
|
15
|
+
items,
|
|
16
|
+
color,
|
|
17
|
+
unstyled,
|
|
18
|
+
}: {
|
|
19
|
+
items: readonly ChartLegendItem[];
|
|
20
|
+
color?: string;
|
|
21
|
+
unstyled?: boolean;
|
|
22
|
+
}) {
|
|
23
|
+
if (items.length === 0) return null;
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div data-part="legend" style={unstyled ? undefined : { ...ROW, color }}>
|
|
27
|
+
{items.map((item) => (
|
|
28
|
+
<span key={item.label} data-part="legend-item" style={unstyled ? undefined : ITEM}>
|
|
29
|
+
<span
|
|
30
|
+
data-part="swatch"
|
|
31
|
+
data-dashed={item.dashed ? "" : undefined}
|
|
32
|
+
aria-hidden="true"
|
|
33
|
+
style={
|
|
34
|
+
unstyled
|
|
35
|
+
? undefined
|
|
36
|
+
: item.dashed
|
|
37
|
+
? { width: 14, height: 0, borderTop: `2px dashed ${item.color}` }
|
|
38
|
+
: { width: 10, height: 10, borderRadius: 2, background: item.color }
|
|
39
|
+
}
|
|
40
|
+
/>
|
|
41
|
+
{item.label}
|
|
42
|
+
</span>
|
|
43
|
+
))}
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
@@ -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
|
@@ -234,7 +234,30 @@ export type {
|
|
|
234
234
|
CategoryScale,
|
|
235
235
|
} from "./utils/category.utils.js";
|
|
236
236
|
export type { Bin, BinOptions } from "./utils/bin.utils.js";
|
|
237
|
-
export type { ScatterChartProps, ScatterSeries } from "./types/scatter.js";
|
|
237
|
+
export type { ScatterChartProps, ScatterReference, ScatterSeries } from "./types/scatter.js";
|
|
238
|
+
export type { ScatterDomain } from "./utils/scatter.utils.js";
|
|
239
|
+
export { ChartLegend } from "./components/ChartLegend/index.js";
|
|
240
|
+
export type { ChartLegendItem } from "./components/ChartLegend/index.js";
|
|
241
|
+
export { niceExtent, niceStep } from "./utils/category.utils.js";
|
|
238
242
|
export type { PolarChartProps } from "./types/polar.js";
|
|
239
243
|
export type { SparklineProps } from "./types/sparkline.js";
|
|
240
244
|
export type { Mode } from "./themes/p1.js";
|
|
245
|
+
export { ChartStory } from "./components/ChartStory/index.js";
|
|
246
|
+
export { ChartFrame } from "./components/ChartFrame/index.js";
|
|
247
|
+
export { useSteps } from "./hooks/useSteps.js";
|
|
248
|
+
export { useTween } from "./hooks/useTween.js";
|
|
249
|
+
export { useMarkMemory } from "./hooks/useMarkMemory.js";
|
|
250
|
+
export { toCsv, downloadCsv } from "./utils/csv.utils.js";
|
|
251
|
+
export { readableInk, contrastRatio } from "./utils/color.utils.js";
|
|
252
|
+
export { easeInOutCubic, easeOutCubic, linear } from "./utils/motion.utils.js";
|
|
253
|
+
export type {
|
|
254
|
+
Steps,
|
|
255
|
+
FrameData,
|
|
256
|
+
StepsOptions,
|
|
257
|
+
ChartFramePart,
|
|
258
|
+
ChartFrameProps,
|
|
259
|
+
ChartStoryPart,
|
|
260
|
+
ChartStoryProps,
|
|
261
|
+
} from "./types/story.js";
|
|
262
|
+
export type { ChartAnimate, Easing, MotionFrames, MotionOptions, TweenMarks } from "./types/motion.js";
|
|
263
|
+
export type { CategorySort } from "./types/category.js";
|
package/src/scatter.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
export { ScatterChart } from "./charts/ScatterChart/index.js";
|
|
2
2
|
export { PolarChart } from "./charts/PolarChart/index.js";
|
|
3
3
|
export { scatterLayout } from "./utils/scatter.utils.js";
|
|
4
|
+
export type { ScatterDomain } from "./utils/scatter.utils.js";
|
|
4
5
|
export { sectorPath } from "./utils/polar.utils.js";
|
|
5
6
|
|
|
6
|
-
export type { ScatterChartProps, ScatterSeries } from "./types/scatter.js";
|
|
7
|
+
export type { ScatterChartProps, ScatterReference, ScatterSeries } from "./types/scatter.js";
|
|
8
|
+
export { ChartLegend } from "./components/ChartLegend/index.js";
|
|
9
|
+
export type { ChartLegendItem } from "./components/ChartLegend/index.js";
|
|
7
10
|
export type { PolarChartProps } from "./types/polar.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";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* p1-charts for pages without React: one script that carries React and the SVG charts.
|
|
3
|
+
*
|
|
4
|
+
* <script src="p1-charts.standalone.js"></script>
|
|
5
|
+
* P1Charts.render(el, "CategoryBarChart", { categories, series, orientation: "horizontal" });
|
|
6
|
+
*
|
|
7
|
+
* `render` mounts once per element and re-renders on later calls; `unmount` removes it. Props are
|
|
8
|
+
* the components' own props, so functions (formatValue, renderTooltip returning a string) work.
|
|
9
|
+
* The canvas charts (TimeSeriesChart and friends) stay in the React entries.
|
|
10
|
+
*/
|
|
11
|
+
import { createElement, type ComponentType } from "react";
|
|
12
|
+
import { createRoot, type Root } from "react-dom/client";
|
|
13
|
+
import { CategoryBarChart } from "./charts/CategoryBarChart/index.js";
|
|
14
|
+
import { ScatterChart } from "./charts/ScatterChart/index.js";
|
|
15
|
+
import { PolarChart } from "./charts/PolarChart/index.js";
|
|
16
|
+
import { PieChart } from "./charts/PieChart/index.js";
|
|
17
|
+
import { Heatmap } from "./charts/Heatmap/index.js";
|
|
18
|
+
import { Sparkline } from "./charts/Sparkline/index.js";
|
|
19
|
+
import { ChartLegend } from "./components/ChartLegend/index.js";
|
|
20
|
+
import { createTheme } from "./utils/theme.utils.js";
|
|
21
|
+
import { p1Theme } from "./themes/p1.js";
|
|
22
|
+
import { p1ThemeFromCss } from "./themes/css.js";
|
|
23
|
+
import { bin } from "./utils/bin.utils.js";
|
|
24
|
+
import { niceExtent, niceStep } from "./utils/category.utils.js";
|
|
25
|
+
|
|
26
|
+
const charts: Record<string, ComponentType<any>> = {
|
|
27
|
+
CategoryBarChart,
|
|
28
|
+
ScatterChart,
|
|
29
|
+
PolarChart,
|
|
30
|
+
PieChart,
|
|
31
|
+
Heatmap,
|
|
32
|
+
Sparkline,
|
|
33
|
+
ChartLegend,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const roots = new WeakMap<Element, Root>();
|
|
37
|
+
|
|
38
|
+
export type StandaloneChart = keyof typeof charts;
|
|
39
|
+
|
|
40
|
+
/** Draw `chart` with `props` into `el`; a later call on the same element updates it in place. */
|
|
41
|
+
export function render(el: Element, chart: StandaloneChart, props: Record<string, unknown>): void {
|
|
42
|
+
const component = charts[chart];
|
|
43
|
+
|
|
44
|
+
if (!component) throw new Error(`p1-charts standalone: no chart named "${chart}"`);
|
|
45
|
+
|
|
46
|
+
let root = roots.get(el);
|
|
47
|
+
|
|
48
|
+
if (!root) {
|
|
49
|
+
el.replaceChildren();
|
|
50
|
+
root = createRoot(el);
|
|
51
|
+
roots.set(el, root);
|
|
52
|
+
}
|
|
53
|
+
root.render(createElement(component, props));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Remove the chart drawn into `el`. */
|
|
57
|
+
export function unmount(el: Element): void {
|
|
58
|
+
roots.get(el)?.unmount();
|
|
59
|
+
roots.delete(el);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The charts `render` knows by name. */
|
|
63
|
+
export const chartNames = Object.keys(charts);
|
|
64
|
+
export { bin, createTheme, niceExtent, niceStep, p1Theme, p1ThemeFromCss };
|
|
65
|
+
export const version: string =
|
|
66
|
+
typeof __P1_CHARTS_VERSION__ === "string" ? __P1_CHARTS_VERSION__ : "dev";
|
|
67
|
+
|
|
68
|
+
declare const __P1_CHARTS_VERSION__: string;
|
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";
|