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 | 1
1
1
1
1
44
44
44
28
44
44
44
9
44
1
1
1
1
1
47
47
4
4
43
43
47
47
42
47
47
1
1
47
47
109
7
7
102
65
11
54
54
47
1
48
1
1
49
113
6
6
113
1
1
49
113
16
16
113
1
1
50
114
11
103
66
103
50
66
1
1
49
49
1
1
48
48
47
1
1
1
1
1
1
1
1
45
45
45
45
109
31
31
11
11
20
20
78
14
14
14
64
45
45
31
31
45
29
16
1
1
1
1
9
9
9
9
9
2
9
1
1
44
106
5
1
| var vlEncDef = require('../encdef');
var util = require('../util');
var consts_1 = require('../consts');
var time = require('./time');
function default_1(encoding) {
var def = [source.def(encoding)];
var summaryDef = summary.def(encoding);
if (summaryDef) {
def.push(summaryDef);
}
filterNonPositive(def[def.length - 1], encoding);
var stackCfg = encoding.stack();
if (stackCfg) {
def.push(stack.def(encoding, stackCfg));
}
return def;
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
var source;
(function (source_1) {
function def(encoding) {
var source = { name: consts_1.Table.SOURCE };
if (encoding.hasValues()) {
source.values = encoding.data().values;
source.format = { type: 'json' };
}
else {
source.url = encoding.data().url;
source.format = { type: encoding.data().formatType };
}
var parse = formatParse(encoding);
if (parse) {
source.format.parse = parse;
}
source.transform = transform(encoding);
return source;
}
source_1.def = def;
;
function formatParse(encoding) {
var parse;
encoding.forEach(function (encDef) {
if (encDef.type == consts_1.Type.T) {
parse = parse || {};
parse[encDef.name] = 'date';
}
else if (encDef.type == consts_1.Type.Q) {
if (vlEncDef.isCount(encDef))
return;
parse = parse || {};
parse[encDef.name] = 'number';
}
});
return parse;
}
;
function transform(encoding) {
return nullFilterTransform(encoding).concat(formulaTransform(encoding), timeTransform(encoding), binTransform(encoding), filterTransform(encoding));
}
source_1.transform = transform;
;
function timeTransform(encoding) {
return encoding.reduce(function (transform, encDef, encType) {
if (encDef.type === consts_1.Type.T && encDef.timeUnit) {
var fieldRef = encoding.fieldRef(encType, { nofn: true, datum: true });
transform.push({
type: 'formula',
field: encoding.fieldRef(encType),
expr: time.formula(encDef.timeUnit, fieldRef)
});
}
return transform;
}, []);
}
source_1.timeTransform = timeTransform;
;
function binTransform(encoding) {
return encoding.reduce(function (transform, encDef, encType) {
if (encoding.bin(encType)) {
transform.push({
type: 'bin',
field: encDef.name,
output: {
start: encoding.fieldRef(encType, { bin_suffix: '_start' }),
end: encoding.fieldRef(encType, { bin_suffix: '_end' })
},
maxbins: encoding.bin(encType).maxbins
});
transform.push({
type: 'formula',
field: encoding.fieldRef(encType, { bin_suffix: '_mid' }),
expr: '(' + encoding.fieldRef(encType, { datum: 1, bin_suffix: '_start' }) + '+' + encoding.fieldRef(encType, { datum: 1, bin_suffix: '_end' }) + ')/2'
});
}
return transform;
}, []);
}
source_1.binTransform = binTransform;
;
function nullFilterTransform(encoding) {
var filteredFields = util.reduce(encoding.fields(), function (filteredFields, fieldList, fieldName) {
if (fieldName === '*')
return filteredFields;
if ((encoding.config('filterNull').Q && fieldList.containsType[consts_1.Type.Q]) ||
(encoding.config('filterNull').T && fieldList.containsType[consts_1.Type.T]) ||
(encoding.config('filterNull').O && fieldList.containsType[consts_1.Type.O]) ||
(encoding.config('filterNull').N && fieldList.containsType[consts_1.Type.N])) {
filteredFields.push(fieldName);
}
return filteredFields;
}, []);
return filteredFields.length > 0 ?
[{
type: 'filter',
test: filteredFields.map(function (fieldName) {
return 'datum.' + fieldName + '!==null';
}).join(' && ')
}] : [];
}
source_1.nullFilterTransform = nullFilterTransform;
;
function filterTransform(encoding) {
var filter = encoding.data().filter;
return filter ? [{
type: 'filter',
test: filter
}] : [];
}
source_1.filterTransform = filterTransform;
;
function formulaTransform(encoding) {
var formulas = encoding.data().formulas;
if (formulas === undefined) {
return [];
}
return formulas.reduce(function (transform, formula) {
formula.type = 'formula';
transform.push(formula);
return transform;
}, []);
}
source_1.formulaTransform = formulaTransform;
;
})(source = exports.source || (exports.source = {}));
var summary;
(function (summary) {
function def(encoding) {
var dims = {};
var meas = {};
var hasAggregate = false;
encoding.forEach(function (encDef, encType) {
if (encDef.aggregate) {
hasAggregate = true;
if (encDef.aggregate === 'count') {
meas['*'] = meas['*'] || {};
meas['*'].count = true;
}
else {
meas[encDef.name] = meas[encDef.name] || {};
meas[encDef.name][encDef.aggregate] = true;
}
}
else {
if (encDef.bin) {
dims[encoding.fieldRef(encType, { bin_suffix: '_start' })] = encoding.fieldRef(encType, { bin_suffix: '_start' });
dims[encoding.fieldRef(encType, { bin_suffix: '_mid' })] = encoding.fieldRef(encType, { bin_suffix: '_mid' });
dims[encoding.fieldRef(encType, { bin_suffix: '_end' })] = encoding.fieldRef(encType, { bin_suffix: '_end' });
}
else {
dims[encDef.name] = encoding.fieldRef(encType);
}
}
});
var groupby = util.vals(dims);
var summarize = util.reduce(meas, function (summarize, fnDictSet, field) {
summarize[field] = util.keys(fnDictSet);
return summarize;
}, {});
if (hasAggregate) {
return {
name: consts_1.Table.SUMMARY,
source: consts_1.Table.SOURCE,
transform: [{
type: 'aggregate',
groupby: groupby,
summarize: summarize
}]
};
}
return null;
}
summary.def = def;
;
})(summary = exports.summary || (exports.summary = {}));
var stack;
(function (stack) {
function def(encoding, stackCfg) {
var dim = stackCfg.groupby;
var val = stackCfg.value;
var facets = encoding.facets();
var stacked = {
name: consts_1.Table.STACKED,
source: encoding.dataTable(),
transform: [{
type: 'aggregate',
groupby: [encoding.fieldRef(dim)].concat(facets),
summarize: [{ ops: ['sum'], field: encoding.fieldRef(val) }]
}]
};
if (facets && facets.length > 0) {
stacked.transform.push({
type: 'aggregate',
groupby: facets,
summarize: [{
ops: ['max'],
field: encoding.fieldRef(val, { prefn: 'sum_' })
}]
});
}
return stacked;
}
stack.def = def;
;
})(stack = exports.stack || (exports.stack = {}));
function filterNonPositive(dataTable, encoding) {
encoding.forEach(function (encDef, encType) {
if (encoding.scale(encType).type === 'log') {
dataTable.transform.push({
type: 'filter',
test: encoding.fieldRef(encType, { datum: 1 }) + ' > 0'
});
}
});
}
exports.filterNonPositive = filterNonPositive;
;
//# sourceMappingURL=data.js.map |