@chipmobilesdk/rn-chart 0.2.0 → 0.2.1

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +126 -115
  2. package/README.md +381 -381
  3. package/package.json +46 -49
  4. package/skills/sdk-rn-chart/SKILL.md +68 -69
  5. package/src/a11y/summary.ts +170 -177
  6. package/src/contract/datasource.ts +126 -136
  7. package/src/contract/errors.ts +129 -133
  8. package/src/contract/format.ts +162 -168
  9. package/src/contract/interaction.ts +70 -71
  10. package/src/contract/marker.ts +75 -75
  11. package/src/contract/props.ts +57 -60
  12. package/src/contract/state.ts +88 -94
  13. package/src/contract/theme.ts +169 -177
  14. package/src/contract/value.ts +121 -125
  15. package/src/geometry/bar.ts +271 -273
  16. package/src/geometry/distribution.ts +197 -203
  17. package/src/geometry/heatmap.ts +189 -195
  18. package/src/geometry/hitTest.ts +117 -120
  19. package/src/geometry/layout.ts +162 -173
  20. package/src/geometry/markers.ts +55 -57
  21. package/src/geometry/path.ts +193 -199
  22. package/src/geometry/progress.ts +223 -230
  23. package/src/geometry/scale.ts +140 -146
  24. package/src/geometry/ticks.ts +145 -153
  25. package/src/geometry/timeSeries.ts +168 -168
  26. package/src/geometry/types.ts +94 -94
  27. package/src/index.ts +158 -176
  28. package/src/react/ChartBoundary.tsx +96 -96
  29. package/src/react/ChartFrame.tsx +583 -622
  30. package/src/react/ChartLegend.tsx +211 -215
  31. package/src/react/ChartThemeProvider.tsx +44 -48
  32. package/src/react/charts.tsx +154 -161
  33. package/src/react/useChartLayout.ts +124 -124
  34. package/src/react/useChartSelection.ts +152 -170
  35. package/src/render/Axis.tsx +120 -122
  36. package/src/render/Marks.tsx +173 -205
  37. package/src/validation/budgets.ts +103 -110
  38. package/src/validation/validateDatasource.ts +174 -174
