@delacour/react-native-ui 0.11.0-alpha.20261124171114 → 0.11.0-alpha.20261124171459
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/package.json +3 -3
- package/src/components/chart/AGENTS.md +53 -0
- package/src/components/chart/chart-accessibility.render.test.tsx +109 -0
- package/src/components/chart/chart-accessibility.test.ts +179 -0
- package/src/components/chart/chart-accessibility.ts +156 -0
- package/src/components/chart/chart-frame.tsx +172 -0
- package/src/components/chart/chart-legend.render.test.tsx +55 -0
- package/src/components/chart/chart-legend.tsx +47 -1
- package/src/components/chart/chart-line.tsx +14 -3
- package/src/components/chart/chart-tooltip.tsx +2 -1
- package/src/components/chart/chart.tsx +37 -7
- package/src/components/chart/chart.types.ts +14 -0
- package/src/components/chart/chart.variants.test.ts +6 -0
- package/src/components/chart/chart.variants.ts +1 -0
- package/src/components/chart/index.ts +9 -0
- package/src/components/chart/pie-chart-slice-targets.tsx +100 -0
- package/src/components/chart/pie-chart.render.test.tsx +119 -0
- package/src/components/chart/pie-chart.tsx +66 -16
- package/src/components/kpi/AGENTS.md +5 -0
- package/src/components/kpi/kpi-sparkline.tsx +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delacour/react-native-ui",
|
|
3
|
-
"version": "0.11.0-alpha.
|
|
3
|
+
"version": "0.11.0-alpha.20261124171459",
|
|
4
4
|
"description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -186,8 +186,8 @@
|
|
|
186
186
|
"expo-blur": ">=15",
|
|
187
187
|
"@maplibre/maplibre-react-native": "^11.5.0",
|
|
188
188
|
"@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
|
|
189
|
-
"@delacour/react-native-bottom-sheet": "0.11.0-alpha.
|
|
190
|
-
"@delacour/react-native-charts": "0.11.0-alpha.
|
|
189
|
+
"@delacour/react-native-bottom-sheet": "0.11.0-alpha.20261124171459",
|
|
190
|
+
"@delacour/react-native-charts": "0.11.0-alpha.20261124171459",
|
|
191
191
|
"@legendapp/list": ">=3.3",
|
|
192
192
|
"react-native-skia": ">=3.9.0",
|
|
193
193
|
"expo-linear-gradient": ">=15",
|
|
@@ -13,6 +13,10 @@ knows nothing about tokens.
|
|
|
13
13
|
| `index.ts` | → `@delacour/react-native-ui/chart` |
|
|
14
14
|
| `chart.tsx` | The root, the child partition, and the single `Object.assign` |
|
|
15
15
|
| `chart.context.tsx` | The context, mounted twice — see below |
|
|
16
|
+
| `chart-frame.tsx` | The frame the canvas fills, as the chart's one accessibility element — an image, or an adjustable stepping the scrub |
|
|
17
|
+
| `chart-accessibility.ts` | The pure text alternative: the summary, the readout, the index step, a slice's spoken label |
|
|
18
|
+
| `chart-accessibility.test.ts` | Every sentence those produce |
|
|
19
|
+
| `chart-accessibility.render.test.tsx` | The frame's role, name, override and steps, mounted |
|
|
16
20
|
| `chart.types.ts` | `ChartConfig`, `ChartSeriesConfig`, `ChartResolvedSeries` |
|
|
17
21
|
| `chart.variants.ts` | The slotted `tv()`, the ramp, and every pure resolver |
|
|
18
22
|
| `chart.variants.test.ts` | Including the assertion that the ramp tokens exist |
|
|
@@ -30,6 +34,8 @@ knows nothing about tokens.
|
|
|
30
34
|
| `pie-chart.context.tsx` | The pie's context, mounted twice the same way; `useChartSeries` in `chart.context.tsx` reads either |
|
|
31
35
|
| `pie-chart-slice.tsx`, `pie-chart-label.tsx` | Skia marks — the wedges with a hairline between them, and a label per wedge |
|
|
32
36
|
| `pie-chart-center.tsx`, `pie-chart-tooltip.tsx` | React Native, over the canvas — the donut hole's content and the tap readout |
|
|
37
|
+
| `pie-chart-slice-targets.tsx` | A selectable pie's accessibility tree — the summary and one `button` per slice, never touch-reactive |
|
|
38
|
+
| `pie-chart.render.test.tsx` | The pie's text alternative and its slice items, activated the way VoiceOver and TalkBack do |
|
|
33
39
|
|
|
34
40
|
## Design
|
|
35
41
|
|
|
@@ -124,6 +130,16 @@ knows nothing about tokens.
|
|
|
124
130
|
the crosshair, the dot and the number all read the same row. `glide` opts
|
|
125
131
|
into the continuous curve position, and the doc says what it costs.
|
|
126
132
|
|
|
133
|
+
- **A series can be told apart by more than its colour.** `dash` on a config
|
|
134
|
+
entry — or `Chart.Line dash`, with `false` forcing it solid — draws that
|
|
135
|
+
line dashed, and `Chart.Legend` draws the same series' swatch as a dashed
|
|
136
|
+
sample. Colour alone fails anyone who cannot separate the two hues, a
|
|
137
|
+
greyscale print, and a low-contrast pair (WCAG 1.4.1); a cue the legend did
|
|
138
|
+
not repeat would distinguish two lines without saying which is which. The
|
|
139
|
+
lengths are what shows: the engine moves the round cap's reach out of each
|
|
140
|
+
dash, so a short gap does not close up into a solid line. The swatches are
|
|
141
|
+
hidden from assistive technology — the label beside each names the series.
|
|
142
|
+
|
|
127
143
|
- **A dot rings itself in the chart's background.** A dot in the series colour
|
|
128
144
|
sitting on a line of that same colour is a slight thickening and nothing
|
|
129
145
|
more; the ring is what makes it a knob. That is why `surfaceColor` is
|
|
@@ -169,6 +185,27 @@ knows nothing about tokens.
|
|
|
169
185
|
series that is only a bar, because a bar has no curve to sit on and the band
|
|
170
186
|
already picks out its column.
|
|
171
187
|
|
|
188
|
+
- **The frame is the chart's one accessibility element.** A Skia canvas is one
|
|
189
|
+
opaque view, so a chart with no text alternative is announced as nothing,
|
|
190
|
+
and its scrub is a long press only a sighted finger can make. The frame
|
|
191
|
+
carries `resolveChartSummary` — the x range, then each series' lowest,
|
|
192
|
+
highest and latest — as an `image`; with a `Chart.Tooltip` placed it is an
|
|
193
|
+
`adjustable` instead, whose increment and decrement step one datum through
|
|
194
|
+
`stepChartIndex` and write the engine's `scrub.focus`, so the tooltip, the
|
|
195
|
+
cursor and the dot follow exactly as they would a touch. Its value text is
|
|
196
|
+
`resolveChartReadout` through the tooltip's own `formatHeading` and
|
|
197
|
+
`formatValue`, read off the placed tooltip at partition time, and the
|
|
198
|
+
tooltip prints through the same `chartTooltipValueText` — the spoken and the
|
|
199
|
+
drawn readout cannot drift. The index lives in `ChartFrame`, a leaf, so a
|
|
200
|
+
drag re-renders the frame and not the root. `accessibilityLabel` replaces
|
|
201
|
+
the summary verbatim; `accessible={false}` hides the chart for a parent that
|
|
202
|
+
names it, which is what `Kpi.Sparkline` does. The summary is English: there
|
|
203
|
+
is no labels provider yet, and the override is the way out.
|
|
204
|
+
|
|
205
|
+
- **The scrub stays up after a step.** Nothing clears it: there is no
|
|
206
|
+
accessibility blur to clear it on, and the next touch's end does. A
|
|
207
|
+
sighted helper beside a screen-reader user sees the datum being read.
|
|
208
|
+
|
|
172
209
|
- **Parts must be direct children of `<Chart>`.** The root partitions them by
|
|
173
210
|
component identity to decide what goes inside the canvas, what floats over
|
|
174
211
|
it and what sits under it — the technique `Spinner` uses on its own children.
|
|
@@ -211,6 +248,22 @@ knows nothing about tokens.
|
|
|
211
248
|
show it — a `PieChart.Tooltip`, an `onSelect`, or a controlled
|
|
212
249
|
`selectedIndex` — so a pie with none of those takes no gesture at all.
|
|
213
250
|
|
|
251
|
+
- **A pie nothing selects is one `image`; a selectable pie is a summary and a
|
|
252
|
+
button per slice.** The canvas is one opaque view, so without either a
|
|
253
|
+
screen reader announces nothing. A picture pie's frame carries the
|
|
254
|
+
generated summary (`Pie chart. Free 57%, …`) — `accessibilityLabel`
|
|
255
|
+
replaces it, `accessible={false}` removes the pie. A selectable pie's frame
|
|
256
|
+
cannot be the element, because iOS ignores accessible descendants of an
|
|
257
|
+
accessible parent, so the canvas is hidden and `PieChartSliceTargets` stands
|
|
258
|
+
in: the summary, then each drawn slice in order as a 44-point box at its
|
|
259
|
+
label position, named with its value and share through the tooltip's own
|
|
260
|
+
formatters. The boxes are `pointerEvents="none"` and activate through
|
|
261
|
+
`accessibilityTap` on iOS and the `activate` action on Android, the split
|
|
262
|
+
`Pressable` makes — wired inline, because importing it would make `delacour
|
|
263
|
+
add chart` copy `Pressable` and install a haptics module — and a touch
|
|
264
|
+
handler there would steal a sighted tap from the engine's hit test. Activating a slice selects it; it never toggles
|
|
265
|
+
it off.
|
|
266
|
+
|
|
214
267
|
- **`@delacour/react-native-charts` is an optional peer, not a dependency.** A dependency
|
|
215
268
|
may be nested, and two copies of the engine mean two chart contexts — so a
|
|
216
269
|
correctly-nested `<Chart.Line>` would throw "must be used inside a `<Chart>`"
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
|
+
import { fireEvent, renderWithProviders, screen } from "../../test/render";
|
|
3
|
+
import { Chart } from "./chart";
|
|
4
|
+
|
|
5
|
+
const CONFIG = {
|
|
6
|
+
desktop: { label: "Desktop" },
|
|
7
|
+
mobile: { label: "Mobile" },
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const DATA = [
|
|
11
|
+
{ month: "Jan", desktop: 186, mobile: 80 },
|
|
12
|
+
{ month: "Feb", desktop: 305, mobile: 200 },
|
|
13
|
+
{ month: "Mar", desktop: 237, mobile: 120 },
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
const step = (name: "increment" | "decrement") => ({ nativeEvent: { actionName: name } });
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What Jest can and cannot see: the engine's `focus` reaction (the UI-thread
|
|
20
|
+
* move of the scrub) does not run here, so these tests observe the frame's
|
|
21
|
+
* own stepped index, which drives the value text beside it. That the scrub
|
|
22
|
+
* then follows `focus` is the engine's own test, and a simulator's.
|
|
23
|
+
*/
|
|
24
|
+
describe("Chart accessibility", () => {
|
|
25
|
+
test("is one image with a generated summary when no tooltip is placed", async () => {
|
|
26
|
+
await renderWithProviders(
|
|
27
|
+
<Chart config={CONFIG} data={DATA} testID="chart" xKey="month">
|
|
28
|
+
<Chart.Line yKey="desktop" />
|
|
29
|
+
</Chart>
|
|
30
|
+
);
|
|
31
|
+
const frame = screen.getByRole("image");
|
|
32
|
+
expect(frame).toBe(screen.getByTestId("chart"));
|
|
33
|
+
expect(frame.props.accessibilityLabel).toContain("Jan to Mar");
|
|
34
|
+
expect(frame.props.accessibilityLabel).toContain("Desktop: lowest 186, highest 305, latest 237.");
|
|
35
|
+
expect(screen.queryByRole("adjustable")).toBeNull();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("an accessibilityLabel replaces the summary verbatim", async () => {
|
|
39
|
+
await renderWithProviders(
|
|
40
|
+
<Chart accessibilityLabel="Revenue trend" config={CONFIG} data={DATA} xKey="month">
|
|
41
|
+
<Chart.Line yKey="desktop" />
|
|
42
|
+
</Chart>
|
|
43
|
+
);
|
|
44
|
+
expect(screen.getByRole("image", { name: "Revenue trend" })).toBeOnTheScreen();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("accessible={false} removes the chart from the accessibility tree", async () => {
|
|
48
|
+
await renderWithProviders(
|
|
49
|
+
<Chart accessible={false} config={CONFIG} data={DATA} testID="chart" xKey="month">
|
|
50
|
+
<Chart.Line yKey="desktop" />
|
|
51
|
+
<Chart.Tooltip />
|
|
52
|
+
</Chart>
|
|
53
|
+
);
|
|
54
|
+
expect(screen.queryByRole("image")).toBeNull();
|
|
55
|
+
expect(screen.queryByRole("adjustable")).toBeNull();
|
|
56
|
+
expect(screen.getByTestId("chart", { includeHiddenElements: true }).props.importantForAccessibility).toBe(
|
|
57
|
+
"no-hide-descendants"
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("a placed tooltip makes the chart adjustable, with no value before the first step", async () => {
|
|
62
|
+
await renderWithProviders(
|
|
63
|
+
<Chart config={CONFIG} data={DATA} xKey="month">
|
|
64
|
+
<Chart.Line yKey="desktop" />
|
|
65
|
+
<Chart.Tooltip />
|
|
66
|
+
</Chart>
|
|
67
|
+
);
|
|
68
|
+
const frame = screen.getByRole("adjustable");
|
|
69
|
+
expect(frame.props.accessibilityActions).toEqual([{ name: "increment" }, { name: "decrement" }]);
|
|
70
|
+
expect(frame.props.accessibilityValue).toBeUndefined();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("increment and decrement step through the data and stop at the ends", async () => {
|
|
74
|
+
await renderWithProviders(
|
|
75
|
+
<Chart config={CONFIG} data={DATA} xKey="month">
|
|
76
|
+
<Chart.Line yKey="desktop" />
|
|
77
|
+
<Chart.Line yKey="mobile" />
|
|
78
|
+
<Chart.Tooltip />
|
|
79
|
+
</Chart>
|
|
80
|
+
);
|
|
81
|
+
const frame = screen.getByRole("adjustable");
|
|
82
|
+
await fireEvent(frame, "accessibilityAction", step("increment"));
|
|
83
|
+
expect(screen.getByRole("adjustable").props.accessibilityValue).toEqual({ text: "Jan: Desktop 186, Mobile 80" });
|
|
84
|
+
await fireEvent(screen.getByRole("adjustable"), "accessibilityAction", step("increment"));
|
|
85
|
+
expect(screen.getByRole("adjustable").props.accessibilityValue).toEqual({ text: "Feb: Desktop 305, Mobile 200" });
|
|
86
|
+
await fireEvent(screen.getByRole("adjustable"), "accessibilityAction", step("decrement"));
|
|
87
|
+
await fireEvent(screen.getByRole("adjustable"), "accessibilityAction", step("decrement"));
|
|
88
|
+
expect(screen.getByRole("adjustable").props.accessibilityValue).toEqual({ text: "Jan: Desktop 186, Mobile 80" });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("the tooltip's formatters reach the value text and the summary", async () => {
|
|
92
|
+
await renderWithProviders(
|
|
93
|
+
<Chart config={CONFIG} data={DATA} xKey="month">
|
|
94
|
+
<Chart.Line yKey="desktop" />
|
|
95
|
+
<Chart.Line yKey="mobile" />
|
|
96
|
+
<Chart.Tooltip
|
|
97
|
+
formatHeading={(row) => `Month ${String(row.month)}`}
|
|
98
|
+
formatValue={(value) => `$${String(value)}`}
|
|
99
|
+
/>
|
|
100
|
+
</Chart>
|
|
101
|
+
);
|
|
102
|
+
const frame = screen.getByRole("adjustable");
|
|
103
|
+
expect(frame.props.accessibilityLabel).toContain("highest $305");
|
|
104
|
+
await fireEvent(frame, "accessibilityAction", step("increment"));
|
|
105
|
+
expect(screen.getByRole("adjustable").props.accessibilityValue).toEqual({
|
|
106
|
+
text: "Month Jan: Desktop $186, Mobile $80",
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import type { ChartTooltipRow } from "./chart.types";
|
|
3
|
+
import {
|
|
4
|
+
chartTooltipValueText,
|
|
5
|
+
pieSliceAccessibilityLabel,
|
|
6
|
+
resolveChartReadout,
|
|
7
|
+
resolveChartSummary,
|
|
8
|
+
stepChartIndex,
|
|
9
|
+
} from "./chart-accessibility";
|
|
10
|
+
|
|
11
|
+
const rows = [
|
|
12
|
+
{ month: "Jan", desktop: 186, mobile: 80 },
|
|
13
|
+
{ month: "Feb", desktop: 305, mobile: 200 },
|
|
14
|
+
{ month: "Mar", desktop: 237, mobile: null },
|
|
15
|
+
{ month: "Apr", desktop: 73, mobile: 190 },
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
const desktop = { key: "desktop", label: "Desktop" };
|
|
19
|
+
const series = [desktop, { key: "mobile", label: "Mobile" }];
|
|
20
|
+
|
|
21
|
+
const formatX = (row: Record<string, unknown>): string => String(row.month);
|
|
22
|
+
|
|
23
|
+
describe("resolveChartSummary — cartesian", () => {
|
|
24
|
+
test("names the x range, then each series' lowest, highest and latest", () => {
|
|
25
|
+
expect(resolveChartSummary({ kind: "cartesian", series, data: rows, formatX })).toBe(
|
|
26
|
+
"Chart, Jan to Apr. Desktop: lowest 73, highest 305, latest 73. Mobile: lowest 80, highest 200, latest 190."
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("latest is the last readable value, skipping a gap at the end", () => {
|
|
31
|
+
const gappy = [...rows, { month: "May", desktop: null, mobile: undefined }];
|
|
32
|
+
expect(resolveChartSummary({ kind: "cartesian", series, data: gappy, formatX })).toBe(
|
|
33
|
+
"Chart, Jan to May. Desktop: lowest 73, highest 305, latest 73. Mobile: lowest 80, highest 200, latest 190."
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("groups digits in threes by default", () => {
|
|
38
|
+
const big = [
|
|
39
|
+
{ month: "Jan", desktop: 1240 },
|
|
40
|
+
{ month: "Feb", desktop: 12_500.5 },
|
|
41
|
+
];
|
|
42
|
+
expect(resolveChartSummary({ kind: "cartesian", series: [desktop], data: big, formatX })).toBe(
|
|
43
|
+
"Chart, Jan to Feb. Desktop: lowest 1,240, highest 12,500.5, latest 12,500.5."
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("prints values through the tooltip's formatter when one is given", () => {
|
|
48
|
+
const formatValue = (value: unknown, key: string): string => `${key}=${String(value)}`;
|
|
49
|
+
expect(
|
|
50
|
+
resolveChartSummary({ kind: "cartesian", series: [desktop], data: rows.slice(0, 2), formatX, formatValue })
|
|
51
|
+
).toBe("Chart, Jan to Feb. Desktop: lowest desktop=186, highest desktop=305, latest desktop=305.");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("one row names one x, not a range", () => {
|
|
55
|
+
expect(resolveChartSummary({ kind: "cartesian", series: [desktop], data: rows.slice(0, 1), formatX })).toBe(
|
|
56
|
+
"Chart, Jan. Desktop: lowest 186, highest 186, latest 186."
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("a series with no readable value says so rather than printing NaN", () => {
|
|
61
|
+
const empty = [{ month: "Jan", desktop: null }];
|
|
62
|
+
expect(resolveChartSummary({ kind: "cartesian", series: [desktop], data: empty, formatX })).toBe(
|
|
63
|
+
"Chart, Jan. Desktop: no data."
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("no rows at all is one short sentence", () => {
|
|
68
|
+
expect(resolveChartSummary({ kind: "cartesian", series, data: [], formatX })).toBe("Chart, no data.");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("an override wins verbatim", () => {
|
|
72
|
+
expect(resolveChartSummary({ kind: "cartesian", series, data: rows, formatX, override: "Traffic, rising" })).toBe(
|
|
73
|
+
"Traffic, rising"
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("an empty override falls back to the generated summary", () => {
|
|
78
|
+
expect(resolveChartSummary({ kind: "cartesian", series, data: [], formatX, override: "" })).toBe("Chart, no data.");
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("resolveChartSummary — pie", () => {
|
|
83
|
+
test("names every slice with its whole-number share", () => {
|
|
84
|
+
expect(
|
|
85
|
+
resolveChartSummary({
|
|
86
|
+
kind: "pie",
|
|
87
|
+
series: [{ label: "Chrome" }, { label: "Safari" }, { label: "Other" }],
|
|
88
|
+
values: [275, 200, 25],
|
|
89
|
+
total: 500,
|
|
90
|
+
})
|
|
91
|
+
).toBe("Pie chart. Chrome 55%, Safari 40%, Other 5%.");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("no slices, or a zero total, is no data", () => {
|
|
95
|
+
expect(resolveChartSummary({ kind: "pie", series: [], values: [], total: 0 })).toBe("Pie chart, no data.");
|
|
96
|
+
expect(resolveChartSummary({ kind: "pie", series: [{ label: "A" }], values: [0], total: 0 })).toBe(
|
|
97
|
+
"Pie chart, no data."
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("an override wins verbatim", () => {
|
|
102
|
+
expect(
|
|
103
|
+
resolveChartSummary({ kind: "pie", series: [{ label: "A" }], values: [1], total: 1, override: "Share" })
|
|
104
|
+
).toBe("Share");
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("pieSliceAccessibilityLabel", () => {
|
|
109
|
+
test("is the name, the grouped value and the whole-number share", () => {
|
|
110
|
+
expect(pieSliceAccessibilityLabel({ label: "Chrome", value: 1240, total: 2000 })).toBe("Chrome, 1,240, 62%");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("prints through the caller's formatters", () => {
|
|
114
|
+
expect(
|
|
115
|
+
pieSliceAccessibilityLabel({
|
|
116
|
+
label: "Chrome",
|
|
117
|
+
value: 1240,
|
|
118
|
+
total: 2000,
|
|
119
|
+
formatValue: (value) => `$${value}`,
|
|
120
|
+
formatPercent: (percent) => `${percent.toFixed(1)} percent`,
|
|
121
|
+
})
|
|
122
|
+
).toBe("Chrome, $1240, 62.0 percent");
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const desktopRow: ChartTooltipRow = { key: "desktop", label: "Desktop", color: "#000", value: 186 };
|
|
127
|
+
const mobileRow: ChartTooltipRow = { key: "mobile", label: "Mobile", color: "#111", value: null };
|
|
128
|
+
const tooltipRows = [desktopRow, mobileRow];
|
|
129
|
+
|
|
130
|
+
describe("chartTooltipValueText", () => {
|
|
131
|
+
test("prints the value as written, and nothing for a missing one", () => {
|
|
132
|
+
expect(chartTooltipValueText(desktopRow)).toBe("186");
|
|
133
|
+
expect(chartTooltipValueText(mobileRow)).toBe("");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("defers to the formatter, with the key", () => {
|
|
137
|
+
expect(chartTooltipValueText(desktopRow, (value, key) => `${key}:${String(value)}`)).toBe("desktop:186");
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe("resolveChartReadout", () => {
|
|
142
|
+
test("is the heading, then each row's label and value — what the tooltip shows", () => {
|
|
143
|
+
expect(resolveChartReadout({ heading: "Jan", rows: tooltipRows })).toBe("Jan: Desktop 186, Mobile no value");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("drops an empty heading", () => {
|
|
147
|
+
expect(resolveChartReadout({ heading: "", rows: tooltipRows.slice(0, 1) })).toBe("Desktop 186");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("uses the tooltip's value formatter", () => {
|
|
151
|
+
expect(
|
|
152
|
+
resolveChartReadout({ heading: "Jan", rows: tooltipRows.slice(0, 1), formatValue: (value) => `${value} visits` })
|
|
153
|
+
).toBe("Jan: Desktop 186 visits");
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("stepChartIndex", () => {
|
|
158
|
+
test("the first increment lands on the first datum, the first decrement on the last", () => {
|
|
159
|
+
expect(stepChartIndex(-1, 1, 5)).toBe(0);
|
|
160
|
+
expect(stepChartIndex(-1, -1, 5)).toBe(4);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("moves one datum and stops at either end", () => {
|
|
164
|
+
expect(stepChartIndex(2, 1, 5)).toBe(3);
|
|
165
|
+
expect(stepChartIndex(2, -1, 5)).toBe(1);
|
|
166
|
+
expect(stepChartIndex(4, 1, 5)).toBe(4);
|
|
167
|
+
expect(stepChartIndex(0, -1, 5)).toBe(0);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("an index past a shrunk data set comes back inside it", () => {
|
|
171
|
+
expect(stepChartIndex(9, -1, 5)).toBe(3);
|
|
172
|
+
expect(stepChartIndex(9, 1, 5)).toBe(4);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("no data has no index", () => {
|
|
176
|
+
expect(stepChartIndex(-1, 1, 0)).toBe(-1);
|
|
177
|
+
expect(stepChartIndex(3, -1, 0)).toBe(-1);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { asNumber } from "@delacour/react-native-charts/core";
|
|
2
|
+
import type { ChartDatum, ChartTooltipRow } from "./chart.types";
|
|
3
|
+
import { formatPieValue, pieSlicePercent } from "./chart.variants";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What a chart's text alternative is built from.
|
|
7
|
+
*
|
|
8
|
+
* A discriminated union because the two roots describe different things: a
|
|
9
|
+
* cartesian chart is a set of series over a run of x, and a pie is a set of
|
|
10
|
+
* shares of one whole. `override` is the caller's `accessibilityLabel`, and
|
|
11
|
+
* an empty one counts as absent — a blank label is no name at all.
|
|
12
|
+
*/
|
|
13
|
+
export type ChartSummaryInput =
|
|
14
|
+
| {
|
|
15
|
+
readonly kind: "cartesian";
|
|
16
|
+
readonly series: readonly { readonly key: string; readonly label: string }[];
|
|
17
|
+
readonly data: readonly ChartDatum[];
|
|
18
|
+
/** Prints a row's x — the axis' own formatter, so a Date reads as a date. */
|
|
19
|
+
readonly formatX: (row: ChartDatum) => string;
|
|
20
|
+
/** The tooltip's value formatter, when it has one. */
|
|
21
|
+
readonly formatValue?: (value: unknown, key: string) => string;
|
|
22
|
+
readonly override?: string;
|
|
23
|
+
}
|
|
24
|
+
| {
|
|
25
|
+
readonly kind: "pie";
|
|
26
|
+
readonly series: readonly { readonly label: string }[];
|
|
27
|
+
readonly values: readonly number[];
|
|
28
|
+
readonly total: number;
|
|
29
|
+
readonly override?: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The sentence a screen reader speaks for a whole chart.
|
|
34
|
+
*
|
|
35
|
+
* A Skia canvas is one opaque view, so without this a chart is announced as
|
|
36
|
+
* nothing at all. A cartesian chart names its x range, then each series'
|
|
37
|
+
* lowest, highest and latest value — the three numbers a sighted reader takes
|
|
38
|
+
* from a glance at a line. A pie names each slice with its whole-number share.
|
|
39
|
+
* An `override` replaces all of it, verbatim.
|
|
40
|
+
*
|
|
41
|
+
* The words are English. There is no labels provider to translate them yet,
|
|
42
|
+
* which is the reason the override exists.
|
|
43
|
+
*/
|
|
44
|
+
export function resolveChartSummary(input: ChartSummaryInput): string {
|
|
45
|
+
if (input.override !== undefined && input.override !== "") return input.override;
|
|
46
|
+
return input.kind === "pie" ? pieSummary(input) : cartesianSummary(input);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function cartesianSummary(input: Extract<ChartSummaryInput, { kind: "cartesian" }>): string {
|
|
50
|
+
const { series, data, formatX, formatValue } = input;
|
|
51
|
+
const first = data[0];
|
|
52
|
+
const last = data[data.length - 1];
|
|
53
|
+
if (first === undefined || last === undefined) return "Chart, no data.";
|
|
54
|
+
|
|
55
|
+
const from = formatX(first);
|
|
56
|
+
const to = formatX(last);
|
|
57
|
+
const range = data.length === 1 || from === to ? from : `${from} to ${to}`;
|
|
58
|
+
const print = (value: number, key: string): string => formatValue?.(value, key) ?? formatPieValue(value);
|
|
59
|
+
|
|
60
|
+
const parts = series.map((entry) => {
|
|
61
|
+
let low = Number.POSITIVE_INFINITY;
|
|
62
|
+
let high = Number.NEGATIVE_INFINITY;
|
|
63
|
+
let latest = Number.NaN;
|
|
64
|
+
for (const row of data) {
|
|
65
|
+
const value = asNumber(row[entry.key]);
|
|
66
|
+
if (!Number.isFinite(value)) continue;
|
|
67
|
+
if (value < low) low = value;
|
|
68
|
+
if (value > high) high = value;
|
|
69
|
+
latest = value;
|
|
70
|
+
}
|
|
71
|
+
if (!Number.isFinite(latest)) return `${entry.label}: no data.`;
|
|
72
|
+
return `${entry.label}: lowest ${print(low, entry.key)}, highest ${print(high, entry.key)}, latest ${print(latest, entry.key)}.`;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return [`Chart, ${range}.`, ...parts].join(" ");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function pieSummary(input: Extract<ChartSummaryInput, { kind: "pie" }>): string {
|
|
79
|
+
const { series, values, total } = input;
|
|
80
|
+
if (series.length === 0 || !(total > 0)) return "Pie chart, no data.";
|
|
81
|
+
const slices = series.map(
|
|
82
|
+
(entry, index) => `${entry.label} ${Math.round(pieSlicePercent(values[index] ?? 0, total))}%`
|
|
83
|
+
);
|
|
84
|
+
return `Pie chart. ${slices.join(", ")}.`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* One pie slice as a screen reader names it: the name, the value, the share.
|
|
89
|
+
*
|
|
90
|
+
* The same three things `PieChart.Tooltip` prints, through the same
|
|
91
|
+
* formatters, so the announced slice and the drawn readout agree.
|
|
92
|
+
*/
|
|
93
|
+
export function pieSliceAccessibilityLabel(input: {
|
|
94
|
+
readonly label: string;
|
|
95
|
+
readonly value: number;
|
|
96
|
+
readonly total: number;
|
|
97
|
+
readonly formatValue?: (value: number, name: string) => string;
|
|
98
|
+
readonly formatPercent?: (percent: number) => string;
|
|
99
|
+
}): string {
|
|
100
|
+
const { label, value, total, formatValue, formatPercent } = input;
|
|
101
|
+
const percent = pieSlicePercent(value, total);
|
|
102
|
+
const printedValue = formatValue?.(value, label) ?? formatPieValue(value);
|
|
103
|
+
const printedPercent = formatPercent?.(percent) ?? `${Math.round(percent)}%`;
|
|
104
|
+
return `${label}, ${printedValue}, ${printedPercent}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A tooltip row's value as `Chart.Tooltip` prints it.
|
|
109
|
+
*
|
|
110
|
+
* One function for the drawn readout and the spoken one, so the two cannot
|
|
111
|
+
* drift: the caller's formatter when there is one, else the value as written,
|
|
112
|
+
* and an empty string for a missing value.
|
|
113
|
+
*/
|
|
114
|
+
export function chartTooltipValueText(
|
|
115
|
+
row: ChartTooltipRow,
|
|
116
|
+
formatValue?: (value: unknown, key: string) => string
|
|
117
|
+
): string {
|
|
118
|
+
return formatValue?.(row.value, row.key) ?? String(row.value ?? "");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The tooltip's readout as one line of text — an adjustable chart's value.
|
|
123
|
+
*
|
|
124
|
+
* `Jan: Desktop 186, Mobile 80`. A row with nothing to print says so, rather
|
|
125
|
+
* than leaving a label hanging with no number after it.
|
|
126
|
+
*/
|
|
127
|
+
export function resolveChartReadout(input: {
|
|
128
|
+
readonly heading: string;
|
|
129
|
+
readonly rows: readonly ChartTooltipRow[];
|
|
130
|
+
readonly formatValue?: (value: unknown, key: string) => string;
|
|
131
|
+
}): string {
|
|
132
|
+
const { heading, rows, formatValue } = input;
|
|
133
|
+
const body = rows
|
|
134
|
+
.map((row) => {
|
|
135
|
+
const text = chartTooltipValueText(row, formatValue);
|
|
136
|
+
return `${row.label} ${text === "" ? "no value" : text}`;
|
|
137
|
+
})
|
|
138
|
+
.join(", ");
|
|
139
|
+
return heading === "" ? body : `${heading}: ${body}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The datum an increment or a decrement moves to.
|
|
144
|
+
*
|
|
145
|
+
* From no selection (`-1`) the first increment lands on the first datum and the
|
|
146
|
+
* first decrement on the last, so either swipe starts somewhere useful. It stops
|
|
147
|
+
* at both ends rather than wrapping — a wrap would announce January straight
|
|
148
|
+
* after December with nothing to say the series had ended. An index left past a
|
|
149
|
+
* data set that shrank is pulled back inside it. No data, no index.
|
|
150
|
+
*/
|
|
151
|
+
export function stepChartIndex(current: number, direction: 1 | -1, count: number): number {
|
|
152
|
+
if (count <= 0) return -1;
|
|
153
|
+
if (current < 0) return direction > 0 ? 0 : count - 1;
|
|
154
|
+
const next = Math.min(current, count - 1) + direction;
|
|
155
|
+
return Math.min(count - 1, Math.max(0, next));
|
|
156
|
+
}
|