@ac-kit/model-chart 0.2.0 → 0.4.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.
Files changed (162) hide show
  1. package/CHANGELOG.md +311 -0
  2. package/README.md +174 -4
  3. package/dist/geographic/index.d.ts +2 -0
  4. package/dist/geographic/index.d.ts.map +1 -0
  5. package/dist/geographic/index.js +2 -0
  6. package/dist/geographic/index.js.map +1 -0
  7. package/dist/geographic/project-geographic-layer.d.ts +54 -0
  8. package/dist/geographic/project-geographic-layer.d.ts.map +1 -0
  9. package/dist/geographic/project-geographic-layer.js +214 -0
  10. package/dist/geographic/project-geographic-layer.js.map +1 -0
  11. package/dist/index.d.ts +12 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +12 -0
  14. package/dist/index.js.map +1 -1
  15. package/dist/layout/_fit.d.ts +12 -0
  16. package/dist/layout/_fit.d.ts.map +1 -0
  17. package/dist/layout/_fit.js +52 -0
  18. package/dist/layout/_fit.js.map +1 -0
  19. package/dist/layout/index.d.ts +4 -0
  20. package/dist/layout/index.d.ts.map +1 -0
  21. package/dist/layout/index.js +4 -0
  22. package/dist/layout/index.js.map +1 -0
  23. package/dist/layout/layout-hierarchy.d.ts +35 -0
  24. package/dist/layout/layout-hierarchy.d.ts.map +1 -0
  25. package/dist/layout/layout-hierarchy.js +98 -0
  26. package/dist/layout/layout-hierarchy.js.map +1 -0
  27. package/dist/layout/layout-network.d.ts +45 -0
  28. package/dist/layout/layout-network.d.ts.map +1 -0
  29. package/dist/layout/layout-network.js +197 -0
  30. package/dist/layout/layout-network.js.map +1 -0
  31. package/dist/layout/options.d.ts +29 -0
  32. package/dist/layout/options.d.ts.map +1 -0
  33. package/dist/layout/options.js +2 -0
  34. package/dist/layout/options.js.map +1 -0
  35. package/dist/render/_fallback-chain.d.ts +13 -0
  36. package/dist/render/_fallback-chain.d.ts.map +1 -0
  37. package/dist/render/_fallback-chain.js +41 -0
  38. package/dist/render/_fallback-chain.js.map +1 -0
  39. package/dist/render/capabilities.d.ts +26 -0
  40. package/dist/render/capabilities.d.ts.map +1 -1
  41. package/dist/render/capabilities.js +48 -10
  42. package/dist/render/capabilities.js.map +1 -1
  43. package/dist/resolve/__fixtures__/vega-lite-views.json +4727 -0
  44. package/dist/resolve/_calendar-interval.d.ts +10 -0
  45. package/dist/resolve/_calendar-interval.d.ts.map +1 -0
  46. package/dist/resolve/_calendar-interval.js +27 -0
  47. package/dist/resolve/_calendar-interval.js.map +1 -0
  48. package/dist/resolve/_coords.d.ts +35 -0
  49. package/dist/resolve/_coords.d.ts.map +1 -0
  50. package/dist/resolve/_coords.js +354 -0
  51. package/dist/resolve/_coords.js.map +1 -0
  52. package/dist/resolve/_grid-field.d.ts +16 -0
  53. package/dist/resolve/_grid-field.d.ts.map +1 -0
  54. package/dist/resolve/_grid-field.js +23 -0
  55. package/dist/resolve/_grid-field.js.map +1 -0
  56. package/dist/resolve/_guides.d.ts +19 -0
  57. package/dist/resolve/_guides.d.ts.map +1 -0
  58. package/dist/resolve/_guides.js +75 -0
  59. package/dist/resolve/_guides.js.map +1 -0
  60. package/dist/resolve/_options.d.ts +17 -0
  61. package/dist/resolve/_options.d.ts.map +1 -0
  62. package/dist/resolve/_options.js +38 -0
  63. package/dist/resolve/_options.js.map +1 -0
  64. package/dist/resolve/color-scheme.d.ts +21 -0
  65. package/dist/resolve/color-scheme.d.ts.map +1 -0
  66. package/dist/resolve/color-scheme.js +42 -0
  67. package/dist/resolve/color-scheme.js.map +1 -0
  68. package/dist/resolve/errors.d.ts +63 -0
  69. package/dist/resolve/errors.d.ts.map +1 -0
  70. package/dist/resolve/errors.js +91 -0
  71. package/dist/resolve/errors.js.map +1 -0
  72. package/dist/resolve/mark-geometry.d.ts +154 -0
  73. package/dist/resolve/mark-geometry.d.ts.map +1 -0
  74. package/dist/resolve/mark-geometry.js +517 -0
  75. package/dist/resolve/mark-geometry.js.map +1 -0
  76. package/dist/resolve/options.d.ts +30 -0
  77. package/dist/resolve/options.d.ts.map +1 -0
  78. package/dist/resolve/options.js +2 -0
  79. package/dist/resolve/options.js.map +1 -0
  80. package/dist/resolve/scale-tick-labels.d.ts +39 -0
  81. package/dist/resolve/scale-tick-labels.d.ts.map +1 -0
  82. package/dist/resolve/scale-tick-labels.js +158 -0
  83. package/dist/resolve/scale-tick-labels.js.map +1 -0
  84. package/dist/resolve/scale-ticks.d.ts +91 -0
  85. package/dist/resolve/scale-ticks.d.ts.map +1 -0
  86. package/dist/resolve/scale-ticks.js +231 -0
  87. package/dist/resolve/scale-ticks.js.map +1 -0
  88. package/dist/resolve/scale-transform.d.ts +102 -0
  89. package/dist/resolve/scale-transform.d.ts.map +1 -0
  90. package/dist/resolve/scale-transform.js +466 -0
  91. package/dist/resolve/scale-transform.js.map +1 -0
  92. package/dist/resolve/scale.d.ts +147 -0
  93. package/dist/resolve/scale.d.ts.map +1 -0
  94. package/dist/resolve/scale.js +730 -0
  95. package/dist/resolve/scale.js.map +1 -0
  96. package/dist/resolve/series.d.ts +26 -0
  97. package/dist/resolve/series.d.ts.map +1 -0
  98. package/dist/resolve/series.js +105 -0
  99. package/dist/resolve/series.js.map +1 -0
  100. package/dist/resolve/view-types.d.ts +226 -0
  101. package/dist/resolve/view-types.d.ts.map +1 -0
  102. package/dist/resolve/view-types.js +2 -0
  103. package/dist/resolve/view-types.js.map +1 -0
  104. package/dist/resolve/view.d.ts +43 -0
  105. package/dist/resolve/view.d.ts.map +1 -0
  106. package/dist/resolve/view.js +709 -0
  107. package/dist/resolve/view.js.map +1 -0
  108. package/dist/spec/channel.d.ts +3 -1
  109. package/dist/spec/channel.d.ts.map +1 -1
  110. package/dist/spec/channel.js +2 -0
  111. package/dist/spec/channel.js.map +1 -1
  112. package/dist/spec/coord.d.ts +17 -0
  113. package/dist/spec/coord.d.ts.map +1 -1
  114. package/dist/spec/data-sources.js.map +1 -1
  115. package/dist/spec/mark.d.ts +6 -1
  116. package/dist/spec/mark.d.ts.map +1 -1
  117. package/dist/spec/scale.d.ts +21 -2
  118. package/dist/spec/scale.d.ts.map +1 -1
  119. package/dist/spec/transform.d.ts +32 -6
  120. package/dist/spec/transform.d.ts.map +1 -1
  121. package/dist/testing/__fixtures__/vega-geometry.json +1434 -0
  122. package/dist/testing/index.d.ts +2 -0
  123. package/dist/testing/index.d.ts.map +1 -0
  124. package/dist/testing/index.js +2 -0
  125. package/dist/testing/index.js.map +1 -0
  126. package/dist/testing/vega-lite-cases.d.ts +164 -0
  127. package/dist/testing/vega-lite-cases.d.ts.map +1 -0
  128. package/dist/testing/vega-lite-cases.js +143 -0
  129. package/dist/testing/vega-lite-cases.js.map +1 -0
  130. package/dist/transform/aggregate.d.ts +28 -0
  131. package/dist/transform/aggregate.d.ts.map +1 -0
  132. package/dist/transform/aggregate.js +463 -0
  133. package/dist/transform/aggregate.js.map +1 -0
  134. package/dist/transform/apply-transforms.d.ts +26 -0
  135. package/dist/transform/apply-transforms.d.ts.map +1 -0
  136. package/dist/transform/apply-transforms.js +66 -0
  137. package/dist/transform/apply-transforms.js.map +1 -0
  138. package/dist/transform/bin.d.ts +30 -0
  139. package/dist/transform/bin.d.ts.map +1 -0
  140. package/dist/transform/bin.js +169 -0
  141. package/dist/transform/bin.js.map +1 -0
  142. package/dist/transform/columns.d.ts +24 -0
  143. package/dist/transform/columns.d.ts.map +1 -0
  144. package/dist/transform/columns.js +77 -0
  145. package/dist/transform/columns.js.map +1 -0
  146. package/dist/transform/filter.d.ts +11 -0
  147. package/dist/transform/filter.d.ts.map +1 -0
  148. package/dist/transform/filter.js +64 -0
  149. package/dist/transform/filter.js.map +1 -0
  150. package/dist/transform/group-rows.d.ts +25 -0
  151. package/dist/transform/group-rows.d.ts.map +1 -0
  152. package/dist/transform/group-rows.js +52 -0
  153. package/dist/transform/group-rows.js.map +1 -0
  154. package/dist/transform/sort.d.ts +10 -0
  155. package/dist/transform/sort.d.ts.map +1 -0
  156. package/dist/transform/sort.js +63 -0
  157. package/dist/transform/sort.js.map +1 -0
  158. package/dist/transform/stack.d.ts +37 -0
  159. package/dist/transform/stack.d.ts.map +1 -0
  160. package/dist/transform/stack.js +218 -0
  161. package/dist/transform/stack.js.map +1 -0
  162. package/package.json +26 -5
