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
package/src/types/category.ts
CHANGED
|
@@ -6,6 +6,8 @@ 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";
|
|
10
|
+
import type { ChartLegendItem } from "../components/ChartLegend/index.js";
|
|
9
11
|
|
|
10
12
|
/** A bar. A number is the value. An object can colour and label that one bar. */
|
|
11
13
|
export type CategoryBarDatum =
|
|
@@ -62,6 +64,21 @@ export interface CategoryBarChartProps extends ThemeProps {
|
|
|
62
64
|
activeIndex?: number;
|
|
63
65
|
/** Caption on the value axis. */
|
|
64
66
|
valueLabel?: string;
|
|
67
|
+
/**
|
|
68
|
+
* Room for the category labels on a horizontal chart, or for the value ticks on a vertical
|
|
69
|
+
* one, in viewBox units. Horizontal defaults to fit the longest name (at least 108); vertical to 44.
|
|
70
|
+
*/
|
|
71
|
+
labelWidth?: number;
|
|
72
|
+
/**
|
|
73
|
+
* Round fitted axis ends so the ticks fall on round numbers. Default true. A fixed `domain` end
|
|
74
|
+
* is kept as given.
|
|
75
|
+
*/
|
|
76
|
+
nice?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* A swatch and name above the chart: `true` for one per series, or your own items (when bars
|
|
79
|
+
* are coloured one by one, say by family). Default false.
|
|
80
|
+
*/
|
|
81
|
+
legend?: boolean | readonly ChartLegendItem[];
|
|
65
82
|
width?: number;
|
|
66
83
|
height?: number;
|
|
67
84
|
className?: string;
|
|
@@ -72,5 +89,15 @@ export interface CategoryBarChartProps extends ThemeProps {
|
|
|
72
89
|
category: string,
|
|
73
90
|
rows: readonly CategoryTooltipRow[],
|
|
74
91
|
) => ReactNode;
|
|
92
|
+
/** Index is the position in `categories`, whatever `sort` does. */
|
|
75
93
|
onSelect?: (category: string, index: number) => void;
|
|
94
|
+
/**
|
|
95
|
+
* Order categories by their total across series. With `animate`, bars glide
|
|
96
|
+
* to their new rank when the data changes: a racing bar chart.
|
|
97
|
+
*/
|
|
98
|
+
sort?: CategorySort;
|
|
99
|
+
/** Tween bars between data states. Default true. Reduced motion jumps. */
|
|
100
|
+
animate?: ChartAnimate;
|
|
76
101
|
}
|
|
102
|
+
|
|
103
|
+
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,7 @@
|
|
|
1
|
+
import type { ChartLegendItem } from "../components/ChartLegend/index.js";
|
|
1
2
|
import type { ReactNode } from "react";
|
|
2
3
|
import type { ThemeProps } from "./theme.js";
|
|
4
|
+
import type { ChartAnimate } from "./motion.js";
|
|
3
5
|
|
|
4
6
|
export interface ScatterPoint {
|
|
5
7
|
x: number;
|
|
@@ -21,6 +23,13 @@ export interface ScatterSeries {
|
|
|
21
23
|
lineStyle?: "solid" | "dashed" | "dotted";
|
|
22
24
|
}
|
|
23
25
|
|
|
26
|
+
export interface ScatterReference {
|
|
27
|
+
from: { x: number; y: number };
|
|
28
|
+
to: { x: number; y: number };
|
|
29
|
+
label?: string;
|
|
30
|
+
color?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
24
33
|
export interface ScatterChartProps extends ThemeProps {
|
|
25
34
|
series: readonly ScatterSeries[];
|
|
26
35
|
width?: number;
|
|
@@ -29,6 +38,20 @@ export interface ScatterChartProps extends ThemeProps {
|
|
|
29
38
|
ariaLabel?: string;
|
|
30
39
|
xLabel?: string;
|
|
31
40
|
yLabel?: string;
|
|
41
|
+
/** Fixed x extent, such as `{ min: 0, max: 1 }` for a probability. A missing end is fitted. */
|
|
42
|
+
xDomain?: { min?: number; max?: number };
|
|
43
|
+
/** Fixed y extent. A missing end is fitted. */
|
|
44
|
+
yDomain?: { min?: number; max?: number };
|
|
45
|
+
/** Tick text on the x axis. Defaults to the number. */
|
|
46
|
+
formatX?: (value: number) => string;
|
|
47
|
+
/** Tick text on the y axis. Defaults to the number. */
|
|
48
|
+
formatY?: (value: number) => string;
|
|
49
|
+
/** Dashed lines between two points, such as the diagonal of a calibration plot. */
|
|
50
|
+
references?: readonly ScatterReference[];
|
|
51
|
+
/** A swatch and name per series above the chart (references included), or your own items. */
|
|
52
|
+
legend?: boolean | readonly ChartLegendItem[];
|
|
53
|
+
/** Tween points between data states, matched by series and index. Default true. */
|
|
54
|
+
animate?: ChartAnimate;
|
|
32
55
|
onSelect?: (point: {
|
|
33
56
|
series: ScatterSeries;
|
|
34
57
|
point: ScatterPoint;
|
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
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type CategoryBarMode = "group" | "stack" | "overlay";
|
|
1
|
+
export type CategoryBarMode = "group" | "stack" | "overlay" | "dot";
|
|
2
2
|
export type CategoryOrientation = "vertical" | "horizontal";
|
|
3
3
|
export type CategoryScale = "left" | "right";
|
|
4
4
|
|
|
@@ -47,6 +47,11 @@ export interface CategoryLayoutOptions {
|
|
|
47
47
|
rightDomain?: CategoryDomain;
|
|
48
48
|
/** Values the axis must contain, such as a threshold the bars have not reached. */
|
|
49
49
|
pins?: readonly { value: number; scale?: CategoryScale }[];
|
|
50
|
+
/**
|
|
51
|
+
* Round each fitted end out so the four even ticks fall on round numbers (0.239 → 0.24 in
|
|
52
|
+
* steps of 0.06). A fixed `domain` end is kept as given. Default true.
|
|
53
|
+
*/
|
|
54
|
+
nice?: boolean;
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
export interface CategoryLine {
|
|
@@ -86,6 +91,7 @@ function extentOf(
|
|
|
86
91
|
scale: CategoryScale,
|
|
87
92
|
domain?: CategoryDomain,
|
|
88
93
|
pins?: readonly number[],
|
|
94
|
+
nice = true,
|
|
89
95
|
): CategoryExtent | null {
|
|
90
96
|
const mine = series.filter((item) => scaleOf(item) === scale);
|
|
91
97
|
|
|
@@ -130,7 +136,52 @@ function extentOf(
|
|
|
130
136
|
max: domain?.max ?? max,
|
|
131
137
|
};
|
|
132
138
|
|
|
133
|
-
return { min: next.min, max: next.max
|
|
139
|
+
if (next.max === next.min) return { min: next.min, max: next.max + 1 };
|
|
140
|
+
|
|
141
|
+
return nice ? niceExtent(next.min, next.max, domain) : next;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const NICE_STEPS = [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10];
|
|
145
|
+
|
|
146
|
+
/** The smallest round step at or above `raw`: 1, 1.5, 2, 2.5, 3, 4, 5, 6 or 8 times a power of ten. */
|
|
147
|
+
export function niceStep(raw: number): number {
|
|
148
|
+
if (!(raw > 0) || !Number.isFinite(raw)) return 1;
|
|
149
|
+
|
|
150
|
+
const power = 10 ** Math.floor(Math.log10(raw));
|
|
151
|
+
|
|
152
|
+
for (const m of NICE_STEPS) if (m * power >= raw * (1 - 1e-9)) return Number((m * power).toPrecision(12));
|
|
153
|
+
|
|
154
|
+
return 10 * power;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Widen the fitted ends of `[min, max]` so `count` even ticks are round numbers. An end fixed in
|
|
159
|
+
* `domain` stays where it is; the step is then chosen so the other end is round.
|
|
160
|
+
*/
|
|
161
|
+
export function niceExtent(
|
|
162
|
+
min: number,
|
|
163
|
+
max: number,
|
|
164
|
+
domain?: CategoryDomain,
|
|
165
|
+
count = 4,
|
|
166
|
+
): CategoryExtent {
|
|
167
|
+
const fixedMin = domain?.min !== undefined;
|
|
168
|
+
const fixedMax = domain?.max !== undefined;
|
|
169
|
+
|
|
170
|
+
if ((fixedMin && fixedMax) || !(max > min)) return { min, max };
|
|
171
|
+
|
|
172
|
+
let step = niceStep((max - min) / count);
|
|
173
|
+
|
|
174
|
+
for (let tries = 0; tries < 4; tries += 1) {
|
|
175
|
+
const lo = fixedMin ? min : Math.floor(min / step + 1e-9) * step;
|
|
176
|
+
const hi = fixedMax ? max : lo + count * step;
|
|
177
|
+
|
|
178
|
+
if (hi >= max - 1e-12 && lo <= min + 1e-12) {
|
|
179
|
+
return { min: Number(lo.toPrecision(12)), max: Number(hi.toPrecision(12)) };
|
|
180
|
+
}
|
|
181
|
+
step = niceStep(step * 1.0001);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return { min, max };
|
|
134
185
|
}
|
|
135
186
|
|
|
136
187
|
function presentAt(
|
|
@@ -193,6 +244,23 @@ function placeVertical(
|
|
|
193
244
|
continue;
|
|
194
245
|
}
|
|
195
246
|
|
|
247
|
+
if (mode === "dot") {
|
|
248
|
+
present.forEach((item) => {
|
|
249
|
+
const yOf = yFor(item.scale);
|
|
250
|
+
|
|
251
|
+
rects.push({
|
|
252
|
+
series: item.id,
|
|
253
|
+
category,
|
|
254
|
+
x: origin,
|
|
255
|
+
y: yOf(item.value),
|
|
256
|
+
width: inner,
|
|
257
|
+
height: 0,
|
|
258
|
+
value: item.value,
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
|
|
196
264
|
if (mode === "overlay") {
|
|
197
265
|
present.forEach((item) => {
|
|
198
266
|
const yOf = yFor(item.scale);
|
|
@@ -290,6 +358,23 @@ function placeHorizontal(
|
|
|
290
358
|
continue;
|
|
291
359
|
}
|
|
292
360
|
|
|
361
|
+
if (mode === "dot") {
|
|
362
|
+
present.forEach((item) => {
|
|
363
|
+
const xOf = xFor(item.scale);
|
|
364
|
+
|
|
365
|
+
rects.push({
|
|
366
|
+
series: item.id,
|
|
367
|
+
category,
|
|
368
|
+
x: xOf(item.value),
|
|
369
|
+
y: origin,
|
|
370
|
+
width: 0,
|
|
371
|
+
height: inner,
|
|
372
|
+
value: item.value,
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
|
|
293
378
|
if (mode === "overlay") {
|
|
294
379
|
present.forEach((item) => {
|
|
295
380
|
const xOf = xFor(item.scale);
|
|
@@ -381,6 +466,7 @@ export function categoryFrame(options: CategoryLayoutOptions): CategoryFrame {
|
|
|
381
466
|
"left",
|
|
382
467
|
options.domain,
|
|
383
468
|
pinsFor("left"),
|
|
469
|
+
options.nice ?? true,
|
|
384
470
|
) ?? {
|
|
385
471
|
min: 0,
|
|
386
472
|
max: 1,
|
|
@@ -392,6 +478,7 @@ export function categoryFrame(options: CategoryLayoutOptions): CategoryFrame {
|
|
|
392
478
|
"right",
|
|
393
479
|
options.rightDomain,
|
|
394
480
|
pinsFor("right"),
|
|
481
|
+
options.nice ?? true,
|
|
395
482
|
);
|
|
396
483
|
const rects =
|
|
397
484
|
options.categories <= 0 || options.series.length === 0
|
|
@@ -435,3 +522,25 @@ export function axisTicks(min: number, max: number, count = 4): number[] {
|
|
|
435
522
|
Number((min + step * index).toPrecision(6)),
|
|
436
523
|
);
|
|
437
524
|
}
|
|
525
|
+
|
|
526
|
+
/** Display order of categories, by each category's total across series. Stable on ties. */
|
|
527
|
+
export function sortOrder(
|
|
528
|
+
count: number,
|
|
529
|
+
series: readonly (readonly number[])[],
|
|
530
|
+
sort: "none" | "ascending" | "descending",
|
|
531
|
+
): number[] {
|
|
532
|
+
const order = Array.from({ length: count }, (_, index) => index);
|
|
533
|
+
|
|
534
|
+
if (sort === "none") return order;
|
|
535
|
+
|
|
536
|
+
const total = (index: number) =>
|
|
537
|
+
series.reduce((sum, data) => {
|
|
538
|
+
const value = data[index];
|
|
539
|
+
|
|
540
|
+
return sum + (value !== undefined && Number.isFinite(value) ? value : 0);
|
|
541
|
+
}, 0);
|
|
542
|
+
const totals = order.map(total);
|
|
543
|
+
const sign = sort === "ascending" ? 1 : -1;
|
|
544
|
+
|
|
545
|
+
return order.sort((a, b) => sign * (totals[a]! - totals[b]!) || a - b);
|
|
546
|
+
}
|
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(
|