@chipmobilesdk/rn-chart 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 +164 -115
- package/README.md +391 -381
- package/package.json +49 -49
- package/skills/sdk-rn-chart/SKILL.md +68 -69
- package/src/a11y/summary.ts +190 -177
- package/src/contract/datasource.ts +126 -136
- package/src/contract/errors.ts +129 -133
- package/src/contract/format.ts +168 -168
- package/src/contract/interaction.ts +70 -71
- package/src/contract/marker.ts +75 -75
- package/src/contract/props.ts +64 -60
- package/src/contract/state.ts +88 -94
- package/src/contract/strings.ts +48 -0
- package/src/contract/theme.ts +169 -177
- package/src/contract/value.ts +121 -125
- package/src/geometry/bar.ts +271 -273
- package/src/geometry/distribution.ts +197 -203
- package/src/geometry/heatmap.ts +189 -195
- package/src/geometry/hitTest.ts +117 -120
- package/src/geometry/layout.ts +162 -173
- package/src/geometry/markers.ts +55 -57
- package/src/geometry/path.ts +193 -199
- package/src/geometry/progress.ts +223 -230
- package/src/geometry/scale.ts +140 -146
- package/src/geometry/ticks.ts +145 -153
- package/src/geometry/timeSeries.ts +168 -168
- package/src/geometry/types.ts +94 -94
- package/src/index.ts +162 -176
- package/src/react/ChartBoundary.tsx +96 -96
- package/src/react/ChartFrame.tsx +596 -622
- package/src/react/ChartLegend.tsx +216 -215
- package/src/react/ChartThemeProvider.tsx +44 -48
- package/src/react/charts.tsx +154 -161
- package/src/react/useChartLayout.ts +124 -124
- package/src/react/useChartSelection.ts +152 -170
- package/src/render/Axis.tsx +120 -122
- package/src/render/Marks.tsx +173 -205
- package/src/validation/budgets.ts +113 -110
- package/src/validation/validateDatasource.ts +174 -174
package/src/render/Marks.tsx
CHANGED
|
@@ -1,205 +1,173 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Private SVG adapter for resolved geometry.
|
|
3
|
-
*
|
|
4
|
-
* Everything here consumes plain data produced by `geometry/`. Swapping the
|
|
5
|
-
* drawing substrate means rewriting this directory and nothing else.
|
|
6
|
-
*/
|
|
7
|
-
import React from 'react';
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
{
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
>
|
|
61
|
-
<Rect width="
|
|
62
|
-
<
|
|
63
|
-
</Pattern>
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}: {
|
|
132
|
-
readonly
|
|
133
|
-
readonly theme: ChartTheme;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
{label.text}
|
|
175
|
-
</SvgText>
|
|
176
|
-
))}
|
|
177
|
-
</G>
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/** Marks the currently selected point, when it still resolves. */
|
|
182
|
-
export function SelectionHighlight({
|
|
183
|
-
x,
|
|
184
|
-
y,
|
|
185
|
-
color,
|
|
186
|
-
}: {
|
|
187
|
-
readonly x: number;
|
|
188
|
-
readonly y: number;
|
|
189
|
-
readonly color: string;
|
|
190
|
-
}) {
|
|
191
|
-
return (
|
|
192
|
-
<G>
|
|
193
|
-
<Rect
|
|
194
|
-
x={x - 6}
|
|
195
|
-
y={y - 6}
|
|
196
|
-
width={12}
|
|
197
|
-
height={12}
|
|
198
|
-
rx={6}
|
|
199
|
-
fill={color}
|
|
200
|
-
opacity={0.25}
|
|
201
|
-
/>
|
|
202
|
-
<Rect x={x - 3} y={y - 3} width={6} height={6} rx={3} fill={color} />
|
|
203
|
-
</G>
|
|
204
|
-
);
|
|
205
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Private SVG adapter for resolved geometry.
|
|
3
|
+
*
|
|
4
|
+
* Everything here consumes plain data produced by `geometry/`. Swapping the
|
|
5
|
+
* drawing substrate means rewriting this directory and nothing else.
|
|
6
|
+
*/
|
|
7
|
+
import React from 'react';
|
|
8
|
+
import { Defs, G, Path, Pattern, Rect, Text as SvgText } from 'react-native-svg';
|
|
9
|
+
|
|
10
|
+
import type { SeriesPattern } from '../contract/datasource';
|
|
11
|
+
import type { ChartTheme, NonValueTexture } from '../contract/theme';
|
|
12
|
+
import type { GeometryLabel, MarkPath, MarkerLine } from '../geometry/types';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Dash patterns give series a second, non-colour identity (FR-064). `undefined`
|
|
16
|
+
* means a solid stroke.
|
|
17
|
+
*/
|
|
18
|
+
export function dashArrayFor(pattern: SeriesPattern): string | undefined {
|
|
19
|
+
switch (pattern) {
|
|
20
|
+
case 'dashed':
|
|
21
|
+
return '6,4';
|
|
22
|
+
case 'dotted':
|
|
23
|
+
return '1,4';
|
|
24
|
+
case 'dashDot':
|
|
25
|
+
return '8,3,1,3';
|
|
26
|
+
default:
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const TEXTURE_IDS: Record<NonValueTexture, string> = {
|
|
32
|
+
gap: 'rnchart-texture-gap',
|
|
33
|
+
hatch: 'rnchart-texture-hatch',
|
|
34
|
+
lockedHatch: 'rnchart-texture-locked',
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Texture definitions for the three non-value classes.
|
|
39
|
+
*
|
|
40
|
+
* Deliberately shape-based rather than colour-based: a reader in greyscale, or
|
|
41
|
+
* with reduced colour perception, must still tell a gap from a hatch from a
|
|
42
|
+
* locked cell (FR-064, SC-012).
|
|
43
|
+
*/
|
|
44
|
+
export function NonValueTextures({ theme }: { readonly theme: ChartTheme }) {
|
|
45
|
+
return (
|
|
46
|
+
<Defs>
|
|
47
|
+
{/* unknown: sparse dots — visibly "nothing here". */}
|
|
48
|
+
<Pattern id={TEXTURE_IDS.gap} patternUnits="userSpaceOnUse" width="6" height="6">
|
|
49
|
+
<Rect width="6" height="6" fill={theme.surface} />
|
|
50
|
+
<Rect x="2" y="2" width="1.5" height="1.5" fill={theme.muted} />
|
|
51
|
+
</Pattern>
|
|
52
|
+
|
|
53
|
+
{/* notApplicable: single diagonal hatch. */}
|
|
54
|
+
<Pattern id={TEXTURE_IDS.hatch} patternUnits="userSpaceOnUse" width="8" height="8">
|
|
55
|
+
<Rect width="8" height="8" fill={theme.surface} />
|
|
56
|
+
<Path d="M0 8L8 0" stroke={theme.muted} strokeWidth="1" />
|
|
57
|
+
</Pattern>
|
|
58
|
+
|
|
59
|
+
{/* withheld: cross hatch — denser, reading as deliberately closed off. */}
|
|
60
|
+
<Pattern id={TEXTURE_IDS.lockedHatch} patternUnits="userSpaceOnUse" width="8" height="8">
|
|
61
|
+
<Rect width="8" height="8" fill={theme.surface} />
|
|
62
|
+
<Path d="M0 8L8 0M0 0L8 8" stroke={theme.muted} strokeWidth="1" />
|
|
63
|
+
</Pattern>
|
|
64
|
+
</Defs>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function fillFor(mark: MarkPath): string {
|
|
69
|
+
if (mark.texture) {
|
|
70
|
+
return `url(#${TEXTURE_IDS[mark.texture]})`;
|
|
71
|
+
}
|
|
72
|
+
return mark.filled ? mark.color : 'none';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function Marks({
|
|
76
|
+
paths,
|
|
77
|
+
selectedKeys,
|
|
78
|
+
}: {
|
|
79
|
+
readonly paths: readonly MarkPath[];
|
|
80
|
+
readonly selectedKeys?: ReadonlySet<string>;
|
|
81
|
+
}) {
|
|
82
|
+
return (
|
|
83
|
+
<G>
|
|
84
|
+
{paths.map(mark => {
|
|
85
|
+
const highlighted = selectedKeys?.has(mark.seriesId);
|
|
86
|
+
return (
|
|
87
|
+
<Path
|
|
88
|
+
key={mark.key}
|
|
89
|
+
d={mark.d}
|
|
90
|
+
fill={fillFor(mark)}
|
|
91
|
+
stroke={mark.filled ? 'none' : mark.color}
|
|
92
|
+
strokeWidth={mark.filled ? 0 : highlighted ? 3 : 2}
|
|
93
|
+
strokeDasharray={mark.filled ? undefined : dashArrayFor(mark.pattern)}
|
|
94
|
+
strokeLinejoin="round"
|
|
95
|
+
strokeLinecap="round"
|
|
96
|
+
opacity={mark.opacity ?? 1}
|
|
97
|
+
/>
|
|
98
|
+
);
|
|
99
|
+
})}
|
|
100
|
+
</G>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function MarkerLines({
|
|
105
|
+
markers,
|
|
106
|
+
theme,
|
|
107
|
+
}: {
|
|
108
|
+
readonly markers: readonly MarkerLine[];
|
|
109
|
+
readonly theme: ChartTheme;
|
|
110
|
+
}) {
|
|
111
|
+
return (
|
|
112
|
+
<G>
|
|
113
|
+
{markers.map(marker => (
|
|
114
|
+
<Path
|
|
115
|
+
key={marker.id}
|
|
116
|
+
d={marker.d}
|
|
117
|
+
stroke={theme.onSurface}
|
|
118
|
+
strokeWidth={1}
|
|
119
|
+
strokeDasharray="4,3"
|
|
120
|
+
fill="none"
|
|
121
|
+
/>
|
|
122
|
+
))}
|
|
123
|
+
</G>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function GeometryLabels({
|
|
128
|
+
labels,
|
|
129
|
+
theme,
|
|
130
|
+
fontSize,
|
|
131
|
+
}: {
|
|
132
|
+
readonly labels: readonly GeometryLabel[];
|
|
133
|
+
readonly theme: ChartTheme;
|
|
134
|
+
readonly fontSize: number;
|
|
135
|
+
}) {
|
|
136
|
+
return (
|
|
137
|
+
<G>
|
|
138
|
+
{labels
|
|
139
|
+
.filter(label => !label.muted)
|
|
140
|
+
.map(label => (
|
|
141
|
+
<SvgText
|
|
142
|
+
key={label.key}
|
|
143
|
+
x={label.x}
|
|
144
|
+
y={label.y}
|
|
145
|
+
fill={theme.typography.valueLabel.color}
|
|
146
|
+
fontSize={fontSize}
|
|
147
|
+
fontWeight={theme.typography.valueLabel.fontWeight}
|
|
148
|
+
textAnchor={label.anchor}
|
|
149
|
+
>
|
|
150
|
+
{label.text}
|
|
151
|
+
</SvgText>
|
|
152
|
+
))}
|
|
153
|
+
</G>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Marks the currently selected point, when it still resolves. */
|
|
158
|
+
export function SelectionHighlight({
|
|
159
|
+
x,
|
|
160
|
+
y,
|
|
161
|
+
color,
|
|
162
|
+
}: {
|
|
163
|
+
readonly x: number;
|
|
164
|
+
readonly y: number;
|
|
165
|
+
readonly color: string;
|
|
166
|
+
}) {
|
|
167
|
+
return (
|
|
168
|
+
<G>
|
|
169
|
+
<Rect x={x - 6} y={y - 6} width={12} height={12} rx={6} fill={color} opacity={0.25} />
|
|
170
|
+
<Rect x={x - 3} y={y - 3} width={6} height={6} rx={3} fill={color} />
|
|
171
|
+
</G>
|
|
172
|
+
);
|
|
173
|
+
}
|
|
@@ -1,110 +1,113 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Dataset budgets (FR-074, FR-075, FR-076).
|
|
3
|
-
*
|
|
4
|
-
* Over budget, the chart **still renders**. Degrading silently would break the
|
|
5
|
-
* "never present a reduced picture as complete" rule, and refusing to draw
|
|
6
|
-
* would turn a performance hint into an outage. Instead we warn the developer
|
|
7
|
-
* once, in dev builds only.
|
|
8
|
-
*
|
|
9
|
-
* The `__DEV__` gate matters: it makes the check dead code that Metro strips
|
|
10
|
-
* from release bundles, which is what makes "never in production" verifiable
|
|
11
|
-
* rather than merely intended.
|
|
12
|
-
*
|
|
13
|
-
* The figures were validated against emitted element counts in
|
|
14
|
-
* specs/007-rn-chart-package/research.md R2 — element count scales with series,
|
|
15
|
-
* not with points, so these budgets are about JS work rather than view count.
|
|
16
|
-
*/
|
|
17
|
-
import
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
readonly
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
readonly
|
|
37
|
-
readonly
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
let
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Warns once per chart instance per mount. De-duplicating matters: a scrolling
|
|
73
|
-
* dashboard would otherwise flood the console on every frame.
|
|
74
|
-
*/
|
|
75
|
-
export function warnIfOverBudget<M>(
|
|
76
|
-
datasource: ChartDatasource<M>,
|
|
77
|
-
family: ChartFamily,
|
|
78
|
-
instanceKey: string,
|
|
79
|
-
): BudgetCheck {
|
|
80
|
-
const check = checkBudget(datasource, family);
|
|
81
|
-
|
|
82
|
-
if (__DEV__ && !check.withinBudget && !warnedCharts.has(instanceKey)) {
|
|
83
|
-
warnedCharts.add(instanceKey);
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Dataset budgets (FR-074, FR-075, FR-076).
|
|
3
|
+
*
|
|
4
|
+
* Over budget, the chart **still renders**. Degrading silently would break the
|
|
5
|
+
* "never present a reduced picture as complete" rule, and refusing to draw
|
|
6
|
+
* would turn a performance hint into an outage. Instead we warn the developer
|
|
7
|
+
* once, in dev builds only.
|
|
8
|
+
*
|
|
9
|
+
* The `__DEV__` gate matters: it makes the check dead code that Metro strips
|
|
10
|
+
* from release bundles, which is what makes "never in production" verifiable
|
|
11
|
+
* rather than merely intended.
|
|
12
|
+
*
|
|
13
|
+
* The figures were validated against emitted element counts in
|
|
14
|
+
* specs/007-rn-chart-package/research.md R2 — element count scales with series,
|
|
15
|
+
* not with points, so these budgets are about JS work rather than view count.
|
|
16
|
+
*/
|
|
17
|
+
import { createDiagnosticChannel } from '@chipmobilesdk/rn-core';
|
|
18
|
+
|
|
19
|
+
import type { ChartDatasource, ChartFamily } from '../contract/datasource';
|
|
20
|
+
|
|
21
|
+
export interface DatasetBudget {
|
|
22
|
+
readonly maxSeries: number;
|
|
23
|
+
readonly maxPointsPerSeries?: number;
|
|
24
|
+
readonly maxCells?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const DATASET_BUDGETS: Readonly<Record<ChartFamily, DatasetBudget>> = {
|
|
28
|
+
timeSeries: { maxSeries: 5, maxPointsPerSeries: 500 },
|
|
29
|
+
bar: { maxSeries: 5, maxPointsPerSeries: 60 },
|
|
30
|
+
distribution: { maxSeries: 1, maxPointsPerSeries: 12 },
|
|
31
|
+
progress: { maxSeries: 1, maxPointsPerSeries: 8 },
|
|
32
|
+
heatmap: { maxSeries: 1, maxCells: 1000 },
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export interface BudgetCheck {
|
|
36
|
+
readonly withinBudget: boolean;
|
|
37
|
+
readonly seriesCount: number;
|
|
38
|
+
readonly maxPointsInAnySeries: number;
|
|
39
|
+
readonly totalPoints: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function checkBudget<M>(datasource: ChartDatasource<M>, family: ChartFamily): BudgetCheck {
|
|
43
|
+
const budget = DATASET_BUDGETS[family];
|
|
44
|
+
const seriesCount = datasource.series.length;
|
|
45
|
+
|
|
46
|
+
let maxPoints = 0;
|
|
47
|
+
let totalPoints = 0;
|
|
48
|
+
for (const series of datasource.series) {
|
|
49
|
+
maxPoints = Math.max(maxPoints, series.points.length);
|
|
50
|
+
totalPoints += series.points.length;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const overSeries = seriesCount > budget.maxSeries;
|
|
54
|
+
const overPoints =
|
|
55
|
+
budget.maxPointsPerSeries !== undefined && maxPoints > budget.maxPointsPerSeries;
|
|
56
|
+
const overCells = budget.maxCells !== undefined && totalPoints > budget.maxCells;
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
withinBudget: !overSeries && !overPoints && !overCells,
|
|
60
|
+
seriesCount,
|
|
61
|
+
maxPointsInAnySeries: maxPoints,
|
|
62
|
+
totalPoints,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const warnedCharts = new Set<string>();
|
|
67
|
+
|
|
68
|
+
/** Chart diagnostics also reach the app's SDK sinks (`installSdkSink`, CORE-08). */
|
|
69
|
+
const channel = createDiagnosticChannel<'over_budget'>('rn-chart');
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Warns once per chart instance per mount. De-duplicating matters: a scrolling
|
|
73
|
+
* dashboard would otherwise flood the console on every frame.
|
|
74
|
+
*/
|
|
75
|
+
export function warnIfOverBudget<M>(
|
|
76
|
+
datasource: ChartDatasource<M>,
|
|
77
|
+
family: ChartFamily,
|
|
78
|
+
instanceKey: string,
|
|
79
|
+
): BudgetCheck {
|
|
80
|
+
const check = checkBudget(datasource, family);
|
|
81
|
+
|
|
82
|
+
if (__DEV__ && !check.withinBudget && !warnedCharts.has(instanceKey)) {
|
|
83
|
+
warnedCharts.add(instanceKey);
|
|
84
|
+
channel.emit('over_budget', 'warn', {
|
|
85
|
+
family,
|
|
86
|
+
series: check.seriesCount,
|
|
87
|
+
maxPoints: check.maxPointsInAnySeries,
|
|
88
|
+
});
|
|
89
|
+
const budget = DATASET_BUDGETS[family];
|
|
90
|
+
console.warn(
|
|
91
|
+
`[rn-chart] ${family} chart "${instanceKey}" exceeds its documented dataset budget ` +
|
|
92
|
+
`(${check.seriesCount} series x up to ${check.maxPointsInAnySeries} points; ` +
|
|
93
|
+
`budget is ${budget.maxSeries} series` +
|
|
94
|
+
(budget.maxPointsPerSeries ? ` x ${budget.maxPointsPerSeries} points` : '') +
|
|
95
|
+
(budget.maxCells ? `, ${budget.maxCells} cells` : '') +
|
|
96
|
+
'). The chart still renders and drops nothing — aggregate or downsample ' +
|
|
97
|
+
'upstream if it feels slow. This warning appears once per chart and never ' +
|
|
98
|
+
'in production builds.',
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return check;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Called on unmount so a remounted chart can warn again. */
|
|
106
|
+
export function releaseBudgetWarning(instanceKey: string): void {
|
|
107
|
+
warnedCharts.delete(instanceKey);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Test-only reset. */
|
|
111
|
+
export function __resetBudgetWarnings(): void {
|
|
112
|
+
warnedCharts.clear();
|
|
113
|
+
}
|