-
Notifications
You must be signed in to change notification settings - Fork 591
/
model.ts
708 lines (585 loc) · 21.6 KB
/
model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
import {
AnchorValue,
Axis as VgAxis,
Legend as VgLegend,
NewSignal,
Projection as VgProjection,
Signal,
SignalRef,
Title as VgTitle
} from 'vega';
import {
Channel,
ExtendedChannel,
FACET_CHANNELS,
getPositionScaleChannel,
isChannel,
isScaleChannel,
ScaleChannel,
SingleDefChannel
} from '../channel';
import {ChannelDef, FieldDef, FieldRefOption, getFieldDef, vgField} from '../channeldef';
import {Config} from '../config';
import {Data, DataSourceType} from '../data';
import {forEach, reduce} from '../encoding';
import {ExprRef} from '../expr';
import * as log from '../log';
import {Resolve} from '../resolve';
import {hasDiscreteDomain} from '../scale';
import {isFacetSpec} from '../spec';
import {
extractCompositionLayout,
GenericCompositionLayoutWithColumns,
LayoutSizeMixins,
SpecType,
ViewBackground
} from '../spec/base';
import {NormalizedSpec} from '../spec/index';
import {extractTitleConfig, isText, TitleParams} from '../title';
import {normalizeTransform, Transform} from '../transform';
import {contains, Dict, duplicate, isEmpty, keys, varName} from '../util';
import {isVgRangeStep, SubstituteType, VgData, VgEncodeEntry, VgLayout, VgMarkGroup} from '../vega.schema';
import {assembleAxes} from './axis/assemble';
import {AxisComponentIndex} from './axis/component';
import {signalOrValueRef} from './common';
import {ConcatModel} from './concat';
import {DataComponent} from './data';
import {FacetModel} from './facet';
import {assembleHeaderGroups, assembleLayoutTitleBand, assembleTitleGroup} from './header/assemble';
import {HEADER_CHANNELS, LayoutHeaderComponent} from './header/component';
import {LayerModel} from './layer';
import {sizeExpr} from './layoutsize/assemble';
import {
getSizeTypeFromLayoutSizeType,
LayoutSizeComponent,
LayoutSizeIndex,
LayoutSizeType
} from './layoutsize/component';
import {assembleLegends} from './legend/assemble';
import {LegendComponentIndex} from './legend/component';
import {parseLegend} from './legend/parse';
import {assembleProjections} from './projection/assemble';
import {ProjectionComponent} from './projection/component';
import {parseProjection} from './projection/parse';
import {assembleScales} from './scale/assemble';
import {ScaleComponent, ScaleComponentIndex} from './scale/component';
import {assembleDomain, getFieldFromDomain} from './scale/domain';
import {parseScales} from './scale/parse';
import {SelectionComponent} from './selection';
import {Split} from './split';
import {UnitModel} from './unit';
/**
* Composable Components that are intermediate results of the parsing phase of the
* compilations. The components represents parts of the specification in a form that
* can be easily merged (during parsing for composite specs).
* In addition, these components are easily transformed into Vega specifications
* during the "assemble" phase, which is the last phase of the compilation step.
*/
export interface Component {
data: DataComponent;
layoutSize: LayoutSizeComponent;
layoutHeaders: {
row?: LayoutHeaderComponent;
column?: LayoutHeaderComponent;
facet?: LayoutHeaderComponent;
};
mark: VgMarkGroup[];
scales: ScaleComponentIndex;
projection: ProjectionComponent;
selection: Dict<SelectionComponent>;
/** Dictionary mapping channel to VgAxis definition */
axes: AxisComponentIndex;
/** Dictionary mapping channel to VgLegend definition */
legends: LegendComponentIndex;
resolve: Resolve;
}
export interface NameMapInterface {
rename(oldname: string, newName: string): void;
has(name: string): boolean;
get(name: string): string;
}
export class NameMap implements NameMapInterface {
private nameMap: Dict<string>;
constructor() {
this.nameMap = {};
}
public rename(oldName: string, newName: string) {
this.nameMap[oldName] = newName;
}
public has(name: string): boolean {
return this.nameMap[name] !== undefined;
}
public get(name: string): string {
// If the name appears in the _nameMap, we need to read its new name.
// We have to loop over the dict just in case the new name also gets renamed.
while (this.nameMap[name] && name !== this.nameMap[name]) {
name = this.nameMap[name];
}
return name;
}
}
/*
We use type guards instead of `instanceof` as `instanceof` makes
different parts of the compiler depend on the actual implementation of
the model classes, which in turn depend on different parts of the compiler.
Thus, `instanceof` leads to circular dependency problems.
On the other hand, type guards only make different parts of the compiler
depend on the type of the model classes, but not the actual implementation.
*/
export function isUnitModel(model: Model): model is UnitModel {
return model?.type === 'unit';
}
export function isFacetModel(model: Model): model is FacetModel {
return model?.type === 'facet';
}
export function isConcatModel(model: Model): model is ConcatModel {
return model?.type === 'concat';
}
export function isLayerModel(model: Model): model is LayerModel {
return model?.type === 'layer';
}
export abstract class Model {
public readonly name: string;
public size: LayoutSizeMixins;
public readonly title: TitleParams<SignalRef>;
public readonly description: string;
public readonly data: Data | null;
public readonly transforms: Transform[];
public readonly layout: GenericCompositionLayoutWithColumns;
/** Name map for scales, which can be renamed by a model's parent. */
protected scaleNameMap: NameMapInterface;
/** Name map for projections, which can be renamed by a model's parent. */
protected projectionNameMap: NameMapInterface;
/** Name map for signals, which can be renamed by a model's parent. */
protected signalNameMap: NameMapInterface;
public readonly component: Component;
public readonly view?: ViewBackground<SignalRef>;
public abstract readonly children: Model[];
constructor(
spec: SubstituteType<NormalizedSpec, ExprRef, SignalRef>,
public readonly type: SpecType,
public readonly parent: Model,
parentGivenName: string,
public readonly config: Config<SignalRef>,
resolve: Resolve,
view?: ViewBackground<SignalRef>
) {
this.parent = parent;
this.config = config;
this.view = view;
// If name is not provided, always use parent's givenName to avoid name conflicts.
this.name = spec.name ?? parentGivenName;
this.title = isText(spec.title) ? {text: spec.title} : spec.title;
// Shared name maps
this.scaleNameMap = parent ? parent.scaleNameMap : new NameMap();
this.projectionNameMap = parent ? parent.projectionNameMap : new NameMap();
this.signalNameMap = parent ? parent.signalNameMap : new NameMap();
this.data = spec.data;
this.description = spec.description;
this.transforms = normalizeTransform(spec.transform ?? []);
this.layout = type === 'layer' || type === 'unit' ? {} : extractCompositionLayout(spec, type, config);
this.component = {
data: {
sources: parent ? parent.component.data.sources : [],
outputNodes: parent ? parent.component.data.outputNodes : {},
outputNodeRefCounts: parent ? parent.component.data.outputNodeRefCounts : {},
// data is faceted if the spec is a facet spec or the parent has faceted data and data is undefined
isFaceted: isFacetSpec(spec) || (parent?.component.data.isFaceted && spec.data === undefined)
},
layoutSize: new Split<LayoutSizeIndex>(),
layoutHeaders: {row: {}, column: {}, facet: {}},
mark: null,
resolve: {
scale: {},
axis: {},
legend: {},
...(resolve ? duplicate(resolve) : {})
},
selection: null,
scales: null,
projection: null,
axes: {},
legends: {}
};
}
public get width(): SignalRef {
return this.getSizeSignalRef('width');
}
public get height(): SignalRef {
return this.getSizeSignalRef('height');
}
public parse() {
this.parseScale();
this.parseLayoutSize(); // depends on scale
this.renameTopLevelLayoutSizeSignal();
this.parseSelections();
this.parseProjection();
this.parseData(); // (pathorder) depends on markDef; selection filters depend on parsed selections; depends on projection because some transforms require the finalized projection name.
this.parseAxesAndHeaders(); // depends on scale and layout size
this.parseLegends(); // depends on scale, markDef
this.parseMarkGroup(); // depends on data name, scale, layout size, axisGroup, and children's scale, axis, legend and mark.
}
public abstract parseData(): void;
public abstract parseSelections(): void;
public parseScale() {
parseScales(this);
}
public parseProjection() {
parseProjection(this);
}
public abstract parseLayoutSize(): void;
/**
* Rename top-level spec's size to be just width / height, ignoring model name.
* This essentially merges the top-level spec's width/height signals with the width/height signals
* to help us reduce redundant signals declaration.
*/
private renameTopLevelLayoutSizeSignal() {
if (this.getName('width') !== 'width') {
this.renameSignal(this.getName('width'), 'width');
}
if (this.getName('height') !== 'height') {
this.renameSignal(this.getName('height'), 'height');
}
}
public abstract parseMarkGroup(): void;
public abstract parseAxesAndHeaders(): void;
public parseLegends() {
parseLegend(this);
}
public abstract assembleSelectionTopLevelSignals(signals: NewSignal[]): NewSignal[];
public abstract assembleSignals(): NewSignal[];
public abstract assembleSelectionData(data: readonly VgData[]): readonly VgData[];
public abstract assembleGroupStyle(): string | string[];
private assembleEncodeFromView(view: ViewBackground<SignalRef>): VgEncodeEntry {
// Exclude "style"
const {style: _, ...baseView} = view;
const e: VgEncodeEntry = {};
for (const property of keys(baseView)) {
const value = baseView[property];
if (value !== undefined) {
e[property] = signalOrValueRef(value);
}
}
return e;
}
public assembleGroupEncodeEntry(isTopLevel: boolean): VgEncodeEntry {
let encodeEntry: VgEncodeEntry = {};
if (this.view) {
encodeEntry = this.assembleEncodeFromView(this.view);
}
if (!isTopLevel) {
// Descriptions are already added to the top-level description so we only need to add them to the inner views.
if (this.description) {
encodeEntry['description'] = signalOrValueRef(this.description);
}
// For top-level spec, we can set the global width and height signal to adjust the group size.
// For other child specs, we have to manually set width and height in the encode entry.
if (this.type === 'unit' || this.type === 'layer') {
return {
width: this.getSizeSignalRef('width'),
height: this.getSizeSignalRef('height'),
...encodeEntry
};
}
}
return isEmpty(encodeEntry) ? undefined : encodeEntry;
}
public assembleLayout(): VgLayout {
if (!this.layout) {
return undefined;
}
const {spacing, ...layout} = this.layout;
const {component, config} = this;
const titleBand = assembleLayoutTitleBand(component.layoutHeaders, config);
return {
padding: spacing,
...this.assembleDefaultLayout(),
...layout,
...(titleBand ? {titleBand} : {})
};
}
protected assembleDefaultLayout(): VgLayout {
return {};
}
public abstract assembleLayoutSignals(): NewSignal[];
public assembleHeaderMarks(): VgMarkGroup[] {
const {layoutHeaders} = this.component;
let headerMarks = [];
for (const channel of FACET_CHANNELS) {
if (layoutHeaders[channel].title) {
headerMarks.push(assembleTitleGroup(this, channel));
}
}
for (const channel of HEADER_CHANNELS) {
headerMarks = headerMarks.concat(assembleHeaderGroups(this, channel));
}
return headerMarks;
}
public abstract assembleMarks(): VgMarkGroup[];
public assembleAxes(): VgAxis[] {
return assembleAxes(this.component.axes, this.config);
}
public assembleLegends(): VgLegend[] {
return assembleLegends(this);
}
public assembleProjections(): VgProjection[] {
return assembleProjections(this);
}
public assembleTitle(): VgTitle {
const {encoding, ...titleNoEncoding} = this.title ?? ({} as TitleParams<SignalRef>);
const title: VgTitle = {
...extractTitleConfig(this.config.title).nonMarkTitleProperties,
...titleNoEncoding,
...(encoding ? {encode: {update: encoding}} : {})
};
if (title.text) {
if (contains(['unit', 'layer'], this.type)) {
// Unit/Layer
if (contains<AnchorValue>(['middle', undefined], title.anchor)) {
title.frame ??= 'group';
}
} else {
// composition with Vega layout
// Set title = "start" by default for composition as "middle" does not look nice
// https://github.com/vega/vega/issues/960#issuecomment-471360328
title.anchor ??= 'start';
}
return isEmpty(title) ? undefined : title;
}
return undefined;
}
/**
* Assemble the mark group for this model. We accept optional `signals` so that we can include concat top-level signals with the top-level model's local signals.
*/
public assembleGroup(signals: Signal[] = []) {
const group: VgMarkGroup = {};
signals = signals.concat(this.assembleSignals());
if (signals.length > 0) {
group.signals = signals;
}
const layout = this.assembleLayout();
if (layout) {
group.layout = layout;
}
group.marks = [].concat(this.assembleHeaderMarks(), this.assembleMarks());
// Only include scales if this spec is top-level or if parent is facet.
// (Otherwise, it will be merged with upper-level's scope.)
const scales = !this.parent || isFacetModel(this.parent) ? assembleScales(this) : [];
if (scales.length > 0) {
group.scales = scales;
}
const axes = this.assembleAxes();
if (axes.length > 0) {
group.axes = axes;
}
const legends = this.assembleLegends();
if (legends.length > 0) {
group.legends = legends;
}
return group;
}
public getName(text: string) {
return varName((this.name ? `${this.name}_` : '') + text);
}
public getDataName(type: DataSourceType) {
return this.getName(DataSourceType[type].toLowerCase());
}
/**
* Request a data source name for the given data source type and mark that data source as required.
* This method should be called in parse, so that all used data source can be correctly instantiated in assembleData().
* You can lookup the correct dataset name in assemble with `lookupDataSource`.
*/
public requestDataName(name: DataSourceType) {
const fullName = this.getDataName(name);
// Increase ref count. This is critical because otherwise we won't create a data source.
// We also increase the ref counts on OutputNode.getSource() calls.
const refCounts = this.component.data.outputNodeRefCounts;
refCounts[fullName] = (refCounts[fullName] || 0) + 1;
return fullName;
}
public getSizeSignalRef(layoutSizeType: LayoutSizeType): SignalRef {
if (isFacetModel(this.parent)) {
const sizeType = getSizeTypeFromLayoutSizeType(layoutSizeType);
const channel = getPositionScaleChannel(sizeType);
const scaleComponent = this.component.scales[channel];
if (scaleComponent && !scaleComponent.merged) {
// independent scale
const type = scaleComponent.get('type');
const range = scaleComponent.get('range');
if (hasDiscreteDomain(type) && isVgRangeStep(range)) {
const scaleName = scaleComponent.get('name');
const domain = assembleDomain(this, channel);
const field = getFieldFromDomain(domain);
if (field) {
const fieldRef = vgField({aggregate: 'distinct', field}, {expr: 'datum'});
return {
signal: sizeExpr(scaleName, scaleComponent, fieldRef)
};
} else {
log.warn(log.message.unknownField(channel));
return null;
}
}
}
}
return {
signal: this.signalNameMap.get(this.getName(layoutSizeType))
};
}
/**
* Lookup the name of the datasource for an output node. You probably want to call this in assemble.
*/
public lookupDataSource(name: string) {
const node = this.component.data.outputNodes[name];
if (!node) {
// Name not found in map so let's just return what we got.
// This can happen if we already have the correct name.
return name;
}
return node.getSource();
}
public getSignalName(oldSignalName: string): string {
return this.signalNameMap.get(oldSignalName);
}
public renameSignal(oldName: string, newName: string) {
this.signalNameMap.rename(oldName, newName);
}
public renameScale(oldName: string, newName: string) {
this.scaleNameMap.rename(oldName, newName);
}
public renameProjection(oldName: string, newName: string) {
this.projectionNameMap.rename(oldName, newName);
}
/**
* @return scale name for a given channel after the scale has been parsed and named.
*/
public scaleName(originalScaleName: ScaleChannel | string, parse?: boolean): string {
if (parse) {
// During the parse phase always return a value
// No need to refer to rename map because a scale can't be renamed
// before it has the original name.
return this.getName(originalScaleName);
}
// If there is a scale for the channel, it should either
// be in the scale component or exist in the name map
if (
// If there is a scale for the channel, there should be a local scale component for it
(isChannel(originalScaleName) && isScaleChannel(originalScaleName) && this.component.scales[originalScaleName]) ||
// in the scale name map (the scale get merged by its parent)
this.scaleNameMap.has(this.getName(originalScaleName))
) {
return this.scaleNameMap.get(this.getName(originalScaleName));
}
return undefined;
}
/**
* @return projection name after the projection has been parsed and named.
*/
public projectionName(parse?: boolean): string {
if (parse) {
// During the parse phase always return a value
// No need to refer to rename map because a projection can't be renamed
// before it has the original name.
return this.getName('projection');
}
if (
(this.component.projection && !this.component.projection.merged) ||
this.projectionNameMap.has(this.getName('projection'))
) {
return this.projectionNameMap.get(this.getName('projection'));
}
return undefined;
}
/**
* Corrects the data references in marks after assemble.
*/
public correctDataNames = (mark: VgMarkGroup) => {
// TODO: make this correct
// for normal data references
if (mark.from?.data) {
mark.from.data = this.lookupDataSource(mark.from.data);
}
// for access to facet data
if (mark.from?.facet?.data) {
mark.from.facet.data = this.lookupDataSource(mark.from.facet.data);
}
return mark;
};
/**
* Traverse a model's hierarchy to get the scale component for a particular channel.
*/
public getScaleComponent(channel: ScaleChannel): ScaleComponent {
/* istanbul ignore next: This is warning for debugging test */
if (!this.component.scales) {
throw new Error(
'getScaleComponent cannot be called before parseScale(). Make sure you have called parseScale or use parseUnitModelWithScale().'
);
}
const localScaleComponent = this.component.scales[channel];
if (localScaleComponent && !localScaleComponent.merged) {
return localScaleComponent;
}
return this.parent ? this.parent.getScaleComponent(channel) : undefined;
}
/**
* Traverse a model's hierarchy to get a particular selection component.
*/
public getSelectionComponent(variableName: string, origName: string): SelectionComponent {
let sel = this.component.selection[variableName];
if (!sel && this.parent) {
sel = this.parent.getSelectionComponent(variableName, origName);
}
if (!sel) {
throw new Error(log.message.selectionNotFound(origName));
}
return sel;
}
/**
* Returns true if the model has a signalRef for an axis orient.
*/
public hasAxisOrientSignalRef() {
return (
this.component.axes.x?.some(a => a.hasOrientSignalRef()) ||
this.component.axes.y?.some(a => a.hasOrientSignalRef())
);
}
}
/** Abstract class for UnitModel and FacetModel. Both of which can contain fieldDefs as a part of its own specification. */
export abstract class ModelWithField extends Model {
public abstract fieldDef(channel: SingleDefChannel): FieldDef<any>;
/** Get "field" reference for Vega */
public vgField(channel: SingleDefChannel, opt: FieldRefOption = {}) {
const fieldDef = this.fieldDef(channel);
if (!fieldDef) {
return undefined;
}
return vgField(fieldDef, opt);
}
protected abstract getMapping(): Partial<Record<ExtendedChannel, any>>;
public reduceFieldDef<T, U>(f: (acc: U, fd: FieldDef<string>, c: Channel) => U, init: T): T {
return reduce(
this.getMapping(),
(acc: U, cd: ChannelDef, c: Channel) => {
const fieldDef = getFieldDef(cd);
if (fieldDef) {
return f(acc, fieldDef, c);
}
return acc;
},
init
);
}
public forEachFieldDef(f: (fd: FieldDef<string>, c: ExtendedChannel) => void, t?: any) {
forEach(
this.getMapping(),
(cd, c) => {
const fieldDef = getFieldDef(cd);
if (fieldDef) {
f(fieldDef, c);
}
},
t
);
}
public abstract channelHasField(channel: Channel): boolean;
}