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
package/src/types/sparkline.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SparklineDatum } from "../utils/sparkline.utils.js";
|
|
2
|
+
import type { ChartAnimate } from "./motion.js";
|
|
2
3
|
|
|
3
4
|
export interface SparklineProps {
|
|
4
5
|
data: readonly SparklineDatum[];
|
|
@@ -6,6 +7,8 @@ export interface SparklineProps {
|
|
|
6
7
|
height?: number;
|
|
7
8
|
color?: string;
|
|
8
9
|
area?: boolean;
|
|
10
|
+
/** Tween the line when values change, matched by index. Default true. */
|
|
11
|
+
animate?: ChartAnimate;
|
|
9
12
|
className?: string;
|
|
10
13
|
ariaLabel?: string;
|
|
11
14
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
2
|
+
import type { ThemeProps } from "./theme.js";
|
|
3
|
+
|
|
4
|
+
export interface StepsOptions {
|
|
5
|
+
/** Number of steps. */
|
|
6
|
+
count: number;
|
|
7
|
+
/** Controlled step. */
|
|
8
|
+
index?: number;
|
|
9
|
+
defaultIndex?: number;
|
|
10
|
+
onIndexChange?: (index: number) => void;
|
|
11
|
+
/** Advance on a timer. Off by default, and never under reduced motion. */
|
|
12
|
+
autoplay?: boolean;
|
|
13
|
+
/** Milliseconds per step while playing. Default 3000. */
|
|
14
|
+
interval?: number;
|
|
15
|
+
/** Wrap from the last step to the first. Without it, playing stops at the end. */
|
|
16
|
+
loop?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface Steps {
|
|
20
|
+
index: number;
|
|
21
|
+
count: number;
|
|
22
|
+
playing: boolean;
|
|
23
|
+
isFirst: boolean;
|
|
24
|
+
isLast: boolean;
|
|
25
|
+
goTo: (index: number) => void;
|
|
26
|
+
next: () => void;
|
|
27
|
+
prev: () => void;
|
|
28
|
+
play: () => void;
|
|
29
|
+
pause: () => void;
|
|
30
|
+
toggle: () => void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type ChartStoryPart =
|
|
34
|
+
| "root"
|
|
35
|
+
| "caption"
|
|
36
|
+
| "stage"
|
|
37
|
+
| "controls"
|
|
38
|
+
| "prev"
|
|
39
|
+
| "next"
|
|
40
|
+
| "play"
|
|
41
|
+
| "dots"
|
|
42
|
+
| "dot"
|
|
43
|
+
| "slider"
|
|
44
|
+
| "position";
|
|
45
|
+
|
|
46
|
+
export interface ChartStoryProps<S> extends Omit<StepsOptions, "count">, ThemeProps {
|
|
47
|
+
/** One entry per state. Anything: chart props, a year, a caption. */
|
|
48
|
+
steps: readonly S[];
|
|
49
|
+
/** Draws the chart for the current step. Keep the same chart mounted so it animates. */
|
|
50
|
+
children: (step: S, index: number, steps: Steps) => ReactNode;
|
|
51
|
+
/** Text read out and shown for the step. */
|
|
52
|
+
caption?: (step: S, index: number) => ReactNode;
|
|
53
|
+
/** Dots for a handful of slides; a slider to scrub a time line. Default "dots". */
|
|
54
|
+
controls?: "dots" | "slider" | "none";
|
|
55
|
+
/** Label of a dot or slider position, such as the year. Default "Step n". */
|
|
56
|
+
stepLabel?: (step: S, index: number) => string;
|
|
57
|
+
ariaLabel?: string;
|
|
58
|
+
className?: string;
|
|
59
|
+
style?: CSSProperties;
|
|
60
|
+
classNames?: Partial<Record<ChartStoryPart, string>>;
|
|
61
|
+
styles?: Partial<Record<ChartStoryPart, CSSProperties>>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface FrameData {
|
|
65
|
+
columns: readonly string[];
|
|
66
|
+
rows: readonly (readonly (string | number | null | undefined)[])[];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type ChartFramePart =
|
|
70
|
+
| "root"
|
|
71
|
+
| "header"
|
|
72
|
+
| "title"
|
|
73
|
+
| "subtitle"
|
|
74
|
+
| "summary"
|
|
75
|
+
| "body"
|
|
76
|
+
| "footer"
|
|
77
|
+
| "source"
|
|
78
|
+
| "note"
|
|
79
|
+
| "actions"
|
|
80
|
+
| "table-toggle"
|
|
81
|
+
| "download"
|
|
82
|
+
| "table";
|
|
83
|
+
|
|
84
|
+
export interface ChartFrameProps extends ThemeProps {
|
|
85
|
+
title?: ReactNode;
|
|
86
|
+
subtitle?: ReactNode;
|
|
87
|
+
/** Read by screen readers only: what the chart shows, in a sentence or two. */
|
|
88
|
+
summary?: string;
|
|
89
|
+
/** Where the numbers come from. Printed as "Source: …". */
|
|
90
|
+
source?: ReactNode;
|
|
91
|
+
note?: ReactNode;
|
|
92
|
+
/** The numbers behind the chart. Adds a table view and a CSV download. */
|
|
93
|
+
data?: FrameData;
|
|
94
|
+
/** File name for the CSV download. Default "data.csv". */
|
|
95
|
+
filename?: string;
|
|
96
|
+
/** Heading level for the title. Default 3. */
|
|
97
|
+
level?: 2 | 3 | 4 | 5 | 6;
|
|
98
|
+
children: ReactNode;
|
|
99
|
+
className?: string;
|
|
100
|
+
style?: CSSProperties;
|
|
101
|
+
classNames?: Partial<Record<ChartFramePart, string>>;
|
|
102
|
+
styles?: Partial<Record<ChartFramePart, CSSProperties>>;
|
|
103
|
+
}
|
|
@@ -435,3 +435,25 @@ export function axisTicks(min: number, max: number, count = 4): number[] {
|
|
|
435
435
|
Number((min + step * index).toPrecision(6)),
|
|
436
436
|
);
|
|
437
437
|
}
|
|
438
|
+
|
|
439
|
+
/** Display order of categories, by each category's total across series. Stable on ties. */
|
|
440
|
+
export function sortOrder(
|
|
441
|
+
count: number,
|
|
442
|
+
series: readonly (readonly number[])[],
|
|
443
|
+
sort: "none" | "ascending" | "descending",
|
|
444
|
+
): number[] {
|
|
445
|
+
const order = Array.from({ length: count }, (_, index) => index);
|
|
446
|
+
|
|
447
|
+
if (sort === "none") return order;
|
|
448
|
+
|
|
449
|
+
const total = (index: number) =>
|
|
450
|
+
series.reduce((sum, data) => {
|
|
451
|
+
const value = data[index];
|
|
452
|
+
|
|
453
|
+
return sum + (value !== undefined && Number.isFinite(value) ? value : 0);
|
|
454
|
+
}, 0);
|
|
455
|
+
const totals = order.map(total);
|
|
456
|
+
const sign = sort === "ascending" ? 1 : -1;
|
|
457
|
+
|
|
458
|
+
return order.sort((a, b) => sign * (totals[a]! - totals[b]!) || a - b);
|
|
459
|
+
}
|
package/src/utils/color.utils.ts
CHANGED
|
@@ -140,3 +140,51 @@ export function toSrgb(color: string): string | undefined {
|
|
|
140
140
|
? trimmed
|
|
141
141
|
: formatRgba(parsed);
|
|
142
142
|
}
|
|
143
|
+
|
|
144
|
+
/** WCAG relative luminance of an sRGB colour, 0–1. */
|
|
145
|
+
export function luminance(color: string): number | null {
|
|
146
|
+
const parsed = parseColor(color);
|
|
147
|
+
|
|
148
|
+
if (!parsed) return null;
|
|
149
|
+
|
|
150
|
+
const linear = (value: number) => {
|
|
151
|
+
const c = value / 255;
|
|
152
|
+
|
|
153
|
+
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
return 0.2126 * linear(parsed.r) + 0.7152 * linear(parsed.g) + 0.0722 * linear(parsed.b);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** WCAG contrast ratio between two colours, 1–21. Null when either does not parse. */
|
|
160
|
+
export function contrastRatio(a: string, b: string): number | null {
|
|
161
|
+
const la = luminance(a);
|
|
162
|
+
const lb = luminance(b);
|
|
163
|
+
|
|
164
|
+
if (la === null || lb === null) return null;
|
|
165
|
+
|
|
166
|
+
return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The candidate with the most contrast against `background`, for a label
|
|
171
|
+
* drawn on a bar, a slice or a cell. Defaults to black and white.
|
|
172
|
+
*/
|
|
173
|
+
export function readableInk(
|
|
174
|
+
background: string,
|
|
175
|
+
candidates: readonly string[] = ["#000000", "#ffffff"],
|
|
176
|
+
): string {
|
|
177
|
+
let best = candidates[0] ?? "#000000";
|
|
178
|
+
let bestRatio = -1;
|
|
179
|
+
|
|
180
|
+
candidates.forEach((candidate) => {
|
|
181
|
+
const ratio = contrastRatio(background, candidate) ?? -1;
|
|
182
|
+
|
|
183
|
+
if (ratio > bestRatio) {
|
|
184
|
+
best = candidate;
|
|
185
|
+
bestRatio = ratio;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
return best;
|
|
190
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { FrameData } from "../types/story.js";
|
|
2
|
+
|
|
3
|
+
function cell(value: string | number | null | undefined): string {
|
|
4
|
+
if (value === null || value === undefined) return "";
|
|
5
|
+
|
|
6
|
+
const text = typeof value === "number" ? (Number.isFinite(value) ? String(value) : "") : value;
|
|
7
|
+
// A leading =, +, - or @ is read as a formula by spreadsheets.
|
|
8
|
+
const safe = /^[=+\-@\t\r]/.test(text) && typeof value === "string" ? `'${text}` : text;
|
|
9
|
+
|
|
10
|
+
return /[",\n\r]/.test(safe) ? `"${safe.replace(/"/g, '""')}"` : safe;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** RFC 4180 CSV with CRLF line ends. Text that a spreadsheet would run as a formula is quoted with `'`. */
|
|
14
|
+
export function toCsv({ columns, rows }: FrameData): string {
|
|
15
|
+
return [columns, ...rows].map((row) => row.map(cell).join(",")).join("\r\n");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Save `data` as a CSV file in the browser. */
|
|
19
|
+
export function downloadCsv(data: FrameData, filename = "data.csv"): void {
|
|
20
|
+
const blob = new Blob(["", toCsv(data)], { type: "text/csv;charset=utf-8" });
|
|
21
|
+
const url = URL.createObjectURL(blob);
|
|
22
|
+
const link = document.createElement("a");
|
|
23
|
+
|
|
24
|
+
link.href = url;
|
|
25
|
+
link.download = filename;
|
|
26
|
+
link.click();
|
|
27
|
+
setTimeout(() => URL.revokeObjectURL(url), 0);
|
|
28
|
+
}
|
|
@@ -1,6 +1,130 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChartAnimate,
|
|
3
|
+
Easing,
|
|
4
|
+
MotionFrames,
|
|
5
|
+
MotionSettings,
|
|
6
|
+
} from "../types/motion.js";
|
|
7
|
+
|
|
8
|
+
export const DURATION = 240;
|
|
9
|
+
|
|
1
10
|
export function prefersReducedMotion(): boolean {
|
|
2
11
|
return (
|
|
12
|
+
typeof window !== "undefined" &&
|
|
3
13
|
typeof window.matchMedia === "function" &&
|
|
4
14
|
window.matchMedia("(prefers-reduced-motion: reduce)").matches
|
|
5
15
|
);
|
|
6
16
|
}
|
|
17
|
+
|
|
18
|
+
export const linear: Easing = (t) => t;
|
|
19
|
+
export const easeOutCubic: Easing = (t) => 1 - (1 - t) ** 3;
|
|
20
|
+
export const easeInOutCubic: Easing = (t) =>
|
|
21
|
+
t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
|
|
22
|
+
|
|
23
|
+
/** `animate` as concrete settings. `false` gives a zero duration. */
|
|
24
|
+
export function motionSettings(animate: ChartAnimate = true): MotionSettings {
|
|
25
|
+
const options = typeof animate === "object" ? animate : {};
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
duration: animate === false ? 0 : Math.max(0, options.duration ?? DURATION),
|
|
29
|
+
easing: options.easing ?? easeInOutCubic,
|
|
30
|
+
stagger: Math.max(0, options.stagger ?? 0),
|
|
31
|
+
appear: options.appear ?? false,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function sameFrames(a: MotionFrames, b: MotionFrames): boolean {
|
|
36
|
+
const keys = Object.keys(b);
|
|
37
|
+
|
|
38
|
+
if (Object.keys(a).length !== keys.length) return false;
|
|
39
|
+
|
|
40
|
+
return keys.every((key) => {
|
|
41
|
+
const left = a[key];
|
|
42
|
+
const right = b[key]!;
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
left !== undefined &&
|
|
46
|
+
left.length === right.length &&
|
|
47
|
+
right.every((value, index) => left[index] === value)
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Blend two keyed frames at progress `t` (0–1, already eased). Keys only in
|
|
54
|
+
* `to` start from `enter(key, to[key])`. Keys only in `from` are dropped.
|
|
55
|
+
*/
|
|
56
|
+
export function tweenFrames(
|
|
57
|
+
from: MotionFrames,
|
|
58
|
+
to: MotionFrames,
|
|
59
|
+
t: number | ((key: string, index: number) => number),
|
|
60
|
+
enter: (key: string, value: readonly number[]) => readonly number[] = (_key, value) => value,
|
|
61
|
+
): Record<string, number[]> {
|
|
62
|
+
const out: Record<string, number[]> = {};
|
|
63
|
+
|
|
64
|
+
Object.keys(to).forEach((key, index) => {
|
|
65
|
+
const target = to[key]!;
|
|
66
|
+
const start = from[key];
|
|
67
|
+
const origin = start && start.length === target.length ? start : enter(key, target);
|
|
68
|
+
const progress = typeof t === "number" ? t : t(key, index);
|
|
69
|
+
|
|
70
|
+
out[key] = target.map((value, slot) => {
|
|
71
|
+
const begin = origin[slot] ?? value;
|
|
72
|
+
|
|
73
|
+
return begin + (value - begin) * progress;
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Eased progress of the key at `index`, `elapsed` ms after the start. */
|
|
81
|
+
export function staggeredProgress(
|
|
82
|
+
elapsed: number,
|
|
83
|
+
index: number,
|
|
84
|
+
settings: Pick<MotionSettings, "duration" | "stagger" | "easing">,
|
|
85
|
+
): number {
|
|
86
|
+
if (settings.duration <= 0) return 1;
|
|
87
|
+
|
|
88
|
+
const local = (elapsed - index * settings.stagger) / settings.duration;
|
|
89
|
+
|
|
90
|
+
return settings.easing(Math.max(0, Math.min(1, local)));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** How long a tween over `count` keys takes, stagger included. */
|
|
94
|
+
export function totalDuration(
|
|
95
|
+
count: number,
|
|
96
|
+
settings: Pick<MotionSettings, "duration" | "stagger">,
|
|
97
|
+
): number {
|
|
98
|
+
return settings.duration + settings.stagger * Math.max(0, count - 1);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A JS easing as a CSS `linear()` timing function, sampled at `samples + 1` points. */
|
|
102
|
+
export function cssEasing(easing: Easing, samples = 16): string {
|
|
103
|
+
if (easing === linear) return "linear";
|
|
104
|
+
|
|
105
|
+
const points = Array.from({ length: samples + 1 }, (_, index) => {
|
|
106
|
+
const value = easing(index / samples);
|
|
107
|
+
|
|
108
|
+
return Number.isFinite(value) ? Number(value.toFixed(4)) : index / samples;
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return `linear(${points.join(", ")})`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A CSS `transition` for marks that only change colour or opacity, or
|
|
116
|
+
* undefined when motion is off or reduced. The easing is sampled into
|
|
117
|
+
* `linear()`, so a custom curve matches the tweened charts.
|
|
118
|
+
*/
|
|
119
|
+
export function cssTransition(
|
|
120
|
+
animate: ChartAnimate | undefined,
|
|
121
|
+
properties: readonly string[],
|
|
122
|
+
): string | undefined {
|
|
123
|
+
const { duration, easing } = motionSettings(animate);
|
|
124
|
+
|
|
125
|
+
if (duration <= 0 || prefersReducedMotion()) return undefined;
|
|
126
|
+
|
|
127
|
+
const timing = cssEasing(easing);
|
|
128
|
+
|
|
129
|
+
return properties.map((property) => `${property} ${duration}ms ${timing}`).join(", ");
|
|
130
|
+
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { PieAngles, PieDatum, PieSlice } from "../types/pie.js";
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
|
|
3
|
+
export { easeInOutCubic } from "./motion.utils.js";
|
|
5
4
|
|
|
6
5
|
export const pieAngles = (slices: ReadonlyArray<PieSlice>): PieAngles =>
|
|
7
6
|
Object.fromEntries(
|
|
@@ -85,3 +85,72 @@ export function sparklineArea(
|
|
|
85
85
|
|
|
86
86
|
return `${line} L ${x(last).toFixed(2)} ${baseline.toFixed(2)} L ${x(first).toFixed(2)} ${baseline.toFixed(2)} Z`;
|
|
87
87
|
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* How far `next` scrolled from `previous`: the smallest k with
|
|
91
|
+
* `previous[k + i] === next[i]` over an overlap of at least two values.
|
|
92
|
+
* 0 when the values did not slide, so a sliding window can scroll instead of morph.
|
|
93
|
+
*/
|
|
94
|
+
export function shiftOffset(
|
|
95
|
+
previous: readonly (number | null)[],
|
|
96
|
+
next: readonly (number | null)[],
|
|
97
|
+
): number {
|
|
98
|
+
const limit = Math.min(previous.length, next.length) - 2;
|
|
99
|
+
|
|
100
|
+
for (let k = 1; k <= limit; k += 1) {
|
|
101
|
+
let same = true;
|
|
102
|
+
|
|
103
|
+
for (let i = 0; i + k < previous.length && i < next.length; i += 1) {
|
|
104
|
+
if (previous[i + k] !== next[i]) {
|
|
105
|
+
same = false;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (same) return k;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Line and area paths through `[position, value]` points sorted by position,
|
|
118
|
+
* where `count` positions span the width. A gap of more than one position
|
|
119
|
+
* (a null) breaks the line. Positions may be fractional while scrolling.
|
|
120
|
+
*/
|
|
121
|
+
export function sparklineShapes(
|
|
122
|
+
points: readonly (readonly [position: number, value: number, slot: number])[],
|
|
123
|
+
count: number,
|
|
124
|
+
width: number,
|
|
125
|
+
height: number,
|
|
126
|
+
pad = 1,
|
|
127
|
+
): { lines: string[]; area: string | null } {
|
|
128
|
+
if (points.length === 0) return { lines: [], area: null };
|
|
129
|
+
|
|
130
|
+
const values = points.map((point) => point[1]);
|
|
131
|
+
const min = Math.min(...values);
|
|
132
|
+
const max = Math.max(...values);
|
|
133
|
+
const span = max - min || 1;
|
|
134
|
+
const steps = Math.max(count - 1, 1);
|
|
135
|
+
const x = (position: number) => pad + (position / steps) * (width - pad * 2);
|
|
136
|
+
const y = (value: number) => pad + (1 - (value - min) / span) * (height - pad * 2);
|
|
137
|
+
const runs: Array<Array<readonly [number, number, number]>> = [];
|
|
138
|
+
|
|
139
|
+
points.forEach((point, index) => {
|
|
140
|
+
const before = points[index - 1];
|
|
141
|
+
|
|
142
|
+
if (!before || point[2] - before[2] !== 1) runs.push([]);
|
|
143
|
+
runs[runs.length - 1]!.push(point);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const lines = runs.map((run) =>
|
|
147
|
+
run
|
|
148
|
+
.map(([position, value], index) => `${index === 0 ? "M" : "L"} ${x(position).toFixed(2)} ${y(value).toFixed(2)}`)
|
|
149
|
+
.join(" "),
|
|
150
|
+
);
|
|
151
|
+
// Same outline as sparklineArea: the first run, closed under the last point.
|
|
152
|
+
const base = y(min).toFixed(2);
|
|
153
|
+
const area = `${lines[0]} L ${x(points[points.length - 1]![0]).toFixed(2)} ${base} L ${x(points[0]![0]).toFixed(2)} ${base} Z`;
|
|
154
|
+
|
|
155
|
+
return { lines, area };
|
|
156
|
+
}
|