@@ -0,0 +1,709 @@
1
+ import { compareValues, dataFrameFromDataGrid, dataFrameSelectFields, dataFrameTakeRows, fieldTitle, getDataFrameColumnByName, getDataFrameFieldByName, isDataGrid, } from "@ac-kit/model-dataset";
2
+ import { fallbackCandidates_ } from "../render/_fallback-chain.js";
3
+ import { RESOLVER_SUPPORT, unsupportedFeatures, } from "../render/capabilities.js";
4
+ import { assertDataReferences, DEFAULT_DATA_SOURCE, resolveDataSource, } from "../spec/data-sources.js";
5
+ import { isFieldEncoding } from "../spec/encoding.js";
6
+ import { applyTransforms } from "../transform/apply-transforms.js";
7
+ import { isSameCoord_, resolveCoord_ } from "./_coords.js";
8
+ import { gridFieldPart_ } from "./_grid-field.js";
9
+ import { panelGuides_ } from "./_guides.js";
10
+ import { checkResolveOptions_, } from "./_options.js";
11
+ import { InvalidEncodingError, InvalidLayerSourceError } from "./errors.js";
12
+ import { resolveScale, scaleChannelOf } from "./scale.js";
13
+ const GRID_MARKS_ = new Set([
14
+ "rect",
15
+ "surface",
16
+ "point",
17
+ "text",
18
+ ]);
19
+ const WGS84_NAMES_ = new Set([
20
+ "EPSG:4326",
21
+ "WGS84",
22
+ "WGS 84",
23
+ "CRS84",
24
+ "OGC:CRS84",
25
+ "URN:OGC:DEF:CRS:OGC:1.3:CRS84",
26
+ "URN:OGC:DEF:CRS:EPSG::4326",
27
+ ]);
28
+ const SCALE_ORDER_ = [
29
+ "x",
30
+ "y",
31
+ "z",
32
+ "xOffset",
33
+ "yOffset",
34
+ "theta",
35
+ "radius",
36
+ "color",
37
+ "fill",
38
+ "stroke",
39
+ "strokeWidth",
40
+ "size",
41
+ "shape",
42
+ "opacity",
43
+ ];
44
+ function intersect_(declared, supported) {
45
+ return declared === undefined
46
+ ? supported
47
+ : new Set([...declared].filter((item) => supported.has(item)));
48
+ }
49
+ function encodingEntries_(spec) {
50
+ // The keys of a `Partial<Record<Channel, …>>` are channels.
51
+ return Object.entries(spec.encoding).filter((entry) => entry[1] !== undefined);
52
+ }
53
+ function withCoord_(spec, inherited) {
54
+ const coord = spec.coord ?? inherited;
55
+ return coord === spec.coord ? spec : { ...spec, coord };
56
+ }
57
+ // Limits of the renderer and of the resolver, known before reading any data.
58
+ function staticReasons_(spec, context) {
59
+ const reasons = unsupportedFeatures(spec, context.capabilities);
60
+ const coord = spec.coord;
61
+ if (coord?.kind === "geographic" &&
62
+ coord.crs !== undefined &&
63
+ !WGS84_NAMES_.has(coord.crs.toUpperCase())) {
64
+ reasons.push(`crs ${coord.crs}`);
65
+ }
66
+ return reasons;
67
+ }
68
+ // The implicit stack of a pie: an arc with theta and no theta2 in polar
69
+ // coordinates stacks theta from 0, its sectors following one another.
70
+ function withImplicitStack_(spec, source) {
71
+ const transforms = spec.transform ?? [];
72
+ const theta = spec.encoding.theta;
73
+ if (spec.mark !== "arc" ||
74
+ spec.coord?.kind !== "polar" ||
75
+ theta === undefined ||
76
+ !isFieldEncoding(theta) ||
77
+ spec.encoding.theta2 !== undefined ||
78
+ isDataGrid(source)) {
79
+ return { spec, transforms };
80
+ }
81
+ const start = `${theta.field}_start`;
82
+ const end = `${theta.field}_end`;
83
+ const title = theta.title ?? fieldTitle(getDataFrameFieldByName(source, theta.field));
84
+ return {
85
+ spec: {
86
+ ...spec,
87
+ encoding: {
88
+ ...spec.encoding,
89
+ theta: { ...theta, field: start, title },
90
+ theta2: { field: end, title },
91
+ },
92
+ },
93
+ transforms: [
94
+ ...transforms,
95
+ {
96
+ kind: "stack",
97
+ field: theta.field,
98
+ groupBy: [],
99
+ offset: "zero",
100
+ as: [start, end],
101
+ },
102
+ ],
103
+ };
104
+ }
105
+ function prepareDraft_(candidate, sourceName, raw, rows, context) {
106
+ if (isDataGrid(raw)) {
107
+ if (!GRID_MARKS_.has(candidate.mark)) {
108
+ throw new InvalidLayerSourceError(sourceName, candidate.mark, `A ${candidate.mark} mark can't read the grid "${sourceName ?? raw.name ?? DEFAULT_DATA_SOURCE}" (rect, surface, point or text can)`);
109
+ }
110
+ if ((candidate.transform ?? []).length > 0) {
111
+ throw new InvalidLayerSourceError(sourceName, candidate.mark, "A layer over a grid takes no transform");
112
+ }
113
+ if (rows !== undefined) {
114
+ throw new InvalidLayerSourceError(sourceName, candidate.mark, "A grid can't be faceted");
115
+ }
116
+ return {
117
+ spec: candidate,
118
+ source: { kind: "grid", grid: raw },
119
+ markCount: raw.columns * raw.rows,
120
+ binned: new Set(),
121
+ stacked: false,
122
+ };
123
+ }
124
+ if (candidate.mark === "surface") {
125
+ throw new InvalidLayerSourceError(sourceName, candidate.mark, "A surface reads a grid, not a frame");
126
+ }
127
+ const { spec, transforms } = withImplicitStack_(candidate, raw);
128
+ const subset = rows === undefined ? raw : dataFrameTakeRows(raw, rows);
129
+ const frame = applyTransforms(transforms, subset, {
130
+ timeZone: context.options.timeZone,
131
+ weekStart: context.options.weekStart,
132
+ maxBins: context.options.maxBins,
133
+ });
134
+ const binned = new Set();
135
+ let stacked = false;
136
+ for (const transform of transforms) {
137
+ if (transform.kind === "bin") {
138
+ const [start, end] = transform.as ?? [
139
+ `${transform.field}_start`,
140
+ `${transform.field}_end`,
141
+ ];
142
+ binned.add(start).add(end);
143
+ }
144
+ else if (transform.kind === "stack") {
145
+ stacked = true;
146
+ }
147
+ }
148
+ return {
149
+ spec,
150
+ source: { kind: "frame", frame },
151
+ markCount: frame.rowCount,
152
+ binned,
153
+ stacked,
154
+ };
155
+ }
156
+ /**
157
+ * The first candidate of `plot`'s fallback chain that the renderer and the
158
+ * resolver can draw over every subset of its source (`subsetsOf`); the reasons
159
+ * of each candidate skipped. Spec and data errors throw.
160
+ */
161
+ function chooseCandidate_(plot, inherited, context, subsetsOf) {
162
+ const skipped = [];
163
+ const maxMarks = context.capabilities.maxMarks;
164
+ for (const candidate of fallbackCandidates_(plot)) {
165
+ const effective = withCoord_(candidate, inherited);
166
+ let reasons = staticReasons_(effective, context);
167
+ if (reasons.length === 0) {
168
+ const raw = resolveDataSource(effective.data, context.sources);
169
+ const drafts = subsetsOf(raw).map((rows) => rows === null
170
+ ? undefined
171
+ : prepareDraft_(effective, effective.data, raw, rows, context));
172
+ const over = drafts.find((draft) => draft !== undefined &&
173
+ maxMarks !== undefined &&
174
+ draft.markCount > maxMarks);
175
+ if (over === undefined) {
176
+ return { spec: effective, skipped, sourceName: effective.data, drafts };
177
+ }
178
+ reasons = [`mark count ${over.markCount} (limit ${maxMarks})`];
179
+ }
180
+ skipped.push({ spec: effective, reasons });
181
+ }
182
+ return { failed: skipped };
183
+ }
184
+ function uniqueReasons_(skipped) {
185
+ return [...new Set(skipped.flatMap((item) => item.reasons))];
186
+ }
187
+ function tableOf_(plot, extraFields, reasons, context) {
188
+ const raw = resolveDataSource(plot.data, context.sources);
189
+ const names = new Set(extraFields);
190
+ for (const [channel, encoding] of encodingEntries_(plot)) {
191
+ if (!isFieldEncoding(encoding)) {
192
+ continue;
193
+ }
194
+ if (isDataGrid(raw)) {
195
+ const { field } = gridFieldPart_(raw, channel, encoding.field);
196
+ names.add(field.name);
197
+ }
198
+ else {
199
+ names.add(encoding.field);
200
+ }
201
+ }
202
+ const frame = isDataGrid(raw) ? dataFrameFromDataGrid(raw) : raw;
203
+ return {
204
+ sourceName: plot.data,
205
+ frame: names.size === 0 ? frame : dataFrameSelectFields(frame, [...names]),
206
+ reasons,
207
+ description: plot.description,
208
+ };
209
+ }
210
+ function scaleInputs_(drafts, into) {
211
+ for (const draft of drafts) {
212
+ const source = draft.source.kind === "frame" ? draft.source.frame : draft.source.grid;
213
+ for (const [channel, encoding] of encodingEntries_(draft.spec)) {
214
+ const scaleChannel = scaleChannelOf(channel);
215
+ if (scaleChannel === undefined || !isFieldEncoding(encoding)) {
216
+ continue;
217
+ }
218
+ let inputs = into.get(scaleChannel);
219
+ if (inputs === undefined) {
220
+ inputs = [];
221
+ into.set(scaleChannel, inputs);
222
+ }
223
+ inputs.push({
224
+ channel,
225
+ encoding,
226
+ mark: draft.spec.mark,
227
+ source,
228
+ binned: draft.binned.has(encoding.field),
229
+ stacked: draft.stacked,
230
+ offset: (scaleChannel === "x" || scaleChannel === "y") &&
231
+ draft.spec.encoding[`${scaleChannel}Offset`] !== undefined,
232
+ });
233
+ }
234
+ }
235
+ }
236
+ // Annotation values join continuous domains: a line or band on its channel, a
237
+ // point or text on both x and y.
238
+ function annotationIncludes_(annotations, into) {
239
+ const add = (channel, value) => {
240
+ if (typeof value !== "number") {
241
+ return;
242
+ }
243
+ let values = into.get(channel);
244
+ if (values === undefined) {
245
+ values = [];
246
+ into.set(channel, values);
247
+ }
248
+ values.push(value);
249
+ };
250
+ for (const annotation of annotations) {
251
+ switch (annotation.kind) {
252
+ case "line":
253
+ add(annotation.channel, annotation.at);
254
+ break;
255
+ case "band":
256
+ add(annotation.channel, annotation.from);
257
+ add(annotation.channel, annotation.to);
258
+ break;
259
+ default:
260
+ add("x", annotation.x);
261
+ add("y", annotation.y);
262
+ }
263
+ }
264
+ }
265
+ // The planar coordinates of a geometry, pushed as x then y extents.
266
+ function geometryExtent_(geometry, extent) {
267
+ const position = (point) => {
268
+ const [x, y] = point;
269
+ if (Number.isFinite(x) && Number.isFinite(y)) {
270
+ extent[0] = Math.min(extent[0], x);
271
+ extent[1] = Math.max(extent[1], x);
272
+ extent[2] = Math.min(extent[2], y);
273
+ extent[3] = Math.max(extent[3], y);
274
+ }
275
+ };
276
+ switch (geometry.type) {
277
+ case "Point":
278
+ position(geometry.coordinates);
279
+ break;
280
+ case "MultiPoint":
281
+ case "LineString":
282
+ for (const point of geometry.coordinates) {
283
+ position(point);
284
+ }
285
+ break;
286
+ case "MultiLineString":
287
+ case "Polygon":
288
+ for (const line of geometry.coordinates) {
289
+ for (const point of line) {
290
+ position(point);
291
+ }
292
+ }
293
+ break;
294
+ case "MultiPolygon":
295
+ for (const polygon of geometry.coordinates) {
296
+ for (const ring of polygon) {
297
+ for (const point of ring) {
298
+ position(point);
299
+ }
300
+ }
301
+ }
302
+ break;
303
+ case "GeometryCollection":
304
+ for (const child of geometry.geometries) {
305
+ geometryExtent_(child, extent);
306
+ }
307
+ break;
308
+ }
309
+ }
310
+ // A `geometry` field in cartesian coordinates is planar: its coordinates join
311
+ // the continuous x and y domains, so a shape layered with other marks is whole.
312
+ function geometryIncludes_(draft, into) {
313
+ const encoding = draft.spec.encoding.geometry;
314
+ if (encoding === undefined ||
315
+ !isFieldEncoding(encoding) ||
316
+ (draft.spec.coord !== undefined && draft.spec.coord.kind !== "cartesian") ||
317
+ draft.source.kind !== "frame") {
318
+ return;
319
+ }
320
+ const column = getDataFrameColumnByName(draft.source.frame, encoding.field);
321
+ const extent = [
322
+ Number.POSITIVE_INFINITY,
323
+ Number.NEGATIVE_INFINITY,
324
+ Number.POSITIVE_INFINITY,
325
+ Number.NEGATIVE_INFINITY,
326
+ ];
327
+ for (let row = 0; row < column.length; row++) {
328
+ const value = column[row];
329
+ if (typeof value === "object" && value !== null) {
330
+ geometryExtent_(value, extent);
331
+ }
332
+ }
333
+ if (extent[0] > extent[1]) {
334
+ return;
335
+ }
336
+ for (const [channel, low, high] of [
337
+ ["x", extent[0], extent[1]],
338
+ ["y", extent[2], extent[3]],
339
+ ]) {
340
+ let values = into.get(channel);
341
+ if (values === undefined) {
342
+ values = [];
343
+ into.set(channel, values);
344
+ }
345
+ values.push(low, high);
346
+ }
347
+ }
348
+ function resolveScales_(drafts, context) {
349
+ const inputs = new Map();
350
+ scaleInputs_(drafts, inputs);
351
+ const includes = new Map();
352
+ for (const draft of drafts) {
353
+ annotationIncludes_(draft.spec.annotations ?? [], includes);
354
+ geometryIncludes_(draft, includes);
355
+ }
356
+ const scales = new Map();
357
+ for (const channel of SCALE_ORDER_) {
358
+ const channelInputs = inputs.get(channel);
359
+ if (channelInputs === undefined) {
360
+ continue;
361
+ }
362
+ const scale = resolveScale(channelInputs, {
363
+ ...context.options,
364
+ include: includes.get(channel),
365
+ });
366
+ if (scale !== undefined) {
367
+ scales.set(channel, scale);
368
+ }
369
+ }
370
+ return scales;
371
+ }
372
+ function resolveChannels_(draft, scales) {
373
+ const channels = new Map();
374
+ for (const [channel, encoding] of encodingEntries_(draft.spec)) {
375
+ if (!isFieldEncoding(encoding)) {
376
+ channels.set(channel, { kind: "value", channel, value: encoding.value });
377
+ continue;
378
+ }
379
+ const scaleChannel = scaleChannelOf(channel);
380
+ const scale = scaleChannel === undefined ? undefined : scales.get(scaleChannel);
381
+ if (draft.source.kind === "grid") {
382
+ const { part, field } = gridFieldPart_(draft.source.grid, channel, encoding.field);
383
+ channels.set(channel, {
384
+ kind: "lattice",
385
+ channel,
386
+ encoding,
387
+ field,
388
+ axis: part,
389
+ scale,
390
+ });
391
+ }
392
+ else {
393
+ channels.set(channel, {
394
+ kind: "field",
395
+ channel,
396
+ encoding,
397
+ field: getDataFrameFieldByName(draft.source.frame, encoding.field),
398
+ column: getDataFrameColumnByName(draft.source.frame, encoding.field),
399
+ scale,
400
+ });
401
+ }
402
+ }
403
+ return channels;
404
+ }
405
+ function buildPanel_(layers, scales, meta, context) {
406
+ const resolvedLayers = layers.map(({ chosen, draft }) => ({
407
+ spec: draft.spec,
408
+ skipped: chosen.skipped,
409
+ sourceName: chosen.sourceName,
410
+ source: draft.source,
411
+ markCount: draft.markCount,
412
+ channels: resolveChannels_(draft, scales),
413
+ }));
414
+ const coord = resolveCoord_(layers[0]?.draft.spec.coord, resolvedLayers, context.sources, context.options);
415
+ return {
416
+ ...meta,
417
+ coord,
418
+ layers: resolvedLayers,
419
+ scales,
420
+ guides: panelGuides_(resolvedLayers.map((layer) => layer.spec), scales),
421
+ };
422
+ }
423
+ const FACET_CHANNELS_ = ["row", "column"];
424
+ function hasFacetEncoding_(plot) {
425
+ return FACET_CHANNELS_.some((channel) => plot.encoding[channel] !== undefined);
426
+ }
427
+ function resolveLayers_(plots, coord, meta, context) {
428
+ for (const plot of plots) {
429
+ if (plots.length > 1 && hasFacetEncoding_(plot)) {
430
+ throw new InvalidEncodingError(plot.encoding.row === undefined ? "column" : "row", "A layer of a composition can't facet by encoding; facet a single plot");
431
+ }
432
+ }
433
+ const chosen = plots.map((plot) => chooseCandidate_(plot, coord, context, () => [undefined]));
434
+ const picked = chosen.filter((item) => !("failed" in item));
435
+ let reasons = [
436
+ ...new Set(chosen.flatMap((item) => "failed" in item ? uniqueReasons_(item.failed) : [])),
437
+ ];
438
+ if (reasons.length === 0) {
439
+ const firstCoord = picked[0]?.spec.coord;
440
+ if (picked.some((item) => !isSameCoord_(item.spec.coord, firstCoord))) {
441
+ reasons = ["layers drawn in different coordinate systems"];
442
+ }
443
+ }
444
+ if (reasons.length > 0) {
445
+ return {
446
+ declined: true,
447
+ reasons,
448
+ tables: plots.map((plot, index) => {
449
+ const item = chosen[index];
450
+ return tableOf_(plot, [], "failed" in item ? uniqueReasons_(item.failed) : reasons, context);
451
+ }),
452
+ };
453
+ }
454
+ const layers = picked.map((item) => ({
455
+ chosen: item,
456
+ draft: item.drafts[0],
457
+ }));
458
+ const scales = resolveScales_(layers.map((layer) => layer.draft), context);
459
+ return {
460
+ kind: "panel",
461
+ panel: buildPanel_(layers, scales, {
462
+ ...meta,
463
+ aspectRatio: plots.find((plot) => plot.aspectRatio !== undefined)
464
+ ?.aspectRatio,
465
+ row: undefined,
466
+ column: undefined,
467
+ }, context),
468
+ };
469
+ }
470
+ function facetAxis_(frame, name) {
471
+ const field = getDataFrameFieldByName(frame, name);
472
+ const column = getDataFrameColumnByName(frame, name);
473
+ const distinct = new Map();
474
+ for (let row = 0; row < frame.rowCount; row++) {
475
+ const value = column[row] ?? null;
476
+ if (!distinct.has(value)) {
477
+ distinct.set(value, value);
478
+ }
479
+ }
480
+ let values = [...distinct.values()].sort(compareValues);
481
+ if (field.kind === "ordinal" && field.domain !== undefined) {
482
+ const declared = field.domain.filter((value) => distinct.has(value));
483
+ const known = new Set(declared);
484
+ values = [...declared, ...values.filter((value) => !known.has(value))];
485
+ }
486
+ const index = new Map(values.map((value, at) => [value, at]));
487
+ return {
488
+ axis: { field, values, title: fieldTitle(field) },
489
+ indexOf: (row) => index.get(column[row] ?? null),
490
+ };
491
+ }
492
+ function partition_(raw, facet, sourceName) {
493
+ if (isDataGrid(raw)) {
494
+ throw new InvalidLayerSourceError(sourceName, facet.plot.mark, "A grid can't be faceted");
495
+ }
496
+ const row = facet.row === undefined ? undefined : facetAxis_(raw, facet.row);
497
+ const column = facet.column === undefined ? undefined : facetAxis_(raw, facet.column);
498
+ const columnCount = column?.axis.values.length ?? 1;
499
+ const cellCount = (row?.axis.values.length ?? 1) * columnCount;
500
+ const lists = Array.from({ length: cellCount }, () => []);
501
+ for (let index = 0; index < raw.rowCount; index++) {
502
+ const cell = (row?.indexOf(index) ?? 0) * columnCount + (column?.indexOf(index) ?? 0);
503
+ lists[cell].push(index);
504
+ }
505
+ return {
506
+ row: row?.axis,
507
+ column: column?.axis,
508
+ cells: lists.map((rows) => rows.length === 0 ? null : Int32Array.from(rows)),
509
+ };
510
+ }
511
+ function resolveFacet_(facet, context) {
512
+ let partition;
513
+ const chosen = chooseCandidate_(facet.plot, undefined, context, (raw) => {
514
+ partition = partition_(raw, facet, facet.plot.data);
515
+ return partition.cells;
516
+ });
517
+ const facetFields = [facet.row, facet.column].filter((name) => name !== undefined);
518
+ if ("failed" in chosen) {
519
+ const reasons = uniqueReasons_(chosen.failed);
520
+ return {
521
+ declined: true,
522
+ reasons,
523
+ tables: [tableOf_(facet.plot, facetFields, reasons, context)],
524
+ };
525
+ }
526
+ const cells = partition; // set by the subsets callback of the chosen candidate
527
+ const drafts = chosen.drafts;
528
+ const shared = facet.sharedScales ?? true;
529
+ const sharedScales = shared
530
+ ? resolveScales_(drafts.filter((draft) => draft !== undefined), context)
531
+ : undefined;
532
+ const columnCount = cells.column?.values.length ?? 1;
533
+ const panels = drafts.map((draft, index) => {
534
+ if (draft === undefined) {
535
+ return undefined;
536
+ }
537
+ return buildPanel_([{ chosen, draft }], sharedScales ?? resolveScales_([draft], context), {
538
+ title: undefined,
539
+ description: undefined,
540
+ aspectRatio: facet.plot.aspectRatio,
541
+ row: cells.row === undefined
542
+ ? undefined
543
+ : cells.row.values[Math.floor(index / columnCount)],
544
+ column: cells.column === undefined
545
+ ? undefined
546
+ : cells.column.values[index % columnCount],
547
+ }, context);
548
+ });
549
+ return {
550
+ kind: "facet",
551
+ row: cells.row,
552
+ column: cells.column,
553
+ cells: panels,
554
+ sharedScales: shared,
555
+ };
556
+ }
557
+ // A plot faceted by its row or column encoding is a facet of the plot without
558
+ // them, sharing its scales (Vega-Lite's facet channels).
559
+ function facetOfEncoding_(plot) {
560
+ if (!hasFacetEncoding_(plot)) {
561
+ return undefined;
562
+ }
563
+ const { row, column, ...encoding } = plot.encoding;
564
+ const fieldOf = (spec, channel) => {
565
+ if (spec !== undefined && !isFieldEncoding(spec)) {
566
+ throw new InvalidEncodingError(channel, `${channel} facets by a field, not a value`);
567
+ }
568
+ return spec?.field;
569
+ };
570
+ const rowField = fieldOf(row, "row");
571
+ const columnField = fieldOf(column, "column");
572
+ return {
573
+ kind: "facet",
574
+ ...(rowField === undefined ? {} : { row: rowField }),
575
+ ...(columnField === undefined ? {} : { column: columnField }),
576
+ plot: { ...plot, encoding },
577
+ };
578
+ }
579
+ function resolveNode_(spec, context) {
580
+ if (!("kind" in spec)) {
581
+ return resolvePlotNode_(spec, context);
582
+ }
583
+ switch (spec.kind) {
584
+ case "plot": {
585
+ const { kind: _kind, ...plot } = spec;
586
+ return resolvePlotNode_(plot, context);
587
+ }
588
+ case "layer":
589
+ return resolveLayers_(spec.layers, spec.coord, { title: spec.title, description: spec.description }, context);
590
+ case "facet":
591
+ return resolveFacet_(spec, context);
592
+ case "concat":
593
+ return resolveConcat_(spec, context);
594
+ }
595
+ }
596
+ function resolvePlotNode_(plot, context) {
597
+ const facet = facetOfEncoding_(plot);
598
+ return facet === undefined
599
+ ? resolveLayers_([plot], undefined, { title: plot.title, description: plot.description }, context)
600
+ : resolveFacet_(facet, context);
601
+ }
602
+ function resolveConcat_(spec, context) {
603
+ return {
604
+ kind: "concat",
605
+ direction: spec.direction,
606
+ title: spec.title,
607
+ description: spec.description,
608
+ children: spec.views.map((child) => {
609
+ const node = resolveNode_(child, context);
610
+ return "declined" in node ? { kind: "table", tables: node.tables } : node;
611
+ }),
612
+ };
613
+ }
614
+ function collectPanels_(node, into) {
615
+ switch (node.kind) {
616
+ case "panel":
617
+ into.push(node.panel);
618
+ return;
619
+ case "facet":
620
+ for (const cell of node.cells) {
621
+ if (cell !== undefined) {
622
+ into.push(cell);
623
+ }
624
+ }
625
+ return;
626
+ case "concat":
627
+ for (const child of node.children) {
628
+ collectPanels_(child, into);
629
+ }
630
+ return;
631
+ case "table":
632
+ return;
633
+ }
634
+ }
635
+ function rootMeta_(spec) {
636
+ if ("kind" in spec && spec.kind === "facet") {
637
+ return { title: spec.plot.title, description: spec.plot.description };
638
+ }
639
+ return { title: spec.title, description: spec.description };
640
+ }
641
+ /**
642
+ * Resolves a view for a renderer: every plot through its fallback chain against
643
+ * `capabilities` ∩ {@link RESOLVER_SUPPORT}, transforms applied, scales shared
644
+ * across a layer composition and, with `sharedScales` (the default), across a
645
+ * facet's cells, facets partitioned (ascending, or an ordinal field's declared
646
+ * order), guides merged and coordinates resolved.
647
+ *
648
+ * - `data`: a frame is the single source (named by its `name`, else `"default"`);
649
+ * otherwise layers read the source they name.
650
+ * - Each candidate of a chain is checked against the capabilities (mark,
651
+ * coordinate system, channels, transforms, dimension, projection, layout), a
652
+ * `crs` other than WGS 84 (`crs X`) and, after its transforms, `maxMarks`
653
+ * (`mark count N (limit M)`); the first that passes is drawn, the others are
654
+ * listed in `skipped`. A `surface` gets an implicit last `rect` candidate.
655
+ * - A layer inherits the enclosing composition's `coord` before it is checked.
656
+ * - A plot encoding `row` or `column` is a facet of it by those fields.
657
+ * - Annotations widen continuous domains: a line or band on its channel, a point
658
+ * or text on x and y.
659
+ * - An `arc` with `theta` and no `theta2` in polar coordinates stacks `theta`
660
+ * from 0 (the pie).
661
+ * - A plot, a layer composition or a facet with nothing drawable declines whole:
662
+ * at the root the result is `kind: "table"`; inside a concatenation it
663
+ * becomes a `table` node. Renderer and resolver limits decline; spec and data
664
+ * errors throw.
665
+ *
666
+ * Synchronous: O(spec nodes + Σ rows × (encoded channels + transforms)), plus
667
+ * the sorts of discrete domains, quantiles and transforms; facets copy their
668
+ * partitions.
669
+ *
670
+ * @throws {UnknownDataSourceError} {InvalidNodeSourceError} {UnknownFieldError}
671
+ * {InvalidScaleError} {InvalidTransformError} {InvalidEncodingError}
672
+ * {InvalidCoordError} {InvalidLayerSourceError} {InvalidHierarchyError}
673
+ * {InvalidNetworkError}
674
+ * @throws {RangeError} Invalid `options`.
675
+ */
676
+ export function resolveView(spec, data, capabilities, options = {}) {
677
+ const checked = checkResolveOptions_(options);
678
+ const sources = "rowCount" in data
679
+ ? new Map([[data.name ?? DEFAULT_DATA_SOURCE, data]])
680
+ : data;
681
+ assertDataReferences(spec, sources);
682
+ const context = {
683
+ sources,
684
+ options: checked,
685
+ capabilities: {
686
+ ...capabilities,
687
+ transforms: intersect_(capabilities.transforms, RESOLVER_SUPPORT.transforms),
688
+ projections: intersect_(capabilities.projections, RESOLVER_SUPPORT.projections),
689
+ layouts: intersect_(capabilities.layouts, RESOLVER_SUPPORT.layouts),
690
+ },
691
+ };
692
+ const root = resolveNode_(spec, context);
693
+ const meta = rootMeta_(spec);
694
+ if ("declined" in root) {
695
+ return {
696
+ kind: "table",
697
+ reasons: root.reasons,
698
+ tables: root.tables,
699
+ ...meta,
700
+ };
701
+ }
702
+ const panels = [];
703
+ collectPanels_(root, panels);
704
+ return {
705
+ kind: "chart",
706
+ view: { ...meta, timeZone: checked.timeZone, root, panels },
707
+ };
708
+ }
709
+ //# sourceMappingURL=view.js.map