-
Notifications
You must be signed in to change notification settings - Fork 590
/
bin.ts
229 lines (194 loc) · 6.92 KB
/
bin.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
import {BinTransform as VgBinTransform, Transforms as VgTransform} from 'vega';
import {isString} from 'vega-util';
import {BinParams, binToString, isBinning, isSelectionExtent} from '../../bin';
import {Channel} from '../../channel';
import {binRequiresRange, FieldName, isTypedFieldDef, normalizeBin, TypedFieldDef, vgField} from '../../channeldef';
import {Config} from '../../config';
import {BinTransform} from '../../transform';
import {Dict, duplicate, hash, isEmpty, keys, replacePathInField, unique, vals, varName} from '../../util';
import {binFormatExpression} from '../format';
import {isUnitModel, Model, ModelWithField} from '../model';
import {parseSelectionExtent} from '../selection/parse';
import {NonPositionScaleChannel, PositionChannel} from './../../channel';
import {DataFlowNode} from './dataflow';
function rangeFormula(model: ModelWithField, fieldDef: TypedFieldDef<string>, channel: Channel, config: Config) {
if (binRequiresRange(fieldDef, channel)) {
// read format from axis or legend, if there is no format then use config.numberFormat
const guide = isUnitModel(model)
? model.axis(channel as PositionChannel) ?? model.legend(channel as NonPositionScaleChannel) ?? {}
: {};
const startField = vgField(fieldDef, {expr: 'datum'});
const endField = vgField(fieldDef, {expr: 'datum', binSuffix: 'end'});
return {
formulaAs: vgField(fieldDef, {binSuffix: 'range', forAs: true}),
formula: binFormatExpression(startField, endField, guide.format, guide.formatType, config)
};
}
return {};
}
function binKey(bin: BinParams, field: string) {
return `${binToString(bin)}_${field}`;
}
function getSignalsFromModel(model: Model, key: string) {
return {
signal: model.getName(`${key}_bins`),
extentSignal: model.getName(`${key}_extent`)
};
}
export function getBinSignalName(model: Model, field: string, bin: boolean | BinParams) {
const normalizedBin = normalizeBin(bin, undefined) ?? {};
const key = binKey(normalizedBin, field);
return model.getName(`${key}_bins`);
}
function isBinTransform(t: TypedFieldDef<string> | BinTransform): t is BinTransform {
return 'as' in t;
}
function createBinComponent(t: TypedFieldDef<string> | BinTransform, bin: boolean | BinParams, model: Model) {
let as: [string, string];
let span: string;
if (isBinTransform(t)) {
as = isString(t.as) ? [t.as, `${t.as}_end`] : [t.as[0], t.as[1]];
} else {
as = [vgField(t, {forAs: true}), vgField(t, {binSuffix: 'end', forAs: true})];
}
const normalizedBin = {...normalizeBin(bin, undefined)};
const key = binKey(normalizedBin, t.field);
const {signal, extentSignal} = getSignalsFromModel(model, key);
if (isSelectionExtent(normalizedBin.extent)) {
const ext = normalizedBin.extent;
const selName = ext.selection;
span = parseSelectionExtent(model.getSelectionComponent(varName(selName), selName), ext);
delete normalizedBin.extent; // Vega-Lite selection extent map to Vega's span property.
}
const binComponent: BinComponent = {
bin: normalizedBin,
field: t.field,
as: [as],
...(signal ? {signal} : {}),
...(extentSignal ? {extentSignal} : {}),
...(span ? {span} : {})
};
return {key, binComponent};
}
export interface BinComponent {
bin: BinParams;
field: FieldName;
extentSignal?: string;
signal?: string;
span?: string;
/** Pairs of strings of the names of start and end signals */
as: [string, string][];
// Range Formula
formula?: string;
formulaAs?: string;
}
export class BinNode extends DataFlowNode {
public clone() {
return new BinNode(null, duplicate(this.bins));
}
constructor(parent: DataFlowNode, private bins: Dict<BinComponent>) {
super(parent);
}
public static makeFromEncoding(parent: DataFlowNode, model: ModelWithField) {
const bins = model.reduceFieldDef((binComponentIndex: Dict<BinComponent>, fieldDef, channel) => {
if (isTypedFieldDef(fieldDef) && isBinning(fieldDef.bin)) {
const {key, binComponent} = createBinComponent(fieldDef, fieldDef.bin, model);
binComponentIndex[key] = {
...binComponent,
...binComponentIndex[key],
...rangeFormula(model, fieldDef, channel, model.config)
};
}
return binComponentIndex;
}, {} as Dict<BinComponent>);
if (isEmpty(bins)) {
return null;
}
return new BinNode(parent, bins);
}
/**
* Creates a bin node from BinTransform.
* The optional parameter should provide
*/
public static makeFromTransform(parent: DataFlowNode, t: BinTransform, model: Model) {
const {key, binComponent} = createBinComponent(t, t.bin, model);
return new BinNode(parent, {
[key]: binComponent
});
}
/**
* Merge bin nodes. This method either integrates the bin config from the other node
* or if this node already has a bin config, renames the corresponding signal in the model.
*/
public merge(other: BinNode, renameSignal: (s1: string, s2: string) => void) {
for (const key of keys(other.bins)) {
if (key in this.bins) {
renameSignal(other.bins[key].signal, this.bins[key].signal);
// Ensure that we don't have duplicate names for signal pairs
this.bins[key].as = unique([...this.bins[key].as, ...other.bins[key].as], hash);
} else {
this.bins[key] = other.bins[key];
}
}
for (const child of other.children) {
other.removeChild(child);
child.parent = this;
}
other.remove();
}
public producedFields() {
return new Set(
vals(this.bins)
.map(c => c.as)
.flat(2)
);
}
public dependentFields() {
return new Set(vals(this.bins).map(c => c.field));
}
public hash() {
return `Bin ${hash(this.bins)}`;
}
public assemble(): VgTransform[] {
return vals(this.bins).flatMap(bin => {
const transform: VgTransform[] = [];
const [binAs, ...remainingAs] = bin.as;
const {extent, ...params} = bin.bin;
const binTrans: VgBinTransform = {
type: 'bin',
field: replacePathInField(bin.field),
as: binAs,
signal: bin.signal,
...(!isSelectionExtent(extent) ? {extent} : {extent: null}),
...(bin.span ? {span: {signal: `span(${bin.span})`}} : {}),
...params
};
if (!extent && bin.extentSignal) {
transform.push({
type: 'extent',
field: replacePathInField(bin.field),
signal: bin.extentSignal
});
binTrans.extent = {signal: bin.extentSignal};
}
transform.push(binTrans);
for (const as of remainingAs) {
for (let i = 0; i < 2; i++) {
transform.push({
type: 'formula',
expr: vgField({field: binAs[i]}, {expr: 'datum'}),
as: as[i]
});
}
}
if (bin.formula) {
transform.push({
type: 'formula',
expr: bin.formula,
as: bin.formulaAs
});
}
return transform;
});
}
}