-
Notifications
You must be signed in to change notification settings - Fork 590
/
project.ts
202 lines (180 loc) · 7.28 KB
/
project.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
import {array, isObject} from 'vega-util';
import {isSingleDefUnitChannel, ScaleChannel, SingleDefUnitChannel} from '../../channel';
import * as log from '../../log';
import {hasContinuousDomain} from '../../scale';
import {PointSelectionConfig, SelectionInitIntervalMapping, SelectionInitMapping, SELECTION_ID} from '../../selection';
import {Dict, hash, keys, replacePathInField, varName, isEmpty} from '../../util';
import {TimeUnitComponent, TimeUnitNode} from '../data/timeunit';
import {SelectionCompiler} from '.';
export const TUPLE_FIELDS = '_tuple_fields';
/**
* Whether the selection tuples hold enumerated or ranged values for a field.
*/
export type TupleStoreType =
// enumerated
| 'E'
// ranged, exclusive, left-right inclusive
| 'R'
// ranged, left-inclusive, right-exclusive
| 'R-RE';
export interface SelectionProjection {
type: TupleStoreType;
field: string;
channel?: SingleDefUnitChannel;
signals?: {data?: string; visual?: string};
hasLegend?: boolean;
}
export class SelectionProjectionComponent {
public hasChannel: Partial<Record<SingleDefUnitChannel, SelectionProjection>>;
public hasField: Record<string, SelectionProjection>;
public hasSelectionId: boolean;
public timeUnit?: TimeUnitNode;
public items: SelectionProjection[];
constructor(...items: SelectionProjection[]) {
this.items = items;
this.hasChannel = {};
this.hasField = {};
this.hasSelectionId = false;
}
}
const project: SelectionCompiler = {
defined: () => {
return true; // This transform handles its own defaults, so always run parse.
},
parse: (model, selCmpt, selDef) => {
const name = selCmpt.name;
const proj = (selCmpt.project ??= new SelectionProjectionComponent());
const parsed: Dict<SelectionProjection> = {};
const timeUnits: Dict<TimeUnitComponent> = {};
const signals = new Set<string>();
const signalName = (p: SelectionProjection, range: 'data' | 'visual') => {
const suffix = range === 'visual' ? p.channel : p.field;
let sg = varName(`${name}_${suffix}`);
for (let counter = 1; signals.has(sg); counter++) {
sg = varName(`${name}_${suffix}_${counter}`);
}
signals.add(sg);
return {[range]: sg};
};
const type = selCmpt.type;
const cfg = model.config.selection[type];
const init =
selDef.value !== undefined
? (array(selDef.value as any) as SelectionInitMapping[] | SelectionInitIntervalMapping[])
: null;
// If no explicit projection (either fields or encodings) is specified, set some defaults.
// If an initial value is set, try to infer projections.
let {fields, encodings} = (isObject(selDef.select) ? selDef.select : {}) as PointSelectionConfig;
if (!fields && !encodings && init) {
for (const initVal of init) {
// initVal may be a scalar value to smoothen varParam -> pointSelection gradient.
if (!isObject(initVal)) {
continue;
}
for (const key of keys(initVal)) {
if (isSingleDefUnitChannel(key)) {
(encodings || (encodings = [])).push(key as SingleDefUnitChannel);
} else {
if (type === 'interval') {
log.warn(log.message.INTERVAL_INITIALIZED_WITH_X_Y);
encodings = cfg.encodings;
} else {
(fields || (fields = [])).push(key);
}
}
}
}
}
// If no initial value is specified, use the default configuration.
// We break this out as a separate if block (instead of an else condition)
// to account for unprojected point selections that have scalar initial values
if (!fields && !encodings) {
encodings = cfg.encodings;
if ('fields' in cfg) {
fields = cfg.fields;
}
}
for (const channel of encodings ?? []) {
const fieldDef = model.fieldDef(channel);
if (fieldDef) {
let field = fieldDef.field;
if (fieldDef.aggregate) {
log.warn(log.message.cannotProjectAggregate(channel, fieldDef.aggregate));
continue;
} else if (!field) {
log.warn(log.message.cannotProjectOnChannelWithoutField(channel));
continue;
}
if (fieldDef.timeUnit) {
field = model.vgField(channel);
// Construct TimeUnitComponents which will be combined into a
// TimeUnitNode. This node may need to be inserted into the
// dataflow if the selection is used across views that do not
// have these time units defined.
const component = {
timeUnit: fieldDef.timeUnit,
as: field,
field: fieldDef.field
};
timeUnits[hash(component)] = component;
}
// Prevent duplicate projections on the same field.
// TODO: what if the same field is bound to multiple channels (e.g., SPLOM diag).
if (!parsed[field]) {
// Determine whether the tuple will store enumerated or ranged values.
// Interval selections store ranges for continuous scales, and enumerations otherwise.
// Single/multi selections store ranges for binned fields, and enumerations otherwise.
let tplType: TupleStoreType = 'E';
if (type === 'interval') {
const scaleType = model.getScaleComponent(channel as ScaleChannel).get('type');
if (hasContinuousDomain(scaleType)) {
tplType = 'R';
}
} else if (fieldDef.bin) {
tplType = 'R-RE';
}
const p: SelectionProjection = {field, channel, type: tplType};
p.signals = {...signalName(p, 'data'), ...signalName(p, 'visual')};
proj.items.push((parsed[field] = p));
proj.hasField[field] = proj.hasChannel[channel] = parsed[field];
proj.hasSelectionId = proj.hasSelectionId || field === SELECTION_ID;
}
} else {
log.warn(log.message.cannotProjectOnChannelWithoutField(channel));
}
}
for (const field of fields ?? []) {
if (proj.hasField[field]) continue;
const p: SelectionProjection = {type: 'E', field};
p.signals = {...signalName(p, 'data')};
proj.items.push(p);
proj.hasField[field] = p;
proj.hasSelectionId = proj.hasSelectionId || field === SELECTION_ID;
}
if (init) {
selCmpt.init = (init as any).map((v: SelectionInitMapping | SelectionInitIntervalMapping) => {
// Selections can be initialized either with a full object that maps projections to values
// or scalar values to smoothen the abstraction gradient from variable params to point selections.
return proj.items.map(p => (isObject(v) ? (v[p.channel] !== undefined ? v[p.channel] : v[p.field]) : v));
});
}
if (!isEmpty(timeUnits)) {
proj.timeUnit = new TimeUnitNode(null, timeUnits);
}
},
signals: (model, selCmpt, allSignals) => {
const name = selCmpt.name + TUPLE_FIELDS;
const hasSignal = allSignals.filter(s => s.name === name);
return hasSignal.length > 0 || selCmpt.project.hasSelectionId
? allSignals
: allSignals.concat({
name,
value: selCmpt.project.items.map(proj => {
const {signals, hasLegend, ...rest} = proj;
rest.field = replacePathInField(rest.field);
return rest;
})
});
}
};
export default project;