@littlepress/renderer-web 0.0.0-stage → 1.0.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/LICENSE +202 -0
- package/NOTICE +20 -0
- package/README.md +229 -2
- package/dist/answer/answerComponents.d.ts +107 -0
- package/dist/answer/answerComponents.js +637 -0
- package/dist/answer/download.d.ts +63 -0
- package/dist/answer/download.js +109 -0
- package/dist/answer/downloadExport.d.ts +44 -0
- package/dist/answer/downloadExport.js +266 -0
- package/dist/answer/hostValues.d.ts +28 -0
- package/dist/answer/hostValues.js +379 -0
- package/dist/answer/mapComponent.d.ts +6 -0
- package/dist/answer/mapComponent.js +213 -0
- package/dist/answer/math.d.ts +57 -0
- package/dist/answer/math.js +168 -0
- package/dist/answer/surfaceStream.d.ts +51 -0
- package/dist/answer/surfaceStream.js +260 -0
- package/dist/answer/tableLayout.d.ts +42 -0
- package/dist/answer/tableLayout.js +89 -0
- package/dist/controls/ActionFeedback.d.ts +5 -0
- package/dist/controls/ActionFeedback.js +43 -0
- package/dist/controls/actionDispatch.d.ts +51 -0
- package/dist/controls/actionDispatch.js +211 -0
- package/dist/controls/actionOutcome.generated.d.ts +64 -0
- package/dist/controls/actionOutcome.generated.js +64 -0
- package/dist/controls/calculationPublisher.d.ts +14 -0
- package/dist/controls/calculationPublisher.js +6 -0
- package/dist/controls/calculations.d.ts +20 -0
- package/dist/controls/calculations.generated.d.ts +73 -0
- package/dist/controls/calculations.generated.js +85 -0
- package/dist/controls/calculations.js +208 -0
- package/dist/controls/controlState.d.ts +84 -0
- package/dist/controls/controlState.js +256 -0
- package/dist/controls/fieldMessages.d.ts +41 -0
- package/dist/controls/fieldMessages.js +81 -0
- package/dist/controls/fieldValidation.d.ts +19 -0
- package/dist/controls/fieldValidation.js +98 -0
- package/dist/controls/formComponents.d.ts +23 -0
- package/dist/controls/formComponents.js +738 -0
- package/dist/controls/formProgress.d.ts +63 -0
- package/dist/controls/formProgress.js +122 -0
- package/dist/controls/inputDrafts.d.ts +55 -0
- package/dist/controls/inputDrafts.js +183 -0
- package/dist/controls/inputValidation.d.ts +33 -0
- package/dist/controls/inputValidation.js +119 -0
- package/dist/controls/localWrites.d.ts +9 -0
- package/dist/controls/localWrites.js +29 -0
- package/dist/core/A2uiSurface.d.ts +13 -0
- package/dist/core/A2uiSurface.js +15 -0
- package/dist/core/AtlasIcon.d.ts +18 -0
- package/dist/core/AtlasIcon.js +22 -0
- package/dist/core/AtlasMedia.d.ts +22 -0
- package/dist/core/AtlasMedia.js +45 -0
- package/dist/core/Carousel.d.ts +61 -0
- package/dist/core/Carousel.js +183 -0
- package/dist/core/CoreMedia.d.ts +19 -0
- package/dist/core/CoreMedia.js +25 -0
- package/dist/core/FoundationA2uiSurface.d.ts +86 -0
- package/dist/core/FoundationA2uiSurface.js +440 -0
- package/dist/core/ImagePreview.d.ts +42 -0
- package/dist/core/ImagePreview.js +73 -0
- package/dist/core/LucideIcon.d.ts +19 -0
- package/dist/core/LucideIcon.js +42 -0
- package/dist/core/SurfaceLink.d.ts +35 -0
- package/dist/core/SurfaceLink.js +60 -0
- package/dist/core/appearance/aegis.d.ts +49 -0
- package/dist/core/appearance/aegis.js +34 -0
- package/dist/core/appearance/colour.d.ts +24 -0
- package/dist/core/appearance/colour.js +103 -0
- package/dist/core/appearance/css.d.ts +17 -0
- package/dist/core/appearance/css.js +153 -0
- package/dist/core/appearance/presets.generated.d.ts +4 -0
- package/dist/core/appearance/presets.generated.js +1184 -0
- package/dist/core/appearance/react.d.ts +29 -0
- package/dist/core/appearance/react.js +110 -0
- package/dist/core/appearance/resolve.d.ts +76 -0
- package/dist/core/appearance/resolve.js +127 -0
- package/dist/core/appearance/textScale.d.ts +11 -0
- package/dist/core/appearance/textScale.js +51 -0
- package/dist/core/appearance/theme.d.ts +138 -0
- package/dist/core/appearance/theme.js +274 -0
- package/dist/core/baseComponents.d.ts +53 -0
- package/dist/core/baseComponents.js +480 -0
- package/dist/core/binding.d.ts +46 -0
- package/dist/core/binding.js +118 -0
- package/dist/core/bundle.d.ts +23 -0
- package/dist/core/bundle.js +75 -0
- package/dist/core/catalog/catalog-index.generated.d.ts +44 -0
- package/dist/core/catalog/catalog-index.generated.js +79 -0
- package/dist/core/catalog/icons.generated.d.ts +6 -0
- package/dist/core/catalog/icons.generated.js +1788 -0
- package/dist/core/catalog.d.ts +6 -0
- package/dist/core/catalog.generated.d.ts +50 -0
- package/dist/core/catalog.generated.js +2174 -0
- package/dist/core/catalog.js +8 -0
- package/dist/core/chartRows.d.ts +7 -0
- package/dist/core/chartRows.js +38 -0
- package/dist/core/component-support.d.ts +39 -0
- package/dist/core/component-support.js +127 -0
- package/dist/core/components/actions.d.ts +4 -0
- package/dist/core/components/actions.js +21 -0
- package/dist/core/components/cards.d.ts +9 -0
- package/dist/core/components/cards.js +227 -0
- package/dist/core/components/charts.d.ts +18 -0
- package/dist/core/components/charts.js +412 -0
- package/dist/core/components/comparisonTable.d.ts +50 -0
- package/dist/core/components/comparisonTable.js +89 -0
- package/dist/core/components/content.d.ts +13 -0
- package/dist/core/components/content.js +108 -0
- package/dist/core/components/seriesCharts.d.ts +531 -0
- package/dist/core/components/seriesCharts.js +1053 -0
- package/dist/core/components/stepFlow.d.ts +7 -0
- package/dist/core/components/stepFlow.js +13 -0
- package/dist/core/components.full.d.ts +10 -0
- package/dist/core/components.full.js +41 -0
- package/dist/core/context.d.ts +207 -0
- package/dist/core/context.js +172 -0
- package/dist/core/coverage.d.ts +30 -0
- package/dist/core/coverage.js +93 -0
- package/dist/core/foundation.d.ts +21 -0
- package/dist/core/foundation.js +22 -0
- package/dist/core/fullTheme.d.ts +5 -0
- package/dist/core/fullTheme.js +9 -0
- package/dist/core/iconNodes.generated.d.ts +7 -0
- package/dist/core/iconNodes.generated.js +5 -0
- package/dist/core/lucideIcons.d.ts +5 -0
- package/dist/core/lucideIcons.js +7 -0
- package/dist/core/messages.d.ts +111 -0
- package/dist/core/messages.js +134 -0
- package/dist/core/pointer.d.ts +17 -0
- package/dist/core/pointer.js +104 -0
- package/dist/core/registry.d.ts +12 -0
- package/dist/core/registry.js +114 -0
- package/dist/core/runtime.d.ts +8 -0
- package/dist/core/runtime.js +6 -0
- package/dist/core/state.d.ts +73 -0
- package/dist/core/state.js +274 -0
- package/dist/core/surfaceMotion.d.ts +5 -0
- package/dist/core/surfaceMotion.js +46 -0
- package/dist/core/theme.d.ts +5 -0
- package/dist/core/theme.js +7 -0
- package/dist/core/useSurfaceImage.d.ts +20 -0
- package/dist/core/useSurfaceImage.js +65 -0
- package/dist/graph/RelationshipGraph.d.ts +14 -0
- package/dist/graph/RelationshipGraph.js +206 -0
- package/dist/graph/model.d.ts +20 -0
- package/dist/graph/model.js +36 -0
- package/dist/graph/program/assets.d.ts +3 -0
- package/dist/graph/program/assets.js +3 -0
- package/dist/graph/program/bridge.d.ts +6 -0
- package/dist/graph/program/bridge.generated.d.ts +136 -0
- package/dist/graph/program/bridge.generated.js +1 -0
- package/dist/graph/program/bridge.js +24 -0
- package/dist/graph/program/bridge.schema.json +717 -0
- package/dist/graph/program/bridgeValidator.cjs +2 -0
- package/dist/graph/program/bridgeValidator.d.cts +4 -0
- package/dist/graph/program/runner.d.ts +1 -0
- package/dist/graph/program/runner.html +2193 -0
- package/dist/graph/program/runner.js +157 -0
- package/dist/graph/program/runtime-manifest.json +1 -0
- package/dist/graph/program/view.d.ts +11 -0
- package/dist/graph/program/view.js +210 -0
- package/dist/graph/program/worker.d.ts +1 -0
- package/dist/graph/program/worker.js +2186 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +45 -0
- package/dist/internal/dsh.d.ts +4 -0
- package/dist/internal/dsh.js +3 -0
- package/dist/internal/relationship-graph.d.ts +4 -0
- package/dist/internal/relationship-graph.js +7202 -0
- package/dist/page/layoutComponents.d.ts +6 -0
- package/dist/page/layoutComponents.js +123 -0
- package/dist/page/layoutGeometry.d.ts +68 -0
- package/dist/page/layoutGeometry.js +174 -0
- package/dist/page/pageFormat.d.ts +34 -0
- package/dist/page/pageFormat.js +119 -0
- package/dist/page/printFit.d.ts +81 -0
- package/dist/page/printFit.js +338 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.js +2 -0
- package/dist/report/locate.d.ts +42 -0
- package/dist/report/locate.js +34 -0
- package/dist/styles/renderer.css +4580 -0
- package/licenses/LUCIDE-LICENSE.txt +43 -0
- package/licenses/QUICKJS-EMSCRIPTEN-LICENSE.txt +21 -0
- package/licenses/QUICKJS-VARIANT-LICENSE.txt +47 -0
- package/package.json +68 -3
|
@@ -0,0 +1,1053 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Geometry and text for the multi-series charts.
|
|
3
|
+
*
|
|
4
|
+
* The renderer only ever receives records plus the field names to read; it
|
|
5
|
+
* never reshapes or pads the data. Everything that can be decided without JSX
|
|
6
|
+
* lives here so it stays testable and so `charts.tsx` keeps no numbers of its
|
|
7
|
+
* own. Colour is chosen by series index through the `--series-1..4` tokens, so
|
|
8
|
+
* this module returns indices, never colour values.
|
|
9
|
+
*/
|
|
10
|
+
/** How many series one chart may draw; the compiler rejects more upstream. */
|
|
11
|
+
export const MAX_SERIES = 4;
|
|
12
|
+
/** Stroke patterns per series index, so a line is identifiable without colour. */
|
|
13
|
+
export const SERIES_DASH = [undefined, "7 4", "2 4", "10 4 2 4"];
|
|
14
|
+
function isFiniteNumber(value) {
|
|
15
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
16
|
+
}
|
|
17
|
+
/** U+2212, the minus sign report text is written with ("−620"), not the hyphen `String()` prints. */
|
|
18
|
+
export const MINUS_SIGN = "−";
|
|
19
|
+
/**
|
|
20
|
+
* A chart value as it is printed on the card, on its axis and in its text
|
|
21
|
+
* alternative: the integer digits grouped in threes with a comma, and a true
|
|
22
|
+
* minus sign, so 2320 reads "2,320" and -620 reads "−620" — the way the numbers
|
|
23
|
+
* in a report's tables and sentences are written. Nothing is rounded: every
|
|
24
|
+
* digit `String()` prints is kept (1234.5678 → "1,234.5678"). A number that only
|
|
25
|
+
* prints in exponent form (1e21, 1e-7) keeps that form apart from its sign.
|
|
26
|
+
*
|
|
27
|
+
* Only values go through here. A category name that happens to be a number,
|
|
28
|
+
* such as the year 2024, is a label and is printed as sent.
|
|
29
|
+
*/
|
|
30
|
+
export function formatChartNumber(value) {
|
|
31
|
+
const plain = String(value);
|
|
32
|
+
const negative = plain.startsWith("-");
|
|
33
|
+
const digits = negative ? plain.slice(1) : plain;
|
|
34
|
+
const match = /^(\d+)(\.\d+)?$/.exec(digits);
|
|
35
|
+
const body = match ? match[1].replace(/\B(?=(\d{3})+$)/g, ",") + (match[2] ?? "") : digits;
|
|
36
|
+
return negative ? `${MINUS_SIGN}${body}` : body;
|
|
37
|
+
}
|
|
38
|
+
function readLabel(row, field, index) {
|
|
39
|
+
const value = row[field];
|
|
40
|
+
if (typeof value === "string" && value.length > 0)
|
|
41
|
+
return value;
|
|
42
|
+
if (isFiniteNumber(value))
|
|
43
|
+
return String(value);
|
|
44
|
+
return `Row ${index + 1}`;
|
|
45
|
+
}
|
|
46
|
+
function niceStep(rough) {
|
|
47
|
+
if (!(rough > 0))
|
|
48
|
+
return 1;
|
|
49
|
+
const exponent = Math.floor(Math.log10(rough));
|
|
50
|
+
const magnitude = 10 ** exponent;
|
|
51
|
+
const fraction = rough / magnitude;
|
|
52
|
+
const step = fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 5 ? 5 : 10;
|
|
53
|
+
return step * magnitude;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Rounded axis bounds and tick values. The axis always contains zero so bar
|
|
57
|
+
* lengths stay proportional to the numbers they show.
|
|
58
|
+
*/
|
|
59
|
+
export function valueAxis(values, targetTicks = 4, includeZero = true) {
|
|
60
|
+
const finite = values.filter(isFiniteNumber);
|
|
61
|
+
const rawMin = includeZero ? Math.min(0, ...finite) : Math.min(...finite);
|
|
62
|
+
const rawMax = includeZero ? Math.max(0, ...finite) : Math.max(...finite);
|
|
63
|
+
const span = rawMax - rawMin;
|
|
64
|
+
if (span === 0)
|
|
65
|
+
return { min: 0, max: 1, ticks: [0, 1] };
|
|
66
|
+
const step = niceStep(span / Math.max(1, targetTicks));
|
|
67
|
+
const min = Math.floor(rawMin / step) * step;
|
|
68
|
+
const max = Math.ceil(rawMax / step) * step;
|
|
69
|
+
const ticks = [];
|
|
70
|
+
// Integer arithmetic on the step index avoids float drift on tick labels.
|
|
71
|
+
for (let index = 0; min + index * step <= max + step / 1000; index += 1) {
|
|
72
|
+
ticks.push(Number((min + index * step).toPrecision(12)));
|
|
73
|
+
}
|
|
74
|
+
return { min, max, ticks };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Normalizes the bound rows into a drawable model, or `null` when there is
|
|
78
|
+
* nothing honest to draw. `null` is what sends the component to its catalog
|
|
79
|
+
* fallback; the renderer never draws half a chart.
|
|
80
|
+
*
|
|
81
|
+
* `seriesFields` is required. Without it there is no chart: JSON object key
|
|
82
|
+
* order is not reliable across platforms and storage, so working the series
|
|
83
|
+
* out from the rows would colour and order them differently on each client.
|
|
84
|
+
*/
|
|
85
|
+
export function readSeriesChart(rows, options = {}) {
|
|
86
|
+
const usable = rows.filter((row) => typeof row === "object" && row !== null && !Array.isArray(row));
|
|
87
|
+
if (usable.length === 0)
|
|
88
|
+
return null;
|
|
89
|
+
const labelField = options.labelField && options.labelField.length > 0 ? options.labelField : "label";
|
|
90
|
+
const fields = (options.seriesFields ?? [])
|
|
91
|
+
.filter((field) => typeof field === "string" && field.length > 0)
|
|
92
|
+
.slice(0, MAX_SERIES);
|
|
93
|
+
if (fields.length === 0)
|
|
94
|
+
return null;
|
|
95
|
+
const labels = (options.seriesLabels ?? []).filter((label) => typeof label === "string" && label.length > 0);
|
|
96
|
+
const series = fields.map((field, index) => ({
|
|
97
|
+
field,
|
|
98
|
+
// Labels only apply when the author gave one per field; a partial list
|
|
99
|
+
// would silently rename the wrong series.
|
|
100
|
+
label: labels.length === fields.length ? labels[index] : field,
|
|
101
|
+
index,
|
|
102
|
+
values: usable.map((row) => (isFiniteNumber(row[field]) ? row[field] : null)),
|
|
103
|
+
}));
|
|
104
|
+
if (series.every((entry) => entry.values.every((value) => value === null)))
|
|
105
|
+
return null;
|
|
106
|
+
const axis = valueAxis(series.flatMap((entry) => entry.values.filter(isFiniteNumber)));
|
|
107
|
+
return {
|
|
108
|
+
labels: usable.map((row, index) => readLabel(row, labelField, index)),
|
|
109
|
+
series,
|
|
110
|
+
min: axis.min,
|
|
111
|
+
max: axis.max,
|
|
112
|
+
ticks: axis.ticks,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* A line is read for its shape, so it fills the plot between its own extremes
|
|
117
|
+
* instead of being pinned to zero.
|
|
118
|
+
*/
|
|
119
|
+
export function lineChartModel(model) {
|
|
120
|
+
const values = model.series.flatMap((entry) => entry.values.filter(isFiniteNumber));
|
|
121
|
+
if (values.length === 0)
|
|
122
|
+
return model;
|
|
123
|
+
const axis = valueAxis(values, 3, false);
|
|
124
|
+
return { ...model, min: axis.min, max: axis.max, ticks: axis.ticks };
|
|
125
|
+
}
|
|
126
|
+
/** Where a value sits on the axis, as a percentage from the axis minimum. */
|
|
127
|
+
export function axisPercent(model, value) {
|
|
128
|
+
const span = model.max - model.min;
|
|
129
|
+
if (span <= 0)
|
|
130
|
+
return 0;
|
|
131
|
+
return ((value - model.min) / span) * 100;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* A stacked chart is measured against its row totals, not its single values;
|
|
135
|
+
* scaling it like a grouped chart would push the top of every stack off the plot.
|
|
136
|
+
*/
|
|
137
|
+
export function barChartModel(model, variant) {
|
|
138
|
+
if (variant !== "stacked")
|
|
139
|
+
return model;
|
|
140
|
+
const totals = model.labels.flatMap((_label, rowIndex) => {
|
|
141
|
+
let positive = 0;
|
|
142
|
+
let negative = 0;
|
|
143
|
+
for (const entry of model.series) {
|
|
144
|
+
const value = entry.values[rowIndex];
|
|
145
|
+
if (value === null || value === undefined)
|
|
146
|
+
continue;
|
|
147
|
+
if (value < 0)
|
|
148
|
+
negative += value;
|
|
149
|
+
else
|
|
150
|
+
positive += value;
|
|
151
|
+
}
|
|
152
|
+
return [positive, negative];
|
|
153
|
+
});
|
|
154
|
+
const axis = valueAxis(totals);
|
|
155
|
+
return { ...model, min: axis.min, max: axis.max, ticks: axis.ticks };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Bar rectangles per row. Grouped bars all grow from the zero line; stacked
|
|
159
|
+
* bars sit on top of each other in series order. Rows with a missing value
|
|
160
|
+
* simply have one fewer segment — nothing is padded to zero.
|
|
161
|
+
*/
|
|
162
|
+
/**
|
|
163
|
+
* The longest value label an upright bar chart prints, in characters. Grouped
|
|
164
|
+
* labels sit centred over bars 18px wide, so a label wider than the gap to the
|
|
165
|
+
* next bar ran into its neighbour ("2,1002,320"); the stylesheet widens the gap
|
|
166
|
+
* between the bars of one category by this many digit widths.
|
|
167
|
+
*/
|
|
168
|
+
export function widestValueLabel(rows) {
|
|
169
|
+
return rows.reduce((widest, row) => row.segments.reduce((most, segment) => Math.max(most, formatChartNumber(segment.value).length), widest), 0);
|
|
170
|
+
}
|
|
171
|
+
export function barRows(model, variant) {
|
|
172
|
+
const zero = axisPercent(model, 0);
|
|
173
|
+
return model.labels.map((label, rowIndex) => {
|
|
174
|
+
const segments = [];
|
|
175
|
+
let stackTop = zero;
|
|
176
|
+
let stackBottom = zero;
|
|
177
|
+
for (const entry of model.series) {
|
|
178
|
+
const value = entry.values[rowIndex];
|
|
179
|
+
if (value === null || value === undefined)
|
|
180
|
+
continue;
|
|
181
|
+
const point = axisPercent(model, value);
|
|
182
|
+
if (variant === "stacked") {
|
|
183
|
+
const size = Math.abs(point - zero);
|
|
184
|
+
// Negative stacked values grow downwards from the same baseline; the
|
|
185
|
+
// compiler rejects them, but the renderer must not overlap if one slips through.
|
|
186
|
+
const start = value < 0 ? stackBottom - size : stackTop;
|
|
187
|
+
if (value < 0)
|
|
188
|
+
stackBottom -= size;
|
|
189
|
+
else
|
|
190
|
+
stackTop += size;
|
|
191
|
+
segments.push({ seriesIndex: entry.index, sizePercent: size, startPercent: start, value });
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
segments.push({
|
|
195
|
+
seriesIndex: entry.index,
|
|
196
|
+
sizePercent: Math.abs(point - zero),
|
|
197
|
+
startPercent: Math.min(point, zero),
|
|
198
|
+
value,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return { label, segments };
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
export function roundedBarEnd(row, segment, variant) {
|
|
206
|
+
if (!(segment.sizePercent > 0) || segment.value === 0)
|
|
207
|
+
return null;
|
|
208
|
+
const positive = segment.value > 0;
|
|
209
|
+
if (variant === "stacked") {
|
|
210
|
+
const sameSide = row.segments.filter((item) => item.sizePercent > 0 && item.value !== 0 && item.value > 0 === positive);
|
|
211
|
+
// Series stack in order, so the last one on a side is the one furthest from zero.
|
|
212
|
+
if (sameSide[sameSide.length - 1]?.seriesIndex !== segment.seriesIndex)
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
return positive ? "top" : "bottom";
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Whether every bar can carry its own value beside or above it.
|
|
219
|
+
* Horizontal rows always have room beside the track; a vertical group only has
|
|
220
|
+
* room while the labels stay wider apart than they are long, which is eight
|
|
221
|
+
* bars across a 390px card. Stacked segments never do: their end is a running
|
|
222
|
+
* total the renderer is not allowed to compute.
|
|
223
|
+
*/
|
|
224
|
+
export function showsValueLabels(model, layout) {
|
|
225
|
+
if (layout.variant === "stacked")
|
|
226
|
+
return false;
|
|
227
|
+
if (layout.horizontal)
|
|
228
|
+
return true;
|
|
229
|
+
return model.series.length <= 2 && model.labels.length * model.series.length <= 8;
|
|
230
|
+
}
|
|
231
|
+
/** A value axis only appears when the bars could not be labelled directly. */
|
|
232
|
+
export function showsValueAxis(model, layout) {
|
|
233
|
+
return !showsValueLabels(model, layout);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* One unnamed series needs nothing to tell it apart from another, so it
|
|
237
|
+
* draws without a legend.
|
|
238
|
+
*/
|
|
239
|
+
export function showsLegend(model, named) {
|
|
240
|
+
return model.series.length > 1 || named;
|
|
241
|
+
}
|
|
242
|
+
/** Inline size of the line plot inside the narrowest card (310px x 284/320). */
|
|
243
|
+
const NARROW_PLOT_WIDTH = 275;
|
|
244
|
+
/** Smallest gap between two markers that still reads as two points, in px. */
|
|
245
|
+
const MARKER_MIN_GAP = 16;
|
|
246
|
+
/**
|
|
247
|
+
* Whether every point on a line or an area gets its own marker.
|
|
248
|
+
*
|
|
249
|
+
* A month of daily points is 31 markers across 275px: 9px apart, drawn 5.5px
|
|
250
|
+
* wide, which turns the line into a chain of beads and hides the shape the chart
|
|
251
|
+
* exists to show. Past that only the two endpoints keep a marker, because those
|
|
252
|
+
* are the points the endpoint values name. Nothing is dropped from the data or
|
|
253
|
+
* the text table — this is the marker layer only, and a chart inside the old
|
|
254
|
+
* twelve-point limit is unchanged.
|
|
255
|
+
*/
|
|
256
|
+
export function showsPointMarkers(model) {
|
|
257
|
+
if (model.labels.length < 2)
|
|
258
|
+
return true;
|
|
259
|
+
return NARROW_PLOT_WIDTH / (model.labels.length - 1) >= MARKER_MIN_GAP;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Which series get their first and last value called out on a line. Three or
|
|
263
|
+
* more sets of endpoint labels collide at 390px, so past two only the lead
|
|
264
|
+
* series is labelled; the rest stay in the legend and in the text table.
|
|
265
|
+
*/
|
|
266
|
+
export function endpointSeries(model) {
|
|
267
|
+
return model.series.length <= 2 ? model.series : model.series.slice(0, 1);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Ticks a narrow horizontal scale can label without crowding: the ends, plus
|
|
271
|
+
* the middle one only when the tick count actually has a middle.
|
|
272
|
+
*/
|
|
273
|
+
export function edgeTicks(model) {
|
|
274
|
+
const { ticks } = model;
|
|
275
|
+
if (ticks.length <= 3)
|
|
276
|
+
return ticks;
|
|
277
|
+
const ends = [ticks[0], ticks[ticks.length - 1]];
|
|
278
|
+
return ticks.length % 2 === 1 ? [ends[0], ticks[(ticks.length - 1) / 2], ends[1]] : ends;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Point coordinates inside `plot`, plus the SVG path for each series. A run of
|
|
282
|
+
* points breaks wherever a row has no number, so a gap reads as a gap.
|
|
283
|
+
*/
|
|
284
|
+
export function linePaths(model, plot) {
|
|
285
|
+
const lastIndex = Math.max(1, model.labels.length - 1);
|
|
286
|
+
return model.series.map((entry) => {
|
|
287
|
+
const points = [];
|
|
288
|
+
const commands = [];
|
|
289
|
+
let penDown = false;
|
|
290
|
+
entry.values.forEach((value, rowIndex) => {
|
|
291
|
+
if (value === null || value === undefined) {
|
|
292
|
+
penDown = false;
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const x = plot.x + (rowIndex / lastIndex) * plot.width;
|
|
296
|
+
const y = plot.y + plot.height - (axisPercent(model, value) / 100) * plot.height;
|
|
297
|
+
points.push({ x, y, value });
|
|
298
|
+
commands.push(`${penDown ? "L" : "M"}${x.toFixed(2)} ${y.toFixed(2)}`);
|
|
299
|
+
penDown = true;
|
|
300
|
+
});
|
|
301
|
+
return { seriesIndex: entry.index, path: commands.join(" "), points };
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Stacked areas are measured against row totals, the way stacked bars are; an
|
|
306
|
+
* overlay keeps the shared axis so the two shapes can be compared directly.
|
|
307
|
+
* Either way the axis contains zero, because an area is read from its baseline.
|
|
308
|
+
*/
|
|
309
|
+
export function areaChartModel(model, variant) {
|
|
310
|
+
if (variant !== "stacked")
|
|
311
|
+
return model;
|
|
312
|
+
const totals = model.labels.map((_label, rowIndex) => model.series.reduce((sum, entry) => {
|
|
313
|
+
const value = entry.values[rowIndex];
|
|
314
|
+
return isFiniteNumber(value) ? sum + value : sum;
|
|
315
|
+
}, 0));
|
|
316
|
+
const axis = valueAxis(totals);
|
|
317
|
+
return { ...model, min: axis.min, max: axis.max, ticks: axis.ticks };
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* The filled shapes and their top edges. A row with no number ends the run, so
|
|
321
|
+
* a gap in the data is a gap in the area rather than a silent zero; the shape
|
|
322
|
+
* always closes on the baseline, never on a rounded cap.
|
|
323
|
+
*/
|
|
324
|
+
export function areaBands(model, variant, plot) {
|
|
325
|
+
const lastIndex = Math.max(1, model.labels.length - 1);
|
|
326
|
+
const atX = (rowIndex) => plot.x + (rowIndex / lastIndex) * plot.width;
|
|
327
|
+
const atY = (value) => plot.y + plot.height - (axisPercent(model, value) / 100) * plot.height;
|
|
328
|
+
const base = model.labels.map(() => 0);
|
|
329
|
+
return model.series.map((entry) => {
|
|
330
|
+
const areas = [];
|
|
331
|
+
const lines = [];
|
|
332
|
+
const points = [];
|
|
333
|
+
let run = [];
|
|
334
|
+
const close = () => {
|
|
335
|
+
if (run.length >= 2) {
|
|
336
|
+
const edge = run.map((step, index) => `${index === 0 ? "M" : "L"}${atX(step.rowIndex).toFixed(2)} ${atY(step.top).toFixed(2)}`);
|
|
337
|
+
const floor = [...run]
|
|
338
|
+
.reverse()
|
|
339
|
+
.map((step) => `L${atX(step.rowIndex).toFixed(2)} ${atY(step.floor).toFixed(2)}`);
|
|
340
|
+
lines.push(edge.join(" "));
|
|
341
|
+
areas.push(`${edge.join(" ")} ${floor.join(" ")} Z`);
|
|
342
|
+
}
|
|
343
|
+
run = [];
|
|
344
|
+
};
|
|
345
|
+
entry.values.forEach((value, rowIndex) => {
|
|
346
|
+
if (!isFiniteNumber(value)) {
|
|
347
|
+
close();
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const floor = variant === "stacked" ? base[rowIndex] : model.min;
|
|
351
|
+
const top = variant === "stacked" ? floor + value : value;
|
|
352
|
+
if (variant === "stacked")
|
|
353
|
+
base[rowIndex] = top;
|
|
354
|
+
run.push({ rowIndex, top, floor });
|
|
355
|
+
points.push({ x: atX(rowIndex), y: atY(top), value });
|
|
356
|
+
});
|
|
357
|
+
close();
|
|
358
|
+
return { seriesIndex: entry.index, areas, lines, points };
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
/** Radius of the hole, as a share of the outer radius. A pie has none.
|
|
362
|
+
* 0.618 is the ratio Swift Charts uses in its own donut example. */
|
|
363
|
+
const DONUT_HOLE = 0.618;
|
|
364
|
+
function arcPoint(centre, radius, degrees) {
|
|
365
|
+
const radians = ((degrees - 90) * Math.PI) / 180;
|
|
366
|
+
return [centre + radius * Math.cos(radians), centre + radius * Math.sin(radians)];
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* An arc drawn as two halves. A single arc command whose ends round to the same
|
|
370
|
+
* point draws nothing, which silently loses a track or a slice that covers the
|
|
371
|
+
* whole circle; halving it keeps the two ends apart at every sweep.
|
|
372
|
+
*/
|
|
373
|
+
function arcPath(centre, radius, from, sweep) {
|
|
374
|
+
const middle = from + sweep / 2;
|
|
375
|
+
const [x0, y0] = arcPoint(centre, radius, from);
|
|
376
|
+
const [xm, ym] = arcPoint(centre, radius, middle);
|
|
377
|
+
const [x1, y1] = arcPoint(centre, radius, from + sweep);
|
|
378
|
+
const r = radius.toFixed(2);
|
|
379
|
+
const clockwise = sweep >= 0 ? 1 : 0;
|
|
380
|
+
return (`M${x0.toFixed(2)} ${y0.toFixed(2)}` +
|
|
381
|
+
` A${r} ${r} 0 0 ${clockwise} ${xm.toFixed(2)} ${ym.toFixed(2)}` +
|
|
382
|
+
` A${r} ${r} 0 0 ${clockwise} ${x1.toFixed(2)} ${y1.toFixed(2)}`);
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Slices in the order the rows arrived, sized by their share of the total.
|
|
386
|
+
*
|
|
387
|
+
* The share is geometry, not a number the card shows: the renderer never writes
|
|
388
|
+
* a percentage or a total, because neither is a value the author bound. Rows
|
|
389
|
+
* with no number, or a total of zero, produce nothing so the fallback runs.
|
|
390
|
+
*/
|
|
391
|
+
export function pieSlices(model, donut, size = 100) {
|
|
392
|
+
const entry = model.series[0];
|
|
393
|
+
if (!entry)
|
|
394
|
+
return [];
|
|
395
|
+
const values = entry.values.map((value) => (isFiniteNumber(value) && value > 0 ? value : 0));
|
|
396
|
+
const total = values.reduce((sum, value) => sum + value, 0);
|
|
397
|
+
if (total <= 0)
|
|
398
|
+
return [];
|
|
399
|
+
const centre = size / 2;
|
|
400
|
+
const outer = centre * 0.84;
|
|
401
|
+
const inner = donut ? outer * DONUT_HOLE : 0;
|
|
402
|
+
let start = 0;
|
|
403
|
+
return model.labels.map((label, rowIndex) => {
|
|
404
|
+
const sweep = (values[rowIndex] / total) * 360;
|
|
405
|
+
const end = start + sweep;
|
|
406
|
+
const [xi1, yi1] = arcPoint(centre, inner, end);
|
|
407
|
+
const outerArc = arcPath(centre, outer, start, sweep);
|
|
408
|
+
// The inner edge runs back the other way, so it is drawn from the end angle.
|
|
409
|
+
const innerArc = arcPath(centre, inner, end, -sweep).replace(/^M[^A]+/, "");
|
|
410
|
+
const path = donut
|
|
411
|
+
? `${outerArc} L${xi1.toFixed(2)} ${yi1.toFixed(2)}${innerArc} Z`
|
|
412
|
+
: `${outerArc} L${centre} ${centre} Z`;
|
|
413
|
+
start = end;
|
|
414
|
+
return { label, value: values[rowIndex], index: rowIndex, path };
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Colour token for an ordered shape. There are four series colours, so a fifth
|
|
419
|
+
* or sixth slice reuses the first two at a lower opacity; the name and value
|
|
420
|
+
* next to every slice are what actually tells them apart.
|
|
421
|
+
*/
|
|
422
|
+
export function orderedTone(index) {
|
|
423
|
+
return { series: (index % MAX_SERIES) + 1, dim: index >= MAX_SERIES };
|
|
424
|
+
}
|
|
425
|
+
/** Outermost track radius, the widest step inwards, and the smallest track. */
|
|
426
|
+
const RADIAL_OUTER = 44;
|
|
427
|
+
const RADIAL_STEP = 12;
|
|
428
|
+
const RADIAL_INNERMOST = 14;
|
|
429
|
+
/**
|
|
430
|
+
* One track per row, outside in, scaled against the largest value in the set.
|
|
431
|
+
*
|
|
432
|
+
* The scale is the renderer's own reading of the bound numbers, the way the
|
|
433
|
+
* bar chart already sizes its bars; the card still shows only the values
|
|
434
|
+
* the author bound, never a share or a total.
|
|
435
|
+
*/
|
|
436
|
+
export function radialTracks(model, size = 100) {
|
|
437
|
+
const entry = model.series[0];
|
|
438
|
+
if (!entry)
|
|
439
|
+
return [];
|
|
440
|
+
const values = entry.values.map((value) => (isFiniteNumber(value) && value > 0 ? value : 0));
|
|
441
|
+
const top = Math.max(...values);
|
|
442
|
+
if (!(top > 0))
|
|
443
|
+
return [];
|
|
444
|
+
const centre = size / 2;
|
|
445
|
+
// Four tracks need a tighter step, or the innermost ring would be narrower
|
|
446
|
+
// than the groove it is drawn in.
|
|
447
|
+
const step = Math.min(RADIAL_STEP, (RADIAL_OUTER - RADIAL_INNERMOST) / Math.max(1, model.labels.length - 1));
|
|
448
|
+
return model.labels.map((label, rowIndex) => {
|
|
449
|
+
const radius = RADIAL_OUTER - rowIndex * step;
|
|
450
|
+
// A full turn stops a thousandth short of closing, so the ends stay apart.
|
|
451
|
+
const sweep = Math.min((values[rowIndex] / top) * 360, 359.999);
|
|
452
|
+
return {
|
|
453
|
+
label,
|
|
454
|
+
value: values[rowIndex],
|
|
455
|
+
index: rowIndex,
|
|
456
|
+
radius,
|
|
457
|
+
path: arcPath(centre, radius, 0, sweep),
|
|
458
|
+
};
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
/** Point radius range. The small end still clears a 24px target with its label. */
|
|
462
|
+
const POINT_MIN_RADIUS = 4;
|
|
463
|
+
const POINT_MAX_RADIUS = 11;
|
|
464
|
+
/** Points drawn with their names beside them; past this only the table names them. */
|
|
465
|
+
export const SCATTER_LABEL_LIMIT = 8;
|
|
466
|
+
function span(values) {
|
|
467
|
+
const finite = values.filter(isFiniteNumber);
|
|
468
|
+
const low = Math.min(...finite);
|
|
469
|
+
const high = Math.max(...finite);
|
|
470
|
+
// A flat axis would divide by zero; centring it keeps the point on the plot.
|
|
471
|
+
return high > low ? { low, high } : { low: low - 1, high: high + 1 };
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Points placed inside `plot` from the first two named fields, with an optional
|
|
475
|
+
* third read as the marker weight. Each axis is scaled against its own range,
|
|
476
|
+
* so the cloud fills the plot instead of hugging a corner. A row missing either
|
|
477
|
+
* coordinate is dropped rather than placed at a guessed zero.
|
|
478
|
+
*/
|
|
479
|
+
export function scatterPoints(model, plot) {
|
|
480
|
+
const [across, up, weight] = model.series;
|
|
481
|
+
if (!across || !up)
|
|
482
|
+
return [];
|
|
483
|
+
const horizontal = span(across.values);
|
|
484
|
+
const vertical = span(up.values);
|
|
485
|
+
const weights = weight ? span(weight.values) : null;
|
|
486
|
+
const points = [];
|
|
487
|
+
model.labels.forEach((label, rowIndex) => {
|
|
488
|
+
const x = across.values[rowIndex];
|
|
489
|
+
const y = up.values[rowIndex];
|
|
490
|
+
if (!isFiniteNumber(x) || !isFiniteNumber(y))
|
|
491
|
+
return;
|
|
492
|
+
const size = weight ? weight.values[rowIndex] : null;
|
|
493
|
+
const ratio = weights && isFiniteNumber(size) ? (size - weights.low) / (weights.high - weights.low) : null;
|
|
494
|
+
points.push({
|
|
495
|
+
label,
|
|
496
|
+
index: rowIndex,
|
|
497
|
+
x: plot.x + ((x - horizontal.low) / (horizontal.high - horizontal.low)) * plot.width,
|
|
498
|
+
y: plot.y + plot.height - ((y - vertical.low) / (vertical.high - vertical.low)) * plot.height,
|
|
499
|
+
radius: ratio === null
|
|
500
|
+
? POINT_MIN_RADIUS + 1
|
|
501
|
+
: POINT_MIN_RADIUS + ratio * (POINT_MAX_RADIUS - POINT_MIN_RADIUS),
|
|
502
|
+
values: model.series.map((entry) => entry.values[rowIndex]).filter(isFiniteNumber),
|
|
503
|
+
});
|
|
504
|
+
});
|
|
505
|
+
return points;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* The plot box point names are placed in: the narrowest card the Widget
|
|
509
|
+
* supports. The plot's height is fixed, and a wider card only spreads the
|
|
510
|
+
* points further apart, so a placement that works here works everywhere.
|
|
511
|
+
*/
|
|
512
|
+
const SCATTER_LABEL_BOX = { width: 270, height: 176 };
|
|
513
|
+
/** One line of the label type role, and the clearance past the marker's edge. */
|
|
514
|
+
const SCATTER_LABEL_LINE = 14;
|
|
515
|
+
const SCATTER_LABEL_CLEARANCE = 4;
|
|
516
|
+
function overlaps(one, other) {
|
|
517
|
+
return one.left < other.right && other.left < one.right && one.top < other.bottom && other.top < one.bottom;
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Where each point's name goes, or nothing when it cannot go anywhere.
|
|
521
|
+
*
|
|
522
|
+
* A name is never cut and never hangs outside the plot: it starts above its
|
|
523
|
+
* point and flips below, then to one side, until it fits the box without
|
|
524
|
+
* touching a name already placed. A point that still has nowhere to put its
|
|
525
|
+
* name keeps it in the hidden table only — dropping the label is honest, a
|
|
526
|
+
* clipped or overlapping one is not.
|
|
527
|
+
*/
|
|
528
|
+
export function scatterLabels(points, box = SCATTER_LABEL_BOX) {
|
|
529
|
+
const placed = [];
|
|
530
|
+
const names = [];
|
|
531
|
+
for (const point of points) {
|
|
532
|
+
const width = labelWidth(point.label);
|
|
533
|
+
const x = (point.x / 100) * box.width;
|
|
534
|
+
const y = (point.y / 100) * box.height;
|
|
535
|
+
const gap = point.radius + SCATTER_LABEL_CLEARANCE;
|
|
536
|
+
let chosen = null;
|
|
537
|
+
for (const below of [false, true]) {
|
|
538
|
+
for (const align of ["center", "start", "end"]) {
|
|
539
|
+
const left = align === "center" ? x - width / 2 : align === "start" ? x : x - width;
|
|
540
|
+
const top = below ? y + gap : y - gap - SCATTER_LABEL_LINE;
|
|
541
|
+
const rect = { left, top, right: left + width, bottom: top + SCATTER_LABEL_LINE };
|
|
542
|
+
if (rect.left < 0 || rect.right > box.width || rect.top < 0 || rect.bottom > box.height)
|
|
543
|
+
continue;
|
|
544
|
+
if (placed.some((other) => overlaps(rect, other)))
|
|
545
|
+
continue;
|
|
546
|
+
chosen = { index: point.index, label: point.label, below, align, gap, rect };
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
if (chosen)
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
if (!chosen)
|
|
553
|
+
continue;
|
|
554
|
+
placed.push(chosen.rect);
|
|
555
|
+
const { rect: _rect, ...name } = chosen;
|
|
556
|
+
names.push(name);
|
|
557
|
+
}
|
|
558
|
+
return names;
|
|
559
|
+
}
|
|
560
|
+
/** Lines a wrapped label may take before the chart is turned sideways instead. */
|
|
561
|
+
export const MAX_LABEL_LINES = 2;
|
|
562
|
+
/**
|
|
563
|
+
* Width of one label character. The labels use the `--meta` role: 11-12px
|
|
564
|
+
* uppercase with per-pack tracking, which is 8px per Latin character in the
|
|
565
|
+
* widest of the five packs. A full-width character takes its own em.
|
|
566
|
+
*/
|
|
567
|
+
const LABEL_CHAR_WIDTH = 8;
|
|
568
|
+
const LABEL_WIDE_CHAR_WIDTH = 12;
|
|
569
|
+
/** Breathing room kept between two neighbouring labels. */
|
|
570
|
+
const LABEL_GAP = 6;
|
|
571
|
+
/** Inline size of the chart inside the narrowest card the Widget supports. */
|
|
572
|
+
const NARROW_CHART_WIDTH = 310;
|
|
573
|
+
/** Room the value axis and its gap take out of the chart's inline size. */
|
|
574
|
+
const AXIS_GUTTER_WIDTH = 40;
|
|
575
|
+
/**
|
|
576
|
+
* Categories an upright bar chart can hold before it turns sideways whatever its
|
|
577
|
+
* labels say.
|
|
578
|
+
*
|
|
579
|
+
* The label rule alone is not enough once a report binds sixteen channels: short
|
|
580
|
+
* names such as "W1".."W16" fit their own column, but that column is 17px wide and
|
|
581
|
+
* four grouped bars inside it are 4px each. Eight is the width this chart was
|
|
582
|
+
* measured at, so nothing drawn today changes; past it every category gets a row of
|
|
583
|
+
* its own on the sideways skeleton, where the number of rows costs no width at all.
|
|
584
|
+
*/
|
|
585
|
+
export const MAX_UPRIGHT_CATEGORIES = 8;
|
|
586
|
+
/** CJK, kana and other full-width characters, which take about one em each. */
|
|
587
|
+
const WIDE_CHARACTER = /[ᄀ-ᅟ⺀-ꥠ-가-힣豈-︰--⦆¢-₩]/;
|
|
588
|
+
function labelWidth(label) {
|
|
589
|
+
let width = LABEL_GAP;
|
|
590
|
+
for (const character of label) {
|
|
591
|
+
width += WIDE_CHARACTER.test(character) ? LABEL_WIDE_CHAR_WIDTH : LABEL_CHAR_WIDTH;
|
|
592
|
+
}
|
|
593
|
+
return width;
|
|
594
|
+
}
|
|
595
|
+
function widestLabel(labels) {
|
|
596
|
+
return labels.reduce((widest, label) => Math.max(widest, labelWidth(label)), 0);
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* The pieces of a label that may not be broken.
|
|
600
|
+
*
|
|
601
|
+
* A run of Latin letters, digits or punctuation stays whole. A full-width
|
|
602
|
+
* character is a piece of its own, because CJK writes no spaces: without a break
|
|
603
|
+
* between two characters a label such as "客户端访问" could never take a second
|
|
604
|
+
* line and would turn every chart sideways.
|
|
605
|
+
*/
|
|
606
|
+
function labelSegments(label) {
|
|
607
|
+
const segments = [];
|
|
608
|
+
let word = "";
|
|
609
|
+
for (const character of label) {
|
|
610
|
+
if (/\s/.test(character)) {
|
|
611
|
+
if (word)
|
|
612
|
+
segments.push(word);
|
|
613
|
+
word = "";
|
|
614
|
+
}
|
|
615
|
+
else if (WIDE_CHARACTER.test(character)) {
|
|
616
|
+
if (word)
|
|
617
|
+
segments.push(word);
|
|
618
|
+
word = "";
|
|
619
|
+
segments.push(character);
|
|
620
|
+
}
|
|
621
|
+
else {
|
|
622
|
+
word += character;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
if (word)
|
|
626
|
+
segments.push(word);
|
|
627
|
+
return segments;
|
|
628
|
+
}
|
|
629
|
+
/** A space is written back only between two Latin runs; CJK runs together. */
|
|
630
|
+
function joinsWithSpace(line, segment) {
|
|
631
|
+
return !WIDE_CHARACTER.test(line.slice(-1)) && !WIDE_CHARACTER.test(segment.slice(0, 1));
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* The lines a label takes in a column this wide, greedily filled the way a
|
|
635
|
+
* renderer wraps it, or `null` when one of its unbreakable runs is wider than
|
|
636
|
+
* the column and the label can only be drawn on a sideways chart.
|
|
637
|
+
*/
|
|
638
|
+
export function labelLines(label, column) {
|
|
639
|
+
const lines = [];
|
|
640
|
+
let line = "";
|
|
641
|
+
for (const segment of labelSegments(label)) {
|
|
642
|
+
if (labelWidth(segment) > column)
|
|
643
|
+
return null;
|
|
644
|
+
if (line === "") {
|
|
645
|
+
line = segment;
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
const joined = `${line}${joinsWithSpace(line, segment) ? " " : ""}${segment}`;
|
|
649
|
+
if (labelWidth(joined) > column) {
|
|
650
|
+
lines.push(line);
|
|
651
|
+
line = segment;
|
|
652
|
+
}
|
|
653
|
+
else {
|
|
654
|
+
line = joined;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
lines.push(line);
|
|
658
|
+
return lines;
|
|
659
|
+
}
|
|
660
|
+
/** The layout the labels need at the narrowest width the chart has to survive. */
|
|
661
|
+
export function rowLabelLayout(labels) {
|
|
662
|
+
if (labels.length === 0)
|
|
663
|
+
return "flat";
|
|
664
|
+
if (labels.length > MAX_UPRIGHT_CATEGORIES)
|
|
665
|
+
return "horizontal";
|
|
666
|
+
const column = (NARROW_CHART_WIDTH - AXIS_GUTTER_WIDTH) / labels.length;
|
|
667
|
+
// Step 1 measures the whole label, step 2 the longest run inside it.
|
|
668
|
+
if (widestLabel(labels) <= column)
|
|
669
|
+
return "flat";
|
|
670
|
+
const wraps = labels.every((label) => {
|
|
671
|
+
const lines = labelLines(label, column);
|
|
672
|
+
return lines !== null && lines.length <= MAX_LABEL_LINES;
|
|
673
|
+
});
|
|
674
|
+
return wraps ? "wrapped" : "horizontal";
|
|
675
|
+
}
|
|
676
|
+
/** The radar box is as wide as the chart inside the narrowest card. */
|
|
677
|
+
export const RADAR_BOX_WIDTH = NARROW_CHART_WIDTH;
|
|
678
|
+
/**
|
|
679
|
+
* The polygon's natural radius, the smallest it may shrink to, and the step it
|
|
680
|
+
* shrinks by. The natural radius is the size S16 already drew at (38 of a
|
|
681
|
+
* 100-unit box scaled to 180px), so a radar whose names fit is not made smaller
|
|
682
|
+
* by this change.
|
|
683
|
+
*/
|
|
684
|
+
const RADAR_RADIUS = 68;
|
|
685
|
+
const RADAR_MIN_RADIUS = 44;
|
|
686
|
+
const RADAR_RADIUS_STEP = 4;
|
|
687
|
+
/** Clearance from a vertex to the near edge of its name, and one line of the name. */
|
|
688
|
+
const RADAR_LABEL_GAP = 8;
|
|
689
|
+
export const RADAR_LABEL_LINE = 14;
|
|
690
|
+
/** Breathing room kept inside the box's own edges. */
|
|
691
|
+
const RADAR_EDGE_PAD = 2;
|
|
692
|
+
/**
|
|
693
|
+
* How far off the centre line a vertex has to sit before it counts as being on
|
|
694
|
+
* that side. A vertex on the vertical gets a centred name, one on the horizontal
|
|
695
|
+
* gets a name centred on its own line.
|
|
696
|
+
*/
|
|
697
|
+
const RADAR_SIDE = 0.15;
|
|
698
|
+
/**
|
|
699
|
+
* Height of the box at a given radius: the polygon plus the tallest name it can
|
|
700
|
+
* carry, on both sides. The room is always reserved for the tallest name a rule
|
|
701
|
+
* allows, so the vertical always fits and the fit test below only ever has to
|
|
702
|
+
* measure width — and a polygon that shrank to make room takes a shorter box
|
|
703
|
+
* with it instead of leaving a band of nothing.
|
|
704
|
+
*/
|
|
705
|
+
export function radarBoxHeight(radius) {
|
|
706
|
+
return 2 * (radius + RADAR_LABEL_GAP + MAX_LABEL_LINES * RADAR_LABEL_LINE);
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Guide rings, as fractions of the declared 0-1 range. Two is what the reading
|
|
710
|
+
* needs — "past halfway" and "at the top" — and the spokes already say how many
|
|
711
|
+
* axes there are; a quarter ring is 22px across at the narrowest card and only
|
|
712
|
+
* adds clutter at the centre, where every polygon is at its busiest.
|
|
713
|
+
*/
|
|
714
|
+
export const RADAR_RINGS = [0.5, 1];
|
|
715
|
+
/** Angle of one axis in radians, starting at twelve o'clock and running clockwise. */
|
|
716
|
+
export function radarAngle(index, count) {
|
|
717
|
+
return -Math.PI / 2 + (index * Math.PI * 2) / Math.max(count, 3);
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Vertex coordinates of one radar face. Values outside 0-1 are clamped, because
|
|
721
|
+
* an axis only reaches the outer ring; a row with no number sits at the centre
|
|
722
|
+
* rather than breaking the closed shape.
|
|
723
|
+
*/
|
|
724
|
+
export function radarPoints(values, radius, centre) {
|
|
725
|
+
return values.map((raw, index) => {
|
|
726
|
+
const angle = radarAngle(index, values.length);
|
|
727
|
+
const value = Math.max(0, Math.min(isFiniteNumber(raw) ? raw : 0, 1));
|
|
728
|
+
return { x: centre.x + Math.cos(angle) * radius * value, y: centre.y + Math.sin(angle) * radius * value };
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* The grid behind the faces: one hairline polygon per ring and one spoke from
|
|
733
|
+
* the centre to each vertex.
|
|
734
|
+
*
|
|
735
|
+
* The rings are polygons, not circles, because the data is a polygon: a circle
|
|
736
|
+
* only touches the axes it happens to cross, so it cannot tell a reader where
|
|
737
|
+
* "half" is on the axis they are looking at. The spokes are what tie a vertex to
|
|
738
|
+
* the name printed outside it, and they make the number of axes countable even
|
|
739
|
+
* where a face covers the outer ring.
|
|
740
|
+
*/
|
|
741
|
+
export function radarGrid(count, radius, centre) {
|
|
742
|
+
const axes = Math.max(count, 3);
|
|
743
|
+
const full = new Array(axes).fill(1);
|
|
744
|
+
return {
|
|
745
|
+
rings: RADAR_RINGS.map((fraction) => radarPoints(full, radius * fraction, centre)),
|
|
746
|
+
spokes: radarPoints(full, radius, centre),
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
/** Width a name may take at `x`, given how it lines up there. */
|
|
750
|
+
function radarColumn(x, anchor, width) {
|
|
751
|
+
if (anchor === "start")
|
|
752
|
+
return width - x - RADAR_EDGE_PAD;
|
|
753
|
+
if (anchor === "end")
|
|
754
|
+
return x - RADAR_EDGE_PAD;
|
|
755
|
+
return 2 * Math.min(x - RADAR_EDGE_PAD, width - x - RADAR_EDGE_PAD);
|
|
756
|
+
}
|
|
757
|
+
/** Every name placed at this radius, or `null` as soon as one of them cannot fit. */
|
|
758
|
+
function radarNames(labels, radius, centre, width) {
|
|
759
|
+
const placed = [];
|
|
760
|
+
for (let index = 0; index < labels.length; index += 1) {
|
|
761
|
+
const angle = radarAngle(index, labels.length);
|
|
762
|
+
const across = Math.cos(angle);
|
|
763
|
+
const down = Math.sin(angle);
|
|
764
|
+
// The anchor sits straight out from the vertex, so the name clears the
|
|
765
|
+
// polygon on the diagonals as well as on the axes.
|
|
766
|
+
const x = centre.x + (radius + RADAR_LABEL_GAP) * across;
|
|
767
|
+
const y = centre.y + (radius + RADAR_LABEL_GAP) * down;
|
|
768
|
+
const anchor = across > RADAR_SIDE ? "start" : across < -RADAR_SIDE ? "end" : "middle";
|
|
769
|
+
const rise = down < -RADAR_SIDE ? "above" : down > RADAR_SIDE ? "below" : "middle";
|
|
770
|
+
const lines = labelLines(labels[index], radarColumn(x, anchor, width));
|
|
771
|
+
if (lines === null || lines.length > MAX_LABEL_LINES)
|
|
772
|
+
return null;
|
|
773
|
+
const block = lines.length * RADAR_LABEL_LINE;
|
|
774
|
+
const top = rise === "above" ? y - block : rise === "below" ? y : y - block / 2;
|
|
775
|
+
placed.push({ index, lines, anchor, rise, x, top });
|
|
776
|
+
}
|
|
777
|
+
return placed;
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* Where every axis name goes, and how large the polygon may be.
|
|
781
|
+
*
|
|
782
|
+
* Each name sits outside its own vertex, lined up by the side of the chart it is
|
|
783
|
+
* on: a vertex on the right starts its name there and runs outwards, one on the
|
|
784
|
+
* left ends it there, and one straight above or below the centre gets a centred
|
|
785
|
+
* name. Nothing is rotated and nothing is cut. A name that does not fit on one
|
|
786
|
+
* line wraps onto a second with the same `labelLines` rule the bar chart uses,
|
|
787
|
+
* and if two lines still do not fit, the polygon shrinks — a smaller polygon
|
|
788
|
+
* moves its side vertices inwards, which is exactly the room a side name needs.
|
|
789
|
+
* Only when the smallest polygon still cannot hold every name do all the names
|
|
790
|
+
* drop to one row under the chart, the way S16 drew them before. It is all or
|
|
791
|
+
* nothing: half the names beside their vertices and half in a row below would be
|
|
792
|
+
* read as two different things.
|
|
793
|
+
*/
|
|
794
|
+
export function radarLayout(labels, width = RADAR_BOX_WIDTH) {
|
|
795
|
+
for (let radius = RADAR_RADIUS; radius >= RADAR_MIN_RADIUS; radius -= RADAR_RADIUS_STEP) {
|
|
796
|
+
const height = radarBoxHeight(radius);
|
|
797
|
+
const centre = { x: width / 2, y: height / 2 };
|
|
798
|
+
const placed = radarNames(labels, radius, centre, width);
|
|
799
|
+
if (placed === null)
|
|
800
|
+
continue;
|
|
801
|
+
// The box reserved room for the tallest name the rule allows; now that every
|
|
802
|
+
// name is placed, it is trimmed to the one that actually reaches furthest, so
|
|
803
|
+
// a chart whose names all fit on one line does not carry a band of nothing.
|
|
804
|
+
const first = Math.min(...placed.map((label) => label.top));
|
|
805
|
+
const last = Math.max(...placed.map((label) => label.top + label.lines.length * RADAR_LABEL_LINE));
|
|
806
|
+
return {
|
|
807
|
+
width,
|
|
808
|
+
height: last - first,
|
|
809
|
+
centre: { x: centre.x, y: centre.y - first },
|
|
810
|
+
radius,
|
|
811
|
+
labels: placed.map((label) => ({ ...label, top: label.top - first })),
|
|
812
|
+
row: false,
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
// No name is drawn beside the polygon, so the box keeps only the drawing.
|
|
816
|
+
const height = 2 * (RADAR_RADIUS + RADAR_LABEL_GAP);
|
|
817
|
+
return { width, height, centre: { x: width / 2, y: height / 2 }, radius: RADAR_RADIUS, labels: [], row: true };
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* The text alternative. It carries every row and every series, and the unit
|
|
821
|
+
* rides in the column header instead of being glued onto each number.
|
|
822
|
+
*/
|
|
823
|
+
export function chartTable(model, unit) {
|
|
824
|
+
const suffix = unit && unit.length > 0 ? ` (${unit})` : "";
|
|
825
|
+
return {
|
|
826
|
+
columns: ["", ...model.series.map((entry) => `${entry.label}${suffix}`)],
|
|
827
|
+
rows: model.labels.map((label, rowIndex) => ({
|
|
828
|
+
label,
|
|
829
|
+
cells: model.series.map((entry) => {
|
|
830
|
+
const value = entry.values[rowIndex];
|
|
831
|
+
return value === null || value === undefined ? "—" : formatChartNumber(value);
|
|
832
|
+
}),
|
|
833
|
+
})),
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* Width one value column under a radar needs, in px.
|
|
838
|
+
*
|
|
839
|
+
* The column has to hold a 0-1 score and, when there are two series, the series
|
|
840
|
+
* name over it. A fixed column wrapped "Northwind" into "North"/"wind" on both
|
|
841
|
+
* native renderers, and a name broken in the middle is exactly what this chart's
|
|
842
|
+
* label rules forbid. Past the cap the head wraps instead of eating the room the
|
|
843
|
+
* axis names need on the other side of the row.
|
|
844
|
+
*/
|
|
845
|
+
const RADAR_VALUE_COLUMN_MIN = 38;
|
|
846
|
+
const RADAR_VALUE_COLUMN_MAX = 96;
|
|
847
|
+
export function radarValueColumn(model) {
|
|
848
|
+
const table = chartTable(model, null);
|
|
849
|
+
const texts = [
|
|
850
|
+
...(model.series.length > 1 ? model.series.map((entry) => entry.label) : []),
|
|
851
|
+
...table.rows.flatMap((row) => row.cells),
|
|
852
|
+
];
|
|
853
|
+
return Math.min(RADAR_VALUE_COLUMN_MAX, Math.max(RADAR_VALUE_COLUMN_MIN, ...texts.map(labelWidth)));
|
|
854
|
+
}
|
|
855
|
+
/* --------------------------------------------------------- scatter chart */
|
|
856
|
+
/*
|
|
857
|
+
* The scatter chart (S47) reads its rows the way the line chart does — one
|
|
858
|
+
* `labelField` and up to four `seriesFields` — except that the label field holds
|
|
859
|
+
* a number: each row is one x value, and each series a y value drawn as a point
|
|
860
|
+
* at it. A row with no number for a series simply has no point for it, so two
|
|
861
|
+
* groups measured at different x values share one list of rows.
|
|
862
|
+
*/
|
|
863
|
+
/** Marker shape per series index, so a series is identifiable without colour. */
|
|
864
|
+
export const SCATTER_MARKS = ["circle", "square", "triangle", "diamond"];
|
|
865
|
+
/**
|
|
866
|
+
* Marker outlines around (0, 0), in px. Each covers about the area of the 8px
|
|
867
|
+
* circle, so no series looks heavier than another; the box they share is 12px,
|
|
868
|
+
* which leaves room for the 1px surface ring.
|
|
869
|
+
*/
|
|
870
|
+
export const SCATTER_MARK_PATHS = {
|
|
871
|
+
circle: "M -4 0 A 4 4 0 1 0 4 0 A 4 4 0 1 0 -4 0 Z",
|
|
872
|
+
square: "M -3.5 -3.5 L 3.5 -3.5 L 3.5 3.5 L -3.5 3.5 Z",
|
|
873
|
+
triangle: "M 0 -5 L 4.8 3.4 L -4.8 3.4 Z",
|
|
874
|
+
diamond: "M 0 -4.8 L 4.8 0 L 0 4.8 L -4.8 0 Z",
|
|
875
|
+
};
|
|
876
|
+
/** Half the side of the box every marker is drawn in. */
|
|
877
|
+
export const SCATTER_MARK_BOX = 6;
|
|
878
|
+
export function scatterMark(seriesIndex) {
|
|
879
|
+
return SCATTER_MARKS[seriesIndex % SCATTER_MARKS.length];
|
|
880
|
+
}
|
|
881
|
+
/** Ticks per axis the plot aims for, before rounding to a readable step. */
|
|
882
|
+
const SCATTER_TARGET_TICKS = 4;
|
|
883
|
+
/** How far a flat axis reaches either side of its one value: a tenth of it, and at least 1. */
|
|
884
|
+
const SCATTER_FLAT_REACH = 0.1;
|
|
885
|
+
/**
|
|
886
|
+
* The largest magnitude a scatter value may have. Below it every span, step, tick and bound the
|
|
887
|
+
* axes are built from is a finite number; -1e308 and 1e308 are each finite, but the span between
|
|
888
|
+
* them is not, and a plot built on it would put both points in the middle. The LP-MDX compiler
|
|
889
|
+
* refuses such rows with `invalid_chart_data`; a renderer given them anyway draws the fallback.
|
|
890
|
+
*/
|
|
891
|
+
export const SCATTER_MAX_MAGNITUDE = 1e300;
|
|
892
|
+
/** The most steps an axis is built from; four are aimed for, so more means a broken step. */
|
|
893
|
+
const SCATTER_MAX_STEPS = 10;
|
|
894
|
+
/** Past this many steps from zero a tick is already a whole number of them, so is kept as is. */
|
|
895
|
+
const SCATTER_EXACT_STEPS = 1e15;
|
|
896
|
+
/**
|
|
897
|
+
* 10^`exponent` as the double nearest it, read from its decimal text rather than from `pow`: the
|
|
898
|
+
* JVM's `Math.pow(10, -5)` is 9.999999999999999e-6, one unit off, and it moved Compose's ticks.
|
|
899
|
+
* Compose and SwiftUI compute every scatter step with these same operations in the same order.
|
|
900
|
+
*/
|
|
901
|
+
function decimalPower(exponent) {
|
|
902
|
+
return Number(`1e${exponent}`);
|
|
903
|
+
}
|
|
904
|
+
/** The largest e with `decimalPower(e) <= value`, for a positive finite value; `log10` is only a guess. */
|
|
905
|
+
function decimalExponent(value) {
|
|
906
|
+
const guess = Math.floor(Math.log10(value));
|
|
907
|
+
if (decimalPower(guess) > value)
|
|
908
|
+
return guess - 1;
|
|
909
|
+
return decimalPower(guess + 1) <= value ? guess + 1 : guess;
|
|
910
|
+
}
|
|
911
|
+
/** A readable step (1, 2 or 5 times a power of ten) at least `rough`. */
|
|
912
|
+
function scatterStep(rough) {
|
|
913
|
+
const magnitude = decimalPower(decimalExponent(rough));
|
|
914
|
+
const fraction = rough / magnitude;
|
|
915
|
+
const factor = fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 5 ? 5 : 10;
|
|
916
|
+
return factor * magnitude;
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* `value` rounded (half up) one decimal place finer than `step`, read back from its decimal
|
|
920
|
+
* digits so it carries no float noise at any magnitude: 3 x 0.2 reads 0.6, and 3 x 1e-25 reads
|
|
921
|
+
* 3e-25. A value too far from zero for that, or a place that cannot be represented, is kept.
|
|
922
|
+
*/
|
|
923
|
+
function roundTick(value, step) {
|
|
924
|
+
if (value === 0 || !Number.isFinite(value))
|
|
925
|
+
return value;
|
|
926
|
+
const place = decimalExponent(step) - 1;
|
|
927
|
+
const scale = decimalPower(Math.abs(place));
|
|
928
|
+
if (!Number.isFinite(scale) || scale === 0)
|
|
929
|
+
return value;
|
|
930
|
+
const scaled = place >= 0 ? value / scale : value * scale;
|
|
931
|
+
if (!Number.isFinite(scaled) || Math.abs(scaled) >= SCATTER_EXACT_STEPS)
|
|
932
|
+
return value;
|
|
933
|
+
const rounded = Number(`${Math.floor(scaled + 0.5)}e${place}`);
|
|
934
|
+
return Number.isFinite(rounded) ? rounded : value;
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* Rounded bounds and ticks for one axis, hugging the data like the line chart's value axis.
|
|
938
|
+
*
|
|
939
|
+
* The step comes from the span, never from the size of the values: thirteen-digit millisecond
|
|
940
|
+
* timestamps 12 ms apart get ticks 5 ms apart. The ticks are counted before they are built and
|
|
941
|
+
* there are never more than `SCATTER_MAX_STEPS + 1`, so no loop runs on a float counter. When
|
|
942
|
+
* the step cannot be told apart from the values (it underflows, adding it changes nothing, or
|
|
943
|
+
* the ticks it gives are not strictly increasing and around the data), the axis is just the two
|
|
944
|
+
* true endpoints. The bounds always hold every tick and every value. A flat axis keeps its one
|
|
945
|
+
* value as its only tick, in the middle of a reach of a tenth of it either side (at least 1).
|
|
946
|
+
*/
|
|
947
|
+
export function scatterAxis(values) {
|
|
948
|
+
const low = Math.min(...values);
|
|
949
|
+
const high = Math.max(...values);
|
|
950
|
+
if (low === high) {
|
|
951
|
+
const reach = Math.max(Math.abs(low) * SCATTER_FLAT_REACH, 1);
|
|
952
|
+
return { min: low - reach, max: low + reach, ticks: [low] };
|
|
953
|
+
}
|
|
954
|
+
const endpoints = { min: low, max: high, ticks: [low, high] };
|
|
955
|
+
const rough = (high - low) / SCATTER_TARGET_TICKS;
|
|
956
|
+
if (!(rough > 0) || !Number.isFinite(rough))
|
|
957
|
+
return endpoints;
|
|
958
|
+
const step = scatterStep(rough);
|
|
959
|
+
if (!(step > 0) || !Number.isFinite(step) || low + step === low || high - step === high)
|
|
960
|
+
return endpoints;
|
|
961
|
+
const start = Math.floor(low / step) * step;
|
|
962
|
+
const count = Math.ceil((high - start) / step);
|
|
963
|
+
if (!Number.isFinite(start) || !Number.isFinite(count) || count < 1 || count > SCATTER_MAX_STEPS)
|
|
964
|
+
return endpoints;
|
|
965
|
+
const ticks = Array.from({ length: count + 1 }, (_value, index) => roundTick(start + index * step, step));
|
|
966
|
+
const ordered = ticks.every((tick, index) => Number.isFinite(tick) && (index === 0 || tick > ticks[index - 1]));
|
|
967
|
+
if (!ordered || ticks[0] > low || ticks[count] < high)
|
|
968
|
+
return endpoints;
|
|
969
|
+
return { min: ticks[0], max: ticks[count], ticks };
|
|
970
|
+
}
|
|
971
|
+
export function scatterAxisPercent(axis, value) {
|
|
972
|
+
const span = axis.max - axis.min;
|
|
973
|
+
return span > 0 ? ((value - axis.min) / span) * 100 : 50;
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* The drawable scatter, or `null` when there is no point to draw. As for the
|
|
977
|
+
* other charts, `seriesFields` is required and `labelField` defaults to
|
|
978
|
+
* `"label"`; a row whose x is not a number has no point in any series.
|
|
979
|
+
*/
|
|
980
|
+
export function readScatterChart(rows, options = {}) {
|
|
981
|
+
const usable = rows.filter((row) => typeof row === "object" && row !== null && !Array.isArray(row));
|
|
982
|
+
const base = readSeriesChart(usable, options);
|
|
983
|
+
if (!base)
|
|
984
|
+
return null;
|
|
985
|
+
const labelField = options.labelField && options.labelField.length > 0 ? options.labelField : "label";
|
|
986
|
+
const xs = usable.map((row) => (isFiniteNumber(row[labelField]) ? row[labelField] : null));
|
|
987
|
+
const pairs = base.series.flatMap((entry) => entry.values.flatMap((y, rowIndex) => {
|
|
988
|
+
const x = xs[rowIndex];
|
|
989
|
+
return x === null || y === null ? [] : [{ seriesIndex: entry.index, rowIndex, x, y }];
|
|
990
|
+
}));
|
|
991
|
+
if (pairs.length === 0)
|
|
992
|
+
return null;
|
|
993
|
+
if (pairs.some((pair) => Math.abs(pair.x) >= SCATTER_MAX_MAGNITUDE || Math.abs(pair.y) >= SCATTER_MAX_MAGNITUDE))
|
|
994
|
+
return null;
|
|
995
|
+
const x = scatterAxis(pairs.map((pair) => pair.x));
|
|
996
|
+
const y = scatterAxis(pairs.map((pair) => pair.y));
|
|
997
|
+
return {
|
|
998
|
+
xs,
|
|
999
|
+
series: base.series,
|
|
1000
|
+
x,
|
|
1001
|
+
y,
|
|
1002
|
+
points: pairs.map((pair) => ({ ...pair, left: scatterAxisPercent(x, pair.x), bottom: scatterAxisPercent(y, pair.y) })),
|
|
1003
|
+
};
|
|
1004
|
+
}
|
|
1005
|
+
/** Inline size the x labels share in the narrowest card, after the y axis gutter. */
|
|
1006
|
+
const SCATTER_X_LABEL_WIDTH = NARROW_CHART_WIDTH - AXIS_GUTTER_WIDTH;
|
|
1007
|
+
function tickWidth(labels) {
|
|
1008
|
+
return labels.reduce((total, label) => total + labelWidth(label), 0);
|
|
1009
|
+
}
|
|
1010
|
+
/**
|
|
1011
|
+
* The x ticks that are labelled, and how. Every tick while the labels fit side by side in the
|
|
1012
|
+
* narrowest card; past that, the ends and the middle tick, the way the horizontal bar scale thins
|
|
1013
|
+
* its own. When even those do not fit — large values a short span apart, such as timestamps —
|
|
1014
|
+
* the first keeps its full value and the others are written as offsets from it ("+5"), so
|
|
1015
|
+
* neighbouring labels never collide or read the same. The first label starts at its tick and
|
|
1016
|
+
* the last ends at its tick, so neither hangs past the plot; a lone tick is centred.
|
|
1017
|
+
*/
|
|
1018
|
+
export function scatterXTicks(axis) {
|
|
1019
|
+
const { ticks } = axis;
|
|
1020
|
+
const align = (index, count) => count === 1 ? "center" : index === 0 ? "start" : index === count - 1 ? "end" : "center";
|
|
1021
|
+
const labelled = (values, labels) => values.map((value, index) => ({ value, label: labels[index], align: align(index, values.length) }));
|
|
1022
|
+
const full = ticks.map(formatChartNumber);
|
|
1023
|
+
if (widestLabel(full) * ticks.length <= SCATTER_X_LABEL_WIDTH)
|
|
1024
|
+
return labelled(ticks, full);
|
|
1025
|
+
const thinned = ticks.length % 2 === 1
|
|
1026
|
+
? [ticks[0], ticks[(ticks.length - 1) / 2], ticks[ticks.length - 1]]
|
|
1027
|
+
: [ticks[0], ticks[ticks.length - 1]];
|
|
1028
|
+
const thinnedFull = thinned.map(formatChartNumber);
|
|
1029
|
+
if (widestLabel(thinnedFull) * thinned.length <= SCATTER_X_LABEL_WIDTH)
|
|
1030
|
+
return labelled(thinned, thinnedFull);
|
|
1031
|
+
const offsets = thinned.map((value, index) => index === 0 ? thinnedFull[0] : `+${formatChartNumber(roundTick(value - thinned[0], ticks[1] - ticks[0]))}`);
|
|
1032
|
+
if (tickWidth(offsets) <= SCATTER_X_LABEL_WIDTH)
|
|
1033
|
+
return labelled(thinned, offsets);
|
|
1034
|
+
const ends = [ticks[0], ticks[ticks.length - 1]];
|
|
1035
|
+
return labelled(ends, ends.map(formatChartNumber));
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* The text alternative: one row per record, its x value first and then every
|
|
1039
|
+
* series, so each point is read with both of its numbers.
|
|
1040
|
+
*/
|
|
1041
|
+
export function scatterTable(model, xName, unit) {
|
|
1042
|
+
const suffix = unit && unit.length > 0 ? ` (${unit})` : "";
|
|
1043
|
+
return {
|
|
1044
|
+
columns: [xName, ...model.series.map((entry) => `${entry.label}${suffix}`)],
|
|
1045
|
+
rows: model.xs.map((x, rowIndex) => ({
|
|
1046
|
+
label: x === null ? "—" : formatChartNumber(x),
|
|
1047
|
+
cells: model.series.map((entry) => {
|
|
1048
|
+
const value = entry.values[rowIndex];
|
|
1049
|
+
return value === null || value === undefined ? "—" : formatChartNumber(value);
|
|
1050
|
+
}),
|
|
1051
|
+
})),
|
|
1052
|
+
};
|
|
1053
|
+
}
|