@@ -1,203 +1,197 @@
1
- /**
2
- * Distribution geometry: proportions between groups.
3
- *
4
- * Segments that do not sum to the whole are **not** rescaled to force a full
5
- * circle (FR-024). The arc stops short and the geometry reports `incomplete`,
6
- * so the chart can say the data is partial rather than quietly implying it is
7
- * complete.
8
- *
9
- * PURE. No React, no SVG.
10
- */
11
- import type { ChartDatasource } from '../contract/datasource';
12
- import { segmentKey } from '../contract/theme';
13
- import { isKnown, isWithheld, sumKnown } from '../contract/value';
14
- import type { HitRegion } from './hitTest';
15
- import { arcPath, rectSubpath } from './path';
16
- import type {
17
- GeometryInput,
18
- GeometryLabel,
19
- MarkPath,
20
- ResolvedGeometry,
21
- } from './types';
22
-
23
- export type DistributionShape = 'donut' | 'pie' | 'segmentedBar';
24
-
25
- export interface DistributionOptions {
26
- readonly shape?: DistributionShape;
27
- /** Donut hole as a fraction of the outer radius. */
28
- readonly innerRadiusRatio?: number;
29
- }
30
-
31
- const TAU = Math.PI * 2;
32
-
33
- export function buildDistributionGeometry<M>(
34
- datasource: ChartDatasource<M>,
35
- input: GeometryInput,
36
- options: DistributionOptions = {},
37
- ): ResolvedGeometry {
38
- const { plot } = input;
39
- const shape = options.shape ?? 'donut';
40
-
41
- const segments = datasource.series.flatMap(series =>
42
- series.points.map(point => ({ series, point })),
43
- );
44
-
45
- if (segments.length === 0 || plot.width <= 0 || plot.height <= 0) {
46
- return {
47
- plot,
48
- paths: [],
49
- markers: [],
50
- labels: [],
51
- xTicks: [],
52
- yTicks: [],
53
- hitRegions: [],
54
- incomplete: false,
55
- };
56
- }
57
-
58
- const total = sumKnown(segments.map(s => s.point.value));
59
- // Nothing known: there is no distribution to draw, and drawing a full grey
60
- // circle would imply a shape the data does not support.
61
- if (total.countedPoints === 0 || total.total <= 0) {
62
- return {
63
- plot,
64
- paths: [],
65
- markers: [],
66
- labels: [],
67
- xTicks: [],
68
- yTicks: [],
69
- hitRegions: [],
70
- incomplete: true,
71
- };
72
- }
73
-
74
- // Proportions supplied directly (each in 0..1, summing to ~1) are honoured
75
- // as given; absolute values are normalised by their own total. Either way we
76
- // never stretch a short sum to fill the circle.
77
- const suppliedAsProportions = segments.every(
78
- s => !isKnown(s.point.value) || (s.point.value.value >= 0 && s.point.value.value <= 1),
79
- );
80
- const denominator = suppliedAsProportions ? 1 : total.total;
81
- const coverage = total.total / denominator;
82
- const incomplete = total.incomplete || coverage < 0.999;
83
-
84
- const paths: MarkPath[] = [];
85
- const labels: GeometryLabel[] = [];
86
- const hitRegions: HitRegion[] = [];
87
- const colorOf = (seriesId: string, pointId: string) =>
88
- input.segmentColors?.[segmentKey(seriesId, pointId)] ?? input.seriesColors[seriesId];
89
-
90
- if (shape === 'segmentedBar') {
91
- const barHeight = Math.min(plot.height, 28);
92
- const barY = plot.y + (plot.height - barHeight) / 2;
93
- let cursor = plot.x;
94
-
95
- for (const { series, point } of segments) {
96
- if (!isKnown(point.value)) {
97
- continue;
98
- }
99
- const fraction = point.value.value / denominator;
100
- const width = plot.width * fraction;
101
-
102
- paths.push({
103
- key: `seg-${series.id}-${point.id}`,
104
- seriesId: series.id,
105
- d: rectSubpath({ x: cursor, y: barY, width, height: barHeight }),
106
- color: colorOf(series.id, point.id),
107
- pattern: 'solid',
108
- filled: true,
109
- });
110
-
111
- hitRegions.push({
112
- seriesId: series.id,
113
- pointId: point.id,
114
- x: cursor + width / 2,
115
- y: barY + barHeight / 2,
116
- bounds: { x: cursor, y: barY, width, height: barHeight },
117
- selectable: !isWithheld(point.value),
118
- });
119
-
120
- cursor += width;
121
- }
122
-
123
- return {
124
- plot,
125
- paths,
126
- markers: [],
127
- labels,
128
- xTicks: [],
129
- yTicks: [],
130
- hitRegions,
131
- incomplete,
132
- };
133
- }
134
-
135
- const cx = plot.x + plot.width / 2;
136
- const cy = plot.y + plot.height / 2;
137
- const outerRadius = Math.min(plot.width, plot.height) / 2;
138
- const innerRadius =
139
- shape === 'pie' ? 0 : outerRadius * (options.innerRadiusRatio ?? 0.6);
140
-
141
- let angle = 0;
142
-
143
- for (const { series, point } of segments) {
144
- if (!isKnown(point.value)) {
145
- continue;
146
- }
147
-
148
- const fraction = point.value.value / denominator;
149
- const sweep = fraction * TAU;
150
- const start = angle;
151
- const end = angle + sweep;
152
- angle = end;
153
-
154
- paths.push({
155
- key: `arc-${series.id}-${point.id}`,
156
- seriesId: series.id,
157
- d: arcPath({
158
- cx,
159
- cy,
160
- outerRadius,
161
- innerRadius,
162
- startAngle: start,
163
- endAngle: end,
164
- }),
165
- color: colorOf(series.id, point.id),
166
- pattern: 'solid',
167
- filled: true,
168
- });
169
-
170
- // Label at the segment's midpoint, on the ring between the two radii.
171
- const mid = start + sweep / 2 - Math.PI / 2;
172
- const labelRadius = (outerRadius + innerRadius) / 2;
173
-
174
- labels.push({
175
- key: `arc-label-${series.id}-${point.id}`,
176
- text: `${Math.round(fraction * 100)}%`,
177
- x: cx + labelRadius * Math.cos(mid),
178
- y: cy + labelRadius * Math.sin(mid),
179
- anchor: 'middle',
180
- // Hide labels on slivers where the text would not fit anyway.
181
- muted: fraction < 0.05,
182
- });
183
-
184
- hitRegions.push({
185
- seriesId: series.id,
186
- pointId: point.id,
187
- x: cx + labelRadius * Math.cos(mid),
188
- y: cy + labelRadius * Math.sin(mid),
189
- selectable: !isWithheld(point.value),
190
- });
191
- }
192
-
193
- return {
194
- plot,
195
- paths,
196
- markers: [],
197
- labels,
198
- xTicks: [],
199
- yTicks: [],
200
- hitRegions,
201
- incomplete,
202
- };
203
- }
1
+ /**
2
+ * Distribution geometry: proportions between groups.
3
+ *
4
+ * Segments that do not sum to the whole are **not** rescaled to force a full
5
+ * circle (FR-024). The arc stops short and the geometry reports `incomplete`,
6
+ * so the chart can say the data is partial rather than quietly implying it is
7
+ * complete.
8
+ *
9
+ * PURE. No React, no SVG.
10
+ */
11
+ import type { ChartDatasource } from '../contract/datasource';
12
+ import { segmentKey } from '../contract/theme';
13
+ import { isKnown, isWithheld, sumKnown } from '../contract/value';
14
+ import type { HitRegion } from './hitTest';
15
+ import { arcPath, rectSubpath } from './path';
16
+ import type { GeometryInput, GeometryLabel, MarkPath, ResolvedGeometry } from './types';
17
+
18
+ export type DistributionShape = 'donut' | 'pie' | 'segmentedBar';
19
+
20
+ export interface DistributionOptions {
21
+ readonly shape?: DistributionShape;
22
+ /** Donut hole as a fraction of the outer radius. */
23
+ readonly innerRadiusRatio?: number;
24
+ }
25
+
26
+ const TAU = Math.PI * 2;
27
+
28
+ export function buildDistributionGeometry<M>(
29
+ datasource: ChartDatasource<M>,
30
+ input: GeometryInput,
31
+ options: DistributionOptions = {},
32
+ ): ResolvedGeometry {
33
+ const { plot } = input;
34
+ const shape = options.shape ?? 'donut';
35
+
36
+ const segments = datasource.series.flatMap(series =>
37
+ series.points.map(point => ({ series, point })),
38
+ );
39
+
40
+ if (segments.length === 0 || plot.width <= 0 || plot.height <= 0) {
41
+ return {
42
+ plot,
43
+ paths: [],
44
+ markers: [],
45
+ labels: [],
46
+ xTicks: [],
47
+ yTicks: [],
48
+ hitRegions: [],
49
+ incomplete: false,
50
+ };
51
+ }
52
+
53
+ const total = sumKnown(segments.map(s => s.point.value));
54
+ // Nothing known: there is no distribution to draw, and drawing a full grey
55
+ // circle would imply a shape the data does not support.
56
+ if (total.countedPoints === 0 || total.total <= 0) {
57
+ return {
58
+ plot,
59
+ paths: [],
60
+ markers: [],
61
+ labels: [],
62
+ xTicks: [],
63
+ yTicks: [],
64
+ hitRegions: [],
65
+ incomplete: true,
66
+ };
67
+ }
68
+
69
+ // Proportions supplied directly (each in 0..1, summing to ~1) are honoured
70
+ // as given; absolute values are normalised by their own total. Either way we
71
+ // never stretch a short sum to fill the circle.
72
+ const suppliedAsProportions = segments.every(
73
+ s => !isKnown(s.point.value) || (s.point.value.value >= 0 && s.point.value.value <= 1),
74
+ );
75
+ const denominator = suppliedAsProportions ? 1 : total.total;
76
+ const coverage = total.total / denominator;
77
+ const incomplete = total.incomplete || coverage < 0.999;
78
+
79
+ const paths: MarkPath[] = [];
80
+ const labels: GeometryLabel[] = [];
81
+ const hitRegions: HitRegion[] = [];
82
+ const colorOf = (seriesId: string, pointId: string) =>
83
+ input.segmentColors?.[segmentKey(seriesId, pointId)] ?? input.seriesColors[seriesId];
84
+
85
+ if (shape === 'segmentedBar') {
86
+ const barHeight = Math.min(plot.height, 28);
87
+ const barY = plot.y + (plot.height - barHeight) / 2;
88
+ let cursor = plot.x;
89
+
90
+ for (const { series, point } of segments) {
91
+ if (!isKnown(point.value)) {
92
+ continue;
93
+ }
94
+ const fraction = point.value.value / denominator;
95
+ const width = plot.width * fraction;
96
+
97
+ paths.push({
98
+ key: `seg-${series.id}-${point.id}`,
99
+ seriesId: series.id,
100
+ d: rectSubpath({ x: cursor, y: barY, width, height: barHeight }),
101
+ color: colorOf(series.id, point.id),
102
+ pattern: 'solid',
103
+ filled: true,
104
+ });
105
+
106
+ hitRegions.push({
107
+ seriesId: series.id,
108
+ pointId: point.id,
109
+ x: cursor + width / 2,
110
+ y: barY + barHeight / 2,
111
+ bounds: { x: cursor, y: barY, width, height: barHeight },
112
+ selectable: !isWithheld(point.value),
113
+ });
114
+
115
+ cursor += width;
116
+ }
117
+
118
+ return {
119
+ plot,
120
+ paths,
121
+ markers: [],
122
+ labels,
123
+ xTicks: [],
124
+ yTicks: [],
125
+ hitRegions,
126
+ incomplete,
127
+ };
128
+ }
129
+
130
+ const cx = plot.x + plot.width / 2;
131
+ const cy = plot.y + plot.height / 2;
132
+ const outerRadius = Math.min(plot.width, plot.height) / 2;
133
+ const innerRadius = shape === 'pie' ? 0 : outerRadius * (options.innerRadiusRatio ?? 0.6);
134
+
135
+ let angle = 0;
136
+
137
+ for (const { series, point } of segments) {
138
+ if (!isKnown(point.value)) {
139
+ continue;
140
+ }
141
+
142
+ const fraction = point.value.value / denominator;
143
+ const sweep = fraction * TAU;
144
+ const start = angle;
145
+ const end = angle + sweep;
146
+ angle = end;
147
+
148
+ paths.push({
149
+ key: `arc-${series.id}-${point.id}`,
150
+ seriesId: series.id,
151
+ d: arcPath({
152
+ cx,
153
+ cy,
154
+ outerRadius,
155
+ innerRadius,
156
+ startAngle: start,
157
+ endAngle: end,
158
+ }),
159
+ color: colorOf(series.id, point.id),
160
+ pattern: 'solid',
161
+ filled: true,
162
+ });
163
+
164
+ // Label at the segment's midpoint, on the ring between the two radii.
165
+ const mid = start + sweep / 2 - Math.PI / 2;
166
+ const labelRadius = (outerRadius + innerRadius) / 2;
167
+
168
+ labels.push({
169
+ key: `arc-label-${series.id}-${point.id}`,
170
+ text: `${Math.round(fraction * 100)}%`,
171
+ x: cx + labelRadius * Math.cos(mid),
172
+ y: cy + labelRadius * Math.sin(mid),
173
+ anchor: 'middle',
174
+ // Hide labels on slivers where the text would not fit anyway.
175
+ muted: fraction < 0.05,
176
+ });
177
+
178
+ hitRegions.push({
179
+ seriesId: series.id,
180
+ pointId: point.id,
181
+ x: cx + labelRadius * Math.cos(mid),
182
+ y: cy + labelRadius * Math.sin(mid),
183
+ selectable: !isWithheld(point.value),
184
+ });
185
+ }
186
+
187
+ return {
188
+ plot,
189
+ paths,
190
+ markers: [],
191
+ labels,
192
+ xTicks: [],
193
+ yTicks: [],
194
+ hitRegions,
195
+ incomplete,
196
+ };
197
+ }