@ac-kit/model-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 +194 -0
- package/README.md +106 -0
- package/dist/geographic/index.d.ts +2 -0
- package/dist/geographic/index.d.ts.map +1 -0
- package/dist/geographic/index.js +2 -0
- package/dist/geographic/index.js.map +1 -0
- package/dist/geographic/project-geographic-layer.d.ts +54 -0
- package/dist/geographic/project-geographic-layer.d.ts.map +1 -0
- package/dist/geographic/project-geographic-layer.js +214 -0
- package/dist/geographic/project-geographic-layer.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/dist/layout/_fit.d.ts +12 -0
- package/dist/layout/_fit.d.ts.map +1 -0
- package/dist/layout/_fit.js +52 -0
- package/dist/layout/_fit.js.map +1 -0
- package/dist/layout/index.d.ts +4 -0
- package/dist/layout/index.d.ts.map +1 -0
- package/dist/layout/index.js +4 -0
- package/dist/layout/index.js.map +1 -0
- package/dist/layout/layout-hierarchy.d.ts +35 -0
- package/dist/layout/layout-hierarchy.d.ts.map +1 -0
- package/dist/layout/layout-hierarchy.js +98 -0
- package/dist/layout/layout-hierarchy.js.map +1 -0
- package/dist/layout/layout-network.d.ts +45 -0
- package/dist/layout/layout-network.d.ts.map +1 -0
- package/dist/layout/layout-network.js +197 -0
- package/dist/layout/layout-network.js.map +1 -0
- package/dist/layout/options.d.ts +29 -0
- package/dist/layout/options.d.ts.map +1 -0
- package/dist/layout/options.js +2 -0
- package/dist/layout/options.js.map +1 -0
- package/dist/render/_fallback-chain.d.ts +13 -0
- package/dist/render/_fallback-chain.d.ts.map +1 -0
- package/dist/render/_fallback-chain.js +41 -0
- package/dist/render/_fallback-chain.js.map +1 -0
- package/dist/render/capabilities.d.ts +13 -0
- package/dist/render/capabilities.d.ts.map +1 -1
- package/dist/render/capabilities.js +40 -10
- package/dist/render/capabilities.js.map +1 -1
- package/dist/resolve/_calendar-interval.d.ts +10 -0
- package/dist/resolve/_calendar-interval.d.ts.map +1 -0
- package/dist/resolve/_calendar-interval.js +27 -0
- package/dist/resolve/_calendar-interval.js.map +1 -0
- package/dist/resolve/_coords.d.ts +35 -0
- package/dist/resolve/_coords.d.ts.map +1 -0
- package/dist/resolve/_coords.js +354 -0
- package/dist/resolve/_coords.js.map +1 -0
- package/dist/resolve/_grid-field.d.ts +16 -0
- package/dist/resolve/_grid-field.d.ts.map +1 -0
- package/dist/resolve/_grid-field.js +23 -0
- package/dist/resolve/_grid-field.js.map +1 -0
- package/dist/resolve/_guides.d.ts +16 -0
- package/dist/resolve/_guides.d.ts.map +1 -0
- package/dist/resolve/_guides.js +66 -0
- package/dist/resolve/_guides.js.map +1 -0
- package/dist/resolve/_options.d.ts +17 -0
- package/dist/resolve/_options.d.ts.map +1 -0
- package/dist/resolve/_options.js +38 -0
- package/dist/resolve/_options.js.map +1 -0
- package/dist/resolve/errors.d.ts +63 -0
- package/dist/resolve/errors.d.ts.map +1 -0
- package/dist/resolve/errors.js +91 -0
- package/dist/resolve/errors.js.map +1 -0
- package/dist/resolve/options.d.ts +30 -0
- package/dist/resolve/options.d.ts.map +1 -0
- package/dist/resolve/options.js +2 -0
- package/dist/resolve/options.js.map +1 -0
- package/dist/resolve/scale-ticks.d.ts +91 -0
- package/dist/resolve/scale-ticks.d.ts.map +1 -0
- package/dist/resolve/scale-ticks.js +231 -0
- package/dist/resolve/scale-ticks.js.map +1 -0
- package/dist/resolve/scale-transform.d.ts +102 -0
- package/dist/resolve/scale-transform.d.ts.map +1 -0
- package/dist/resolve/scale-transform.js +466 -0
- package/dist/resolve/scale-transform.js.map +1 -0
- package/dist/resolve/scale.d.ts +139 -0
- package/dist/resolve/scale.d.ts.map +1 -0
- package/dist/resolve/scale.js +710 -0
- package/dist/resolve/scale.js.map +1 -0
- package/dist/resolve/series.d.ts +26 -0
- package/dist/resolve/series.d.ts.map +1 -0
- package/dist/resolve/series.js +105 -0
- package/dist/resolve/series.js.map +1 -0
- package/dist/resolve/view-types.d.ts +223 -0
- package/dist/resolve/view-types.d.ts.map +1 -0
- package/dist/resolve/view-types.js +2 -0
- package/dist/resolve/view-types.js.map +1 -0
- package/dist/resolve/view.d.ts +43 -0
- package/dist/resolve/view.d.ts.map +1 -0
- package/dist/resolve/view.js +621 -0
- package/dist/resolve/view.js.map +1 -0
- package/dist/spec/channel.d.ts +3 -1
- package/dist/spec/channel.d.ts.map +1 -1
- package/dist/spec/channel.js.map +1 -1
- package/dist/spec/coord.d.ts +17 -0
- package/dist/spec/coord.d.ts.map +1 -1
- package/dist/spec/data-sources.js.map +1 -1
- package/dist/spec/mark.d.ts +6 -1
- package/dist/spec/mark.d.ts.map +1 -1
- package/dist/spec/scale.d.ts +21 -2
- package/dist/spec/scale.d.ts.map +1 -1
- package/dist/spec/transform.d.ts +32 -6
- package/dist/spec/transform.d.ts.map +1 -1
- package/dist/transform/aggregate.d.ts +28 -0
- package/dist/transform/aggregate.d.ts.map +1 -0
- package/dist/transform/aggregate.js +463 -0
- package/dist/transform/aggregate.js.map +1 -0
- package/dist/transform/apply-transforms.d.ts +26 -0
- package/dist/transform/apply-transforms.d.ts.map +1 -0
- package/dist/transform/apply-transforms.js +66 -0
- package/dist/transform/apply-transforms.js.map +1 -0
- package/dist/transform/bin.d.ts +30 -0
- package/dist/transform/bin.d.ts.map +1 -0
- package/dist/transform/bin.js +169 -0
- package/dist/transform/bin.js.map +1 -0
- package/dist/transform/columns.d.ts +24 -0
- package/dist/transform/columns.d.ts.map +1 -0
- package/dist/transform/columns.js +77 -0
- package/dist/transform/columns.js.map +1 -0
- package/dist/transform/filter.d.ts +11 -0
- package/dist/transform/filter.d.ts.map +1 -0
- package/dist/transform/filter.js +64 -0
- package/dist/transform/filter.js.map +1 -0
- package/dist/transform/group-rows.d.ts +25 -0
- package/dist/transform/group-rows.d.ts.map +1 -0
- package/dist/transform/group-rows.js +52 -0
- package/dist/transform/group-rows.js.map +1 -0
- package/dist/transform/sort.d.ts +10 -0
- package/dist/transform/sort.d.ts.map +1 -0
- package/dist/transform/sort.js +63 -0
- package/dist/transform/sort.js.map +1 -0
- package/dist/transform/stack.d.ts +37 -0
- package/dist/transform/stack.d.ts.map +1 -0
- package/dist/transform/stack.js +218 -0
- package/dist/transform/stack.js.map +1 -0
- package/package.json +22 -4
|
@@ -0,0 +1,710 @@
|
|
|
1
|
+
import { calendarCeil, calendarFloor, calendarNice } from "@ac-kit/core";
|
|
2
|
+
import { logNice, nice } from "@ac-kit/math-scalar";
|
|
3
|
+
import { quantile } from "@ac-kit/math-stats";
|
|
4
|
+
import { columnFiniteExtent, columnToFloat64Array, compareValues, getDataFrameColumnByName, getDataFrameFieldByName, isDataGrid, isNumericField, } from "@ac-kit/model-dataset";
|
|
5
|
+
import { calendarIntervalOf_, isCalendarIntervalSpec_, } from "./_calendar-interval.js";
|
|
6
|
+
import { gridFieldPart_ } from "./_grid-field.js";
|
|
7
|
+
import { checkResolveOptions_, } from "./_options.js";
|
|
8
|
+
import { InvalidScaleError } from "./errors.js";
|
|
9
|
+
const SCALE_CHANNELS_ = {
|
|
10
|
+
x: "x",
|
|
11
|
+
x2: "x",
|
|
12
|
+
y: "y",
|
|
13
|
+
y2: "y",
|
|
14
|
+
z: "z",
|
|
15
|
+
theta: "theta",
|
|
16
|
+
theta2: "theta",
|
|
17
|
+
radius: "radius",
|
|
18
|
+
radius2: "radius",
|
|
19
|
+
color: "color",
|
|
20
|
+
fill: "fill",
|
|
21
|
+
stroke: "stroke",
|
|
22
|
+
strokeWidth: "strokeWidth",
|
|
23
|
+
size: "size",
|
|
24
|
+
shape: "shape",
|
|
25
|
+
opacity: "opacity",
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The scale channel an encoded channel reads from: `x2` → `x`, `y2` → `y`,
|
|
29
|
+
* `theta2` → `theta`, `radius2` → `radius`.
|
|
30
|
+
*
|
|
31
|
+
* `undefined` for the channels without a scale: text, tooltip, detail, key,
|
|
32
|
+
* order, parent, source, target, geometry, longitude, latitude, row, column.
|
|
33
|
+
*
|
|
34
|
+
* O(1).
|
|
35
|
+
*/
|
|
36
|
+
export function scaleChannelOf(channel) {
|
|
37
|
+
return SCALE_CHANNELS_[channel];
|
|
38
|
+
}
|
|
39
|
+
const CONTINUOUS_KINDS_ = new Set([
|
|
40
|
+
"linear",
|
|
41
|
+
"log",
|
|
42
|
+
"pow",
|
|
43
|
+
"sqrt",
|
|
44
|
+
"symlog",
|
|
45
|
+
"time",
|
|
46
|
+
]);
|
|
47
|
+
const BINNING_KINDS_ = new Set([
|
|
48
|
+
"quantile",
|
|
49
|
+
"quantize",
|
|
50
|
+
"threshold",
|
|
51
|
+
]);
|
|
52
|
+
const ZERO_CHANNELS_ = new Set([
|
|
53
|
+
"x",
|
|
54
|
+
"y",
|
|
55
|
+
"z",
|
|
56
|
+
"theta",
|
|
57
|
+
"radius",
|
|
58
|
+
"size",
|
|
59
|
+
]);
|
|
60
|
+
// Vega-Lite nices x and y only: an angle or a radius keeps its data extent, so
|
|
61
|
+
// a pie closes at its total.
|
|
62
|
+
const NICE_CHANNELS_ = new Set(["x", "y", "z"]);
|
|
63
|
+
const DEFAULT_TICK_COUNT_ = 10;
|
|
64
|
+
const DEFAULT_BAND_PADDING_ = 0.1;
|
|
65
|
+
const DEFAULT_POINT_PADDING_ = 0.5;
|
|
66
|
+
/**
|
|
67
|
+
* The scale every input shares, resolved by Vega-Lite's documented defaults:
|
|
68
|
+
* kind, zero, nice, paddings and sort.
|
|
69
|
+
*
|
|
70
|
+
* An explicit `domain` is final: `zero`, `nice`, `include` and the midpoint
|
|
71
|
+
* apply only to a domain derived from the data. Shared inputs must agree on
|
|
72
|
+
* every scale parameter and on `sort` when they set one; the nice count is the
|
|
73
|
+
* first `axis.tickCount` set.
|
|
74
|
+
*
|
|
75
|
+
* O(Σ rows) for continuous and identity scales, O(Σ rows + d log d) for
|
|
76
|
+
* discrete ones, O(n log n) for quantile.
|
|
77
|
+
*
|
|
78
|
+
* @returns `undefined` for a channel without a scale.
|
|
79
|
+
* @throws {RangeError} No inputs, inputs on different scale channels, or
|
|
80
|
+
* invalid options.
|
|
81
|
+
* @throws {UnknownFieldError} A field missing from its source.
|
|
82
|
+
* @throws {InvalidScaleError} A kind the field or source can't feed, a
|
|
83
|
+
* malformed explicit domain, parameters out of range, or inputs that
|
|
84
|
+
* disagree.
|
|
85
|
+
*/
|
|
86
|
+
export function resolveScale(inputs, options = {}) {
|
|
87
|
+
if (inputs.length === 0) {
|
|
88
|
+
throw new RangeError("resolveScale needs at least one input");
|
|
89
|
+
}
|
|
90
|
+
const resolvedOptions = checkResolveOptions_(options);
|
|
91
|
+
const channel = scaleChannelOf(inputs[0].channel);
|
|
92
|
+
for (const input of inputs) {
|
|
93
|
+
if (scaleChannelOf(input.channel) !== channel) {
|
|
94
|
+
throw new RangeError(`Inputs on different scale channels: "${inputs[0].channel}" and "${input.channel}"`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (channel === undefined) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
const prepared = inputs.map(prepareInput_);
|
|
101
|
+
const errorChannel = inputs[0].channel;
|
|
102
|
+
const fields = distinctFields_(prepared);
|
|
103
|
+
const kind = resolveKind_(prepared, channel, errorChannel);
|
|
104
|
+
const reverse = unanimous_(prepared, (item) => item.spec?.reverse, errorChannel, "reverse") ?? false;
|
|
105
|
+
const base = { channel, fields, reverse };
|
|
106
|
+
if (kind === "identity") {
|
|
107
|
+
return { ...base, kind };
|
|
108
|
+
}
|
|
109
|
+
if (kind === "band" || kind === "point" || kind === "ordinal") {
|
|
110
|
+
return resolveDiscrete_(base, kind, prepared, errorChannel);
|
|
111
|
+
}
|
|
112
|
+
if (kind === "quantile" || kind === "quantize" || kind === "threshold") {
|
|
113
|
+
return resolveBinning_(base, kind, prepared, errorChannel, resolvedOptions.classCount);
|
|
114
|
+
}
|
|
115
|
+
return resolveContinuous_(base, kind, prepared, errorChannel, resolvedOptions, options.include);
|
|
116
|
+
}
|
|
117
|
+
function prepareInput_(input) {
|
|
118
|
+
const spec = input.encoding.scale;
|
|
119
|
+
const name = input.encoding.field;
|
|
120
|
+
if (!isDataGrid(input.source)) {
|
|
121
|
+
return {
|
|
122
|
+
input,
|
|
123
|
+
field: getDataFrameFieldByName(input.source, name),
|
|
124
|
+
spec,
|
|
125
|
+
column: getDataFrameColumnByName(input.source, name),
|
|
126
|
+
lattice: undefined,
|
|
127
|
+
isGrid: false,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const grid = input.source;
|
|
131
|
+
const { part, field } = gridFieldPart_(grid, input.channel, name);
|
|
132
|
+
if (part === "value") {
|
|
133
|
+
return {
|
|
134
|
+
input,
|
|
135
|
+
field,
|
|
136
|
+
spec,
|
|
137
|
+
column: grid.values,
|
|
138
|
+
lattice: undefined,
|
|
139
|
+
isGrid: true,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
input,
|
|
144
|
+
field,
|
|
145
|
+
spec,
|
|
146
|
+
column: undefined,
|
|
147
|
+
lattice: latticeExtent_(grid, part, input.mark === "rect"),
|
|
148
|
+
isGrid: true,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// Cell edges for a raster (`rect`), cell centres otherwise.
|
|
152
|
+
function latticeExtent_(grid, axisName, edges) {
|
|
153
|
+
const count = axisName === "x" ? grid.columns : grid.rows;
|
|
154
|
+
if (count === 0) {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
const { start, step } = grid[axisName];
|
|
158
|
+
const first = edges ? start - step / 2 : start;
|
|
159
|
+
const last = start + (count - 1) * step + (edges ? step / 2 : 0);
|
|
160
|
+
return first <= last ? [first, last] : [last, first];
|
|
161
|
+
}
|
|
162
|
+
function distinctFields_(prepared) {
|
|
163
|
+
const fields = [];
|
|
164
|
+
for (const item of prepared) {
|
|
165
|
+
if (!fields.includes(item.field)) {
|
|
166
|
+
fields.push(item.field);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return fields;
|
|
170
|
+
}
|
|
171
|
+
// The value every input that sets one agrees on.
|
|
172
|
+
function unanimous_(prepared, pick, channel, name, same = Object.is) {
|
|
173
|
+
let found;
|
|
174
|
+
for (const item of prepared) {
|
|
175
|
+
const value = pick(item);
|
|
176
|
+
if (value === undefined) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (found === undefined) {
|
|
180
|
+
found = value;
|
|
181
|
+
}
|
|
182
|
+
else if (!same(found, value)) {
|
|
183
|
+
throw new InvalidScaleError(channel, `Inputs of "${channel}" disagree on ${name}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return found;
|
|
187
|
+
}
|
|
188
|
+
function inferKind_(item, channel) {
|
|
189
|
+
switch (item.field.kind) {
|
|
190
|
+
case "quantitative":
|
|
191
|
+
case "cyclic":
|
|
192
|
+
return "linear";
|
|
193
|
+
case "temporal":
|
|
194
|
+
return "time";
|
|
195
|
+
case "nominal":
|
|
196
|
+
case "ordinal": {
|
|
197
|
+
if (channel !== "x" && channel !== "y") {
|
|
198
|
+
return "ordinal";
|
|
199
|
+
}
|
|
200
|
+
const mark = item.input.mark;
|
|
201
|
+
return mark === "bar" || mark === "rect" || mark === "surface"
|
|
202
|
+
? "band"
|
|
203
|
+
: "point";
|
|
204
|
+
}
|
|
205
|
+
default:
|
|
206
|
+
throw new InvalidScaleError(item.input.channel, `A ${item.field.kind} field ("${item.field.name}") can't drive a scale`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function isCompatible_(kind, field) {
|
|
210
|
+
if (field.kind === "geometry" || field.kind === "identifier") {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
if (CONTINUOUS_KINDS_.has(kind) || BINNING_KINDS_.has(kind)) {
|
|
214
|
+
return isNumericField(field);
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
function resolveKind_(prepared, channel, errorChannel) {
|
|
219
|
+
const explicit = unanimous_(prepared, (item) => item.spec?.kind, errorChannel, "kind");
|
|
220
|
+
let kind;
|
|
221
|
+
if (explicit !== undefined) {
|
|
222
|
+
kind = explicit;
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
const inferred = new Set(prepared.map((item) => inferKind_(item, channel)));
|
|
226
|
+
if (inferred.has("band")) {
|
|
227
|
+
inferred.delete("point");
|
|
228
|
+
}
|
|
229
|
+
if (inferred.size > 1) {
|
|
230
|
+
throw new InvalidScaleError(errorChannel, `Inputs of "${errorChannel}" disagree on kind: ${[...inferred].join(", ")}`);
|
|
231
|
+
}
|
|
232
|
+
kind = [...inferred][0];
|
|
233
|
+
}
|
|
234
|
+
for (const item of prepared) {
|
|
235
|
+
if (!isCompatible_(kind, item.field)) {
|
|
236
|
+
throw new InvalidScaleError(item.input.channel, `A ${kind} scale can't serve the ${item.field.kind} field "${item.field.name}"`);
|
|
237
|
+
}
|
|
238
|
+
if (item.isGrid &&
|
|
239
|
+
(kind === "band" || kind === "point" || kind === "ordinal")) {
|
|
240
|
+
throw new InvalidScaleError(item.input.channel, `A ${kind} scale can't serve a grid ("${item.field.name}")`);
|
|
241
|
+
}
|
|
242
|
+
if (item.lattice !== undefined && !CONTINUOUS_KINDS_.has(kind)) {
|
|
243
|
+
throw new InvalidScaleError(item.input.channel, `A lattice axis ("${item.field.name}") takes a continuous scale, not ${kind}`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return kind;
|
|
247
|
+
}
|
|
248
|
+
function numberOrThrow_(value, channel, what) {
|
|
249
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
250
|
+
throw new InvalidScaleError(channel, `${what} must be finite numbers`);
|
|
251
|
+
}
|
|
252
|
+
return value;
|
|
253
|
+
}
|
|
254
|
+
function sameDomain_(a, b) {
|
|
255
|
+
return a.length === b.length && a.every((value, index) => value === b[index]);
|
|
256
|
+
}
|
|
257
|
+
function explicitDomain_(prepared, channel) {
|
|
258
|
+
return unanimous_(prepared, (item) => item.spec?.domain, channel, "domain", sameDomain_);
|
|
259
|
+
}
|
|
260
|
+
function extentOf_(item, sign) {
|
|
261
|
+
if (item.field.kind === "cyclic") {
|
|
262
|
+
return sign === -1 ? undefined : [0, item.field.period];
|
|
263
|
+
}
|
|
264
|
+
if (item.lattice !== undefined) {
|
|
265
|
+
const [low, high] = item.lattice;
|
|
266
|
+
if (sign === 1 ? low <= 0 : sign === -1 && high >= 0) {
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
return [low, high];
|
|
270
|
+
}
|
|
271
|
+
const extent = columnFiniteExtent(item.column, sign);
|
|
272
|
+
return extent === undefined ? undefined : [extent[0], extent[1]];
|
|
273
|
+
}
|
|
274
|
+
function unionExtent_(prepared, sign) {
|
|
275
|
+
let union;
|
|
276
|
+
for (const item of prepared) {
|
|
277
|
+
const extent = extentOf_(item, sign);
|
|
278
|
+
if (extent === undefined) {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (union === undefined) {
|
|
282
|
+
union = extent;
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
union[0] = Math.min(union[0], extent[0]);
|
|
286
|
+
union[1] = Math.max(union[1], extent[1]);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return union;
|
|
290
|
+
}
|
|
291
|
+
function derivedExtent_(prepared, logarithmic) {
|
|
292
|
+
if (!logarithmic) {
|
|
293
|
+
return unionExtent_(prepared);
|
|
294
|
+
}
|
|
295
|
+
return unionExtent_(prepared, 1) ?? unionExtent_(prepared, -1);
|
|
296
|
+
}
|
|
297
|
+
function resolveContinuous_(base, kindName, prepared, channel, options, include) {
|
|
298
|
+
const kind = kindName === "sqrt" ? "pow" : kindName;
|
|
299
|
+
const logarithmic = kind === "log";
|
|
300
|
+
const parameters = continuousParameters_(kindName, prepared, channel);
|
|
301
|
+
const explicit = explicitDomain_(prepared, channel);
|
|
302
|
+
let domain;
|
|
303
|
+
let reverse = base.reverse;
|
|
304
|
+
if (explicit !== undefined) {
|
|
305
|
+
if (explicit.length !== 2) {
|
|
306
|
+
throw new InvalidScaleError(channel, `A continuous domain has two values, got ${explicit.length}`);
|
|
307
|
+
}
|
|
308
|
+
const first = numberOrThrow_(explicit[0], channel, "A continuous domain");
|
|
309
|
+
const second = numberOrThrow_(explicit[1], channel, "A continuous domain");
|
|
310
|
+
if (logarithmic && !(first * second > 0)) {
|
|
311
|
+
throw new InvalidScaleError(channel, "A log domain has two non-zero values of one sign");
|
|
312
|
+
}
|
|
313
|
+
domain = [Math.min(first, second), Math.max(first, second)];
|
|
314
|
+
reverse = reverse !== first > second;
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
domain = derivedExtent_(prepared, logarithmic);
|
|
318
|
+
}
|
|
319
|
+
const midpoint = resolveMidpoint_(prepared, channel);
|
|
320
|
+
if (explicit === undefined) {
|
|
321
|
+
domain = widenDerived_(domain, kind, base.channel, prepared, channel, options, include, midpoint);
|
|
322
|
+
}
|
|
323
|
+
const finalDomain = domain ?? (logarithmic ? [1, 10] : [0, 1]);
|
|
324
|
+
// A negative power is singular at zero: T(0) is infinite, so no affine
|
|
325
|
+
// position exists on a domain that reaches it.
|
|
326
|
+
if (kind === "pow" &&
|
|
327
|
+
parameters.exponent < 0 &&
|
|
328
|
+
finalDomain[0] <= 0 &&
|
|
329
|
+
finalDomain[1] >= 0) {
|
|
330
|
+
throw new InvalidScaleError(channel, `A pow scale with exponent ${parameters.exponent} is undefined at zero; its domain [${finalDomain[0]}, ${finalDomain[1]}] reaches it (set zero to false, or give a domain away from zero)`);
|
|
331
|
+
}
|
|
332
|
+
return {
|
|
333
|
+
...base,
|
|
334
|
+
reverse,
|
|
335
|
+
kind,
|
|
336
|
+
...parameters,
|
|
337
|
+
domain: finalDomain,
|
|
338
|
+
clamp: unanimous_(prepared, (item) => item.spec?.clamp, channel, "clamp") ??
|
|
339
|
+
false,
|
|
340
|
+
midpoint: midpoint !== undefined && logarithmic && !fitsLog_(midpoint, domain)
|
|
341
|
+
? undefined
|
|
342
|
+
: midpoint,
|
|
343
|
+
...(kind === "time"
|
|
344
|
+
? { timeZone: options.timeZone, weekStart: options.weekStart }
|
|
345
|
+
: {}),
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
function fitsLog_(value, domain) {
|
|
349
|
+
if (domain === undefined) {
|
|
350
|
+
return value > 0;
|
|
351
|
+
}
|
|
352
|
+
return domain[0] > 0 ? value > 0 : value < 0;
|
|
353
|
+
}
|
|
354
|
+
function continuousParameters_(kind, prepared, channel) {
|
|
355
|
+
if (kind === "log") {
|
|
356
|
+
const base = unanimous_(prepared, (item) => item.spec?.base, channel, "base") ?? 10;
|
|
357
|
+
if (!Number.isFinite(base) || base <= 0 || base === 1) {
|
|
358
|
+
throw new InvalidScaleError(channel, `A log base is finite, positive and not 1, got ${base}`);
|
|
359
|
+
}
|
|
360
|
+
return { base };
|
|
361
|
+
}
|
|
362
|
+
if (kind === "sqrt") {
|
|
363
|
+
return { exponent: 0.5 };
|
|
364
|
+
}
|
|
365
|
+
if (kind === "pow") {
|
|
366
|
+
const exponent = unanimous_(prepared, (item) => item.spec?.exponent, channel, "exponent") ?? 1;
|
|
367
|
+
if (!Number.isFinite(exponent) || exponent === 0) {
|
|
368
|
+
throw new InvalidScaleError(channel, `A pow exponent is finite and non-zero, got ${exponent}`);
|
|
369
|
+
}
|
|
370
|
+
return { exponent };
|
|
371
|
+
}
|
|
372
|
+
if (kind === "symlog") {
|
|
373
|
+
return { constant: 1 };
|
|
374
|
+
}
|
|
375
|
+
return {};
|
|
376
|
+
}
|
|
377
|
+
function resolveMidpoint_(prepared, channel) {
|
|
378
|
+
const midpoint = unanimous_(prepared, (item) => item.spec?.scheme?.kind === "diverging"
|
|
379
|
+
? (item.spec.scheme.midpoint ?? 0)
|
|
380
|
+
: undefined, channel, "midpoint");
|
|
381
|
+
if (midpoint !== undefined && !Number.isFinite(midpoint)) {
|
|
382
|
+
throw new InvalidScaleError(channel, "A midpoint must be finite");
|
|
383
|
+
}
|
|
384
|
+
return midpoint;
|
|
385
|
+
}
|
|
386
|
+
// Zero, include and midpoint, then nice, on a domain derived from the data.
|
|
387
|
+
function widenDerived_(derived, kind, scaleChannel, prepared, channel, options, include, midpoint) {
|
|
388
|
+
let domain = derived === undefined ? undefined : [...derived];
|
|
389
|
+
const widen = (value) => {
|
|
390
|
+
if (!Number.isFinite(value)) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (domain === undefined) {
|
|
394
|
+
domain = [value, value];
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
domain[0] = Math.min(domain[0], value);
|
|
398
|
+
domain[1] = Math.max(domain[1], value);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
const logarithmic = kind === "log";
|
|
402
|
+
const cyclic = prepared.some((item) => item.field.kind === "cyclic");
|
|
403
|
+
const zero = unanimous_(prepared, (item) => item.spec?.zero, channel, "zero") ??
|
|
404
|
+
(ZERO_CHANNELS_.has(scaleChannel) &&
|
|
405
|
+
(kind === "linear" || kind === "pow" || kind === "symlog") &&
|
|
406
|
+
!prepared.some((item) => item.input.binned === true));
|
|
407
|
+
if (zero && !logarithmic && kind !== "time" && domain !== undefined) {
|
|
408
|
+
widen(0);
|
|
409
|
+
}
|
|
410
|
+
// A log domain holds one sign, set by the data, else by the first value.
|
|
411
|
+
const accepts = (value) => !logarithmic ||
|
|
412
|
+
(domain === undefined ? value !== 0 : fitsLog_(value, domain));
|
|
413
|
+
for (const value of include ?? []) {
|
|
414
|
+
if (accepts(value)) {
|
|
415
|
+
widen(value);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (midpoint !== undefined && accepts(midpoint)) {
|
|
419
|
+
widen(midpoint);
|
|
420
|
+
}
|
|
421
|
+
if (domain === undefined) {
|
|
422
|
+
return undefined;
|
|
423
|
+
}
|
|
424
|
+
const niceDefault = NICE_CHANNELS_.has(scaleChannel) &&
|
|
425
|
+
(kind === "linear" ||
|
|
426
|
+
kind === "pow" ||
|
|
427
|
+
kind === "symlog" ||
|
|
428
|
+
kind === "log") &&
|
|
429
|
+
!prepared.some((item) => item.input.binned === true);
|
|
430
|
+
const niceSpec = unanimous_(prepared, (item) => item.spec?.nice, channel, "nice", sameNice_) ?? niceDefault;
|
|
431
|
+
if (niceSpec === false || cyclic) {
|
|
432
|
+
return domain;
|
|
433
|
+
}
|
|
434
|
+
if (isCalendarIntervalSpec_(niceSpec)) {
|
|
435
|
+
const interval = calendarIntervalOf_(niceSpec);
|
|
436
|
+
if (kind !== "time" || interval === undefined) {
|
|
437
|
+
throw new InvalidScaleError(channel, kind === "time"
|
|
438
|
+
? `A nice interval has a calendar unit and a positive integer step, got ${JSON.stringify(niceSpec)}`
|
|
439
|
+
: `A nice calendar interval needs a time scale, not ${kind}`);
|
|
440
|
+
}
|
|
441
|
+
const calendarOptions = { weekStart: options.weekStart };
|
|
442
|
+
return [
|
|
443
|
+
calendarFloor(domain[0], interval, options.timeZone, calendarOptions),
|
|
444
|
+
calendarCeil(domain[1], interval, options.timeZone, calendarOptions),
|
|
445
|
+
];
|
|
446
|
+
}
|
|
447
|
+
if (typeof niceSpec === "number" &&
|
|
448
|
+
!(niceSpec > 0 && niceSpec - niceSpec === 0)) {
|
|
449
|
+
throw new InvalidScaleError(channel, `A nice count is a positive finite number, got ${niceSpec}`);
|
|
450
|
+
}
|
|
451
|
+
const count = typeof niceSpec === "number"
|
|
452
|
+
? niceSpec
|
|
453
|
+
: (firstTickCount_(prepared, channel) ??
|
|
454
|
+
options.tickCount ??
|
|
455
|
+
DEFAULT_TICK_COUNT_);
|
|
456
|
+
if (kind === "log") {
|
|
457
|
+
const base = unanimous_(prepared, (item) => item.spec?.base, channel, "base") ?? 10;
|
|
458
|
+
return logNice(domain[0], domain[1], base);
|
|
459
|
+
}
|
|
460
|
+
if (kind === "time") {
|
|
461
|
+
return calendarNice(domain[0], domain[1], count, options.timeZone, {
|
|
462
|
+
weekStart: options.weekStart,
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
return nice(domain[0], domain[1], count);
|
|
466
|
+
}
|
|
467
|
+
// Equal nice settings: the same flag or count, or the same interval however
|
|
468
|
+
// it is written ("month" and { unit: "month", step: 1 } agree).
|
|
469
|
+
function sameNice_(a, b) {
|
|
470
|
+
if (isCalendarIntervalSpec_(a) && isCalendarIntervalSpec_(b)) {
|
|
471
|
+
const left = calendarIntervalOf_(a);
|
|
472
|
+
const right = calendarIntervalOf_(b);
|
|
473
|
+
return (left !== undefined &&
|
|
474
|
+
right !== undefined &&
|
|
475
|
+
left.unit === right.unit &&
|
|
476
|
+
left.step === right.step);
|
|
477
|
+
}
|
|
478
|
+
return a === b;
|
|
479
|
+
}
|
|
480
|
+
function firstTickCount_(prepared, channel) {
|
|
481
|
+
for (const item of prepared) {
|
|
482
|
+
const count = item.input.encoding.axis?.tickCount;
|
|
483
|
+
if (count === undefined) {
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
if (!Number.isInteger(count) || count <= 0) {
|
|
487
|
+
throw new InvalidScaleError(channel, `An axis tickCount is a positive integer, got ${count}`);
|
|
488
|
+
}
|
|
489
|
+
return count;
|
|
490
|
+
}
|
|
491
|
+
return undefined;
|
|
492
|
+
}
|
|
493
|
+
function resolveBinning_(base, kind, prepared, channel, classCount) {
|
|
494
|
+
const explicit = explicitDomain_(prepared, channel);
|
|
495
|
+
const numbers = (explicit ?? []).map((value) => numberOrThrow_(value, channel, `A ${kind} domain`));
|
|
496
|
+
if (kind === "threshold") {
|
|
497
|
+
if (explicit === undefined || numbers.length === 0) {
|
|
498
|
+
throw new InvalidScaleError(channel, "A threshold scale needs an explicit domain of cut points");
|
|
499
|
+
}
|
|
500
|
+
for (let index = 1; index < numbers.length; index++) {
|
|
501
|
+
if (numbers[index] < numbers[index - 1]) {
|
|
502
|
+
throw new InvalidScaleError(channel, "Threshold cut points must be ascending");
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return {
|
|
506
|
+
...base,
|
|
507
|
+
kind,
|
|
508
|
+
domain: [numbers[0], numbers.at(-1)],
|
|
509
|
+
thresholds: numbers,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
if (kind === "quantize") {
|
|
513
|
+
let extent;
|
|
514
|
+
if (explicit !== undefined) {
|
|
515
|
+
if (numbers.length !== 2) {
|
|
516
|
+
throw new InvalidScaleError(channel, `A quantize domain has two values, got ${numbers.length}`);
|
|
517
|
+
}
|
|
518
|
+
extent = [Math.min(...numbers), Math.max(...numbers)];
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
extent = unionExtent_(prepared) ?? [0, 1];
|
|
522
|
+
}
|
|
523
|
+
const [low, high] = extent;
|
|
524
|
+
const thresholds = [];
|
|
525
|
+
for (let index = 1; index < classCount; index++) {
|
|
526
|
+
thresholds.push(low + ((high - low) * index) / classCount);
|
|
527
|
+
}
|
|
528
|
+
return { ...base, kind, domain: [low, high], thresholds };
|
|
529
|
+
}
|
|
530
|
+
const sample = explicit === undefined
|
|
531
|
+
? gatherSample_(prepared, channel)
|
|
532
|
+
: sortedCopy_(numbers);
|
|
533
|
+
const domain = sample.length === 0 ? [0, 1] : [sample[0], sample[sample.length - 1]];
|
|
534
|
+
const thresholds = [];
|
|
535
|
+
for (let index = 1; index < classCount; index++) {
|
|
536
|
+
thresholds.push(sample.length === 0
|
|
537
|
+
? index / classCount
|
|
538
|
+
: quantile(sample, index / classCount));
|
|
539
|
+
}
|
|
540
|
+
return { ...base, kind, domain, thresholds };
|
|
541
|
+
}
|
|
542
|
+
function sortedCopy_(values) {
|
|
543
|
+
return Float64Array.from(values).sort();
|
|
544
|
+
}
|
|
545
|
+
// A sorted copy of every finite value.
|
|
546
|
+
function gatherSample_(prepared, channel) {
|
|
547
|
+
let total = 0;
|
|
548
|
+
for (const item of prepared) {
|
|
549
|
+
if (item.column === undefined) {
|
|
550
|
+
throw new InvalidScaleError(channel, `A quantile scale can't serve the lattice axis "${item.field.name}"`);
|
|
551
|
+
}
|
|
552
|
+
total += item.column.length;
|
|
553
|
+
}
|
|
554
|
+
const all = new Float64Array(total);
|
|
555
|
+
let used = 0;
|
|
556
|
+
for (const item of prepared) {
|
|
557
|
+
const column = item.column;
|
|
558
|
+
columnToFloat64Array(all.subarray(used, used + column.length), column);
|
|
559
|
+
used += column.length;
|
|
560
|
+
}
|
|
561
|
+
let kept = 0;
|
|
562
|
+
for (let index = 0; index < total; index++) {
|
|
563
|
+
const value = all[index];
|
|
564
|
+
if (Number.isFinite(value)) {
|
|
565
|
+
all[kept++] = value;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return all.subarray(0, kept).sort();
|
|
569
|
+
}
|
|
570
|
+
function sameSort_(a, b) {
|
|
571
|
+
if (typeof a === "object" &&
|
|
572
|
+
a !== null &&
|
|
573
|
+
typeof b === "object" &&
|
|
574
|
+
b !== null) {
|
|
575
|
+
return (a.field === b.field &&
|
|
576
|
+
(a.order ?? "ascending") === (b.order ?? "ascending"));
|
|
577
|
+
}
|
|
578
|
+
return a === b;
|
|
579
|
+
}
|
|
580
|
+
function resolveDiscrete_(base, kind, prepared, channel) {
|
|
581
|
+
const explicit = explicitDomain_(prepared, channel);
|
|
582
|
+
const domain = explicit === undefined
|
|
583
|
+
? derivedDiscreteDomain_(prepared, channel)
|
|
584
|
+
: [...new Set(explicit)];
|
|
585
|
+
if (kind === "ordinal") {
|
|
586
|
+
return { ...base, kind, domain };
|
|
587
|
+
}
|
|
588
|
+
const padding = unanimous_(prepared, (item) => item.spec?.padding, channel, "padding");
|
|
589
|
+
if (padding !== undefined && !(padding >= 0 && padding <= 1)) {
|
|
590
|
+
throw new InvalidScaleError(channel, `A padding lies in [0, 1], got ${padding}`);
|
|
591
|
+
}
|
|
592
|
+
if (kind === "point") {
|
|
593
|
+
return {
|
|
594
|
+
...base,
|
|
595
|
+
kind,
|
|
596
|
+
domain,
|
|
597
|
+
padding: padding ?? DEFAULT_POINT_PADDING_,
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
const flat = prepared.some((item) => item.input.mark === "rect" || item.input.mark === "surface");
|
|
601
|
+
const inner = padding ?? (flat ? 0 : DEFAULT_BAND_PADDING_);
|
|
602
|
+
return {
|
|
603
|
+
...base,
|
|
604
|
+
kind,
|
|
605
|
+
domain,
|
|
606
|
+
paddingInner: inner,
|
|
607
|
+
paddingOuter: padding ?? inner / 2,
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
function derivedDiscreteDomain_(prepared, channel) {
|
|
611
|
+
const sort = unanimous_(prepared, (item) => item.input.encoding.sort, channel, "sort", sameSort_);
|
|
612
|
+
const seen = new Set();
|
|
613
|
+
const order = [];
|
|
614
|
+
for (const item of prepared) {
|
|
615
|
+
const column = item.column;
|
|
616
|
+
for (let index = 0; index < column.length; index++) {
|
|
617
|
+
const value = column[index];
|
|
618
|
+
if (value === null ||
|
|
619
|
+
(typeof value === "number" && Number.isNaN(value)) ||
|
|
620
|
+
seen.has(value)) {
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
seen.add(value);
|
|
624
|
+
order.push(value);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
if (sort === null) {
|
|
628
|
+
return order;
|
|
629
|
+
}
|
|
630
|
+
if (typeof sort === "object") {
|
|
631
|
+
return sortByAggregate_(order, prepared, sort);
|
|
632
|
+
}
|
|
633
|
+
if (sort === "descending") {
|
|
634
|
+
return order.sort((a, b) => compareValues(b, a));
|
|
635
|
+
}
|
|
636
|
+
if (sort === "ascending") {
|
|
637
|
+
return order.sort(compareValues);
|
|
638
|
+
}
|
|
639
|
+
// An ordinal field's declared categories lead, in their order; the rest
|
|
640
|
+
// follows, ascending.
|
|
641
|
+
const declared = new Set();
|
|
642
|
+
const described = [];
|
|
643
|
+
for (const item of prepared) {
|
|
644
|
+
if (item.field.kind === "ordinal" && item.field.domain !== undefined) {
|
|
645
|
+
for (const value of item.field.domain) {
|
|
646
|
+
if (!declared.has(value)) {
|
|
647
|
+
declared.add(value);
|
|
648
|
+
described.push(value);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
if (described.length === 0) {
|
|
654
|
+
return order.sort(compareValues);
|
|
655
|
+
}
|
|
656
|
+
const undeclared = new Set();
|
|
657
|
+
for (const item of prepared) {
|
|
658
|
+
if (item.field.kind === "ordinal" && item.field.domain !== undefined) {
|
|
659
|
+
continue;
|
|
660
|
+
}
|
|
661
|
+
const column = item.column;
|
|
662
|
+
for (let index = 0; index < column.length; index++) {
|
|
663
|
+
const value = column[index];
|
|
664
|
+
if (value !== null && !declared.has(value) && !Number.isNaN(value)) {
|
|
665
|
+
undeclared.add(value);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
return described.concat([...undeclared].sort(compareValues));
|
|
670
|
+
}
|
|
671
|
+
// `sum` for stacked layers, `min` otherwise (Vega-Lite); categories with no
|
|
672
|
+
// finite value go last. Stable.
|
|
673
|
+
function sortByAggregate_(categories, prepared, sort) {
|
|
674
|
+
const aggregate = new Map();
|
|
675
|
+
for (const item of prepared) {
|
|
676
|
+
const encodingSort = item.input.encoding.sort;
|
|
677
|
+
if (typeof encodingSort !== "object" ||
|
|
678
|
+
encodingSort === null ||
|
|
679
|
+
isDataGrid(item.input.source)) {
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
const keys = item.column;
|
|
683
|
+
const measure = getDataFrameColumnByName(item.input.source, sort.field);
|
|
684
|
+
const stacked = item.input.stacked === true;
|
|
685
|
+
for (let index = 0; index < keys.length; index++) {
|
|
686
|
+
const key = keys[index];
|
|
687
|
+
const raw = measure[index];
|
|
688
|
+
const amount = typeof raw === "bigint" ? Number(raw) : raw;
|
|
689
|
+
if (typeof amount !== "number" || !Number.isFinite(amount)) {
|
|
690
|
+
continue;
|
|
691
|
+
}
|
|
692
|
+
const previous = aggregate.get(key);
|
|
693
|
+
aggregate.set(key, previous === undefined
|
|
694
|
+
? amount
|
|
695
|
+
: stacked
|
|
696
|
+
? previous + amount
|
|
697
|
+
: Math.min(previous, amount));
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
const sign = sort.order === "descending" ? -1 : 1;
|
|
701
|
+
return categories.sort((a, b) => {
|
|
702
|
+
const left = aggregate.get(a);
|
|
703
|
+
const right = aggregate.get(b);
|
|
704
|
+
if (left === undefined || right === undefined) {
|
|
705
|
+
return left === right ? 0 : left === undefined ? 1 : -1;
|
|
706
|
+
}
|
|
707
|
+
return sign * (left - right);
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
//# sourceMappingURL=scale.js.map
|