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 | 1×
1×
1×
1×
1×
1×
1452×
722×
730×
1×
722×
1×
64×
61×
3×
2×
3×
1×
61×
1×
9×
9×
9×
9×
6×
9×
3×
3×
9×
12×
58×
58×
58×
12×
470×
470×
470×
237×
470×
199×
470×
120×
470×
13×
13×
13×
1×
12×
33×
33×
33×
33×
33×
31×
12×
73×
73×
26×
47×
53×
53×
53×
6×
6×
6×
6×
6×
6×
5×
5×
2×
3×
67×
67×
67×
67×
1×
66×
273×
207×
273×
66×
6×
6×
8×
6×
5×
5×
5×
3×
5×
5×
2×
3×
1×
1×
1×
2×
2×
5×
2×
2×
2×
2×
2×
1×
1×
5×
5×
4×
1×
1×
1×
2281×
2281×
2281×
883×
727×
156×
156×
460×
156×
1398×
26×
1372×
1×
1371×
182×
182×
451×
451×
182×
1189×
1×
12×
1×
7×
1×
1×
19×
5×
14×
2×
12×
8×
8×
11×
11×
8×
10×
2×
8×
4×
3×
3×
3×
3×
2×
2×
3×
1×
1×
1×
| import d3 from 'd3';
import vg from 'vega';
import { isArray, isString } from 'datalib';
import axisTemplate from './axis.json';
import { getElementSize, read } from '..';
let getNestedRec = function (spec, parts) {
if (spec === undefined || parts.length === 0) {
return spec;
}
return getNestedRec(spec[parts[0]], parts.slice(1));
};
let getNested = function (spec, name) {
return getNestedRec(spec, name.split('.'));
};
let setNestedRec = function (spec, parts, value) {
if (parts.length === 1) {
spec[parts[0]] = value;
return;
}
if (spec[parts[0]] === undefined) {
spec[parts[0]] = {};
}
setNestedRec(spec[parts[0]], parts.slice(1), value);
};
let setNested = function (spec, name, value) {
setNestedRec(spec, name.split('.'), value);
};
let templateFunctions = {
defaults: function (args, options, scope) {
for (let index = 0; index < args[0].length; index += 1) {
let name = transform(args[0][index][0], options, scope);
let value = getNested(scope, name);
if (value === undefined) {
value = getNested(options, name);
}
if (value === undefined) {
value = transform(args[0][index][1], options, scope);
setNested(scope, name, value);
}
}
return transform(args[1], options, scope);
},
let: function (args, options, scope) {
for (let index = 0; index < args[0].length; index += 1) {
let name = transform(args[0][index][0], options, scope);
let value = transform(args[0][index][1], options, scope);
setNested(scope, name, value);
}
return transform(args[1], options, scope);
},
get: function (args, options, scope) {
let name = transform(args[0], options, scope);
let value = getNested(scope, name);
if (value === undefined) {
value = getNested(options, name);
}
if (value === undefined) {
value = transform(args[1], options, scope);
}
if (value === undefined) {
value = null;
}
return value;
},
map: function (args, options, scope) {
let transformed = [];
let elements = transform(args[0], options, scope);
if (!Array.isArray(elements)) {
return transformed;
}
for (let elementIndex = 0; elementIndex < elements.length; elementIndex += 1) {
scope[args[1]] = elements[elementIndex];
scope.index = elementIndex;
for (let itemIndex = 2; itemIndex < args.length; itemIndex += 1) {
let element = transform(args[itemIndex], options, scope);
if (element !== null) {
transformed.push(element);
}
}
}
return transformed;
},
if: function (args, options, scope) {
let condition = transform(args[0], options, scope);
if (condition) {
return transform(args[1], options, scope);
}
return transform(args[2], options, scope);
},
eq: function (args, options, scope) {
let a = transform(args[0], options, scope);
let b = transform(args[1], options, scope);
return (a === b);
},
lt: function (args, options, scope) {
let a = transform(args[0], options, scope);
let b = transform(args[1], options, scope);
return (a < b);
},
gt: function (args, options, scope) {
let a = transform(args[0], options, scope);
let b = transform(args[1], options, scope);
return (a > b);
},
length: function (args, options, scope) {
let a = transform(args[0], options, scope);
if (isArray(a) || isString(a)) {
return a.length;
}
return 0;
},
and: function (args, options, scope) {
let arr = transform(args, options, scope);
for (let i = 0; i < arr.length; i += 1) {
if (!arr[i]) {
return false;
}
}
return true;
},
or: function (args, options, scope) {
let arr = transform(args, options, scope);
for (let i = 0; i < arr.length; i += 1) {
if (arr[i]) {
return true;
}
}
return false;
},
mult: function (args, options, scope) {
let arr = transform(args, options, scope);
let value = 1;
for (let i = 0; i < arr.length; i += 1) {
value *= arr[i];
}
return value;
},
add: function (args, options, scope) {
let arr = transform(args, options, scope);
let value = 0;
for (let i = 0; i < arr.length; i += 1) {
value += arr[i];
}
return value;
},
join: function (args, options, scope) {
let result = '';
let sep = transform(args[0], options, scope);
let arr = transform(args[1], options, scope);
if (!Array.isArray(arr)) {
return result;
}
for (let i = 0; i < arr.length; i += 1) {
if (i > 0) {
result += sep;
}
result += arr[i];
}
return result;
},
merge: function (args, options, scope) {
let merged = transform(args[0], options, scope);
for (let i = 1; i < args.length; i += 1) {
merged = merge(merged, transform(args[i], options, scope));
}
return merged;
},
colorScale: function (args, options, scope) {
let params = transform(args[0], options, scope);
let name = params.name || 'color';
if (!Array.isArray(params.values) ||
params.values.length < 1 ||
params.values[0][params.field] === undefined ||
typeof params.values[0][params.field] === 'string') {
return {
name: name,
type: 'ordinal',
domain: {data: params.data, field: params.field},
range: 'category10'
};
}
return {
name: name,
type: 'linear',
zero: false,
domain: {data: params.data, field: params.field},
range: ['#df2709', '#2709df']
};
},
axis: function (args, options, scope) {
let opt = transform(args[0], options, scope);
if (Eopt.data && Array.isArray(opt.data) && opt.field) {
if (!opt.data.__types__) {
read(opt.data);
}
let type = opt.data.__types__[opt.field];
if (type === 'string') {
opt.formatType = 'string';
} else if (type === 'date') {
opt.type = opt.type || 'time';
opt.formatType = 'time';
opt.format = '%Y-%m-%d';
} else {
opt.formatType = 'number';
opt.format = 's';
}
}
return transform(axisTemplate, opt);
},
isStringField: function (args, options, scope) {
let values = transform(args[0], options, scope);
let field = transform(args[1], options, scope);
if (!Array.isArray(values) ||
values.length < 1 ||
values[0][field] === undefined ||
typeof values[0][field] === 'string') {
return true;
}
return false;
},
orient: function (args, options, scope) {
let dir = transform(args[0], options, scope);
let obj = transform(args[1], options, scope);
let transformed = {};
let matching = {
x: 'y',
x2: 'y2',
xc: 'yc',
y: 'x',
y2: 'x2',
yc: 'xc',
width: 'height',
height: 'width'
};
if (dir === 'horizontal') {
return obj;
}
for (let key in obj) {
if (Eobj.hasOwnProperty(key)) {
if (matching[key]) {
transformed[matching[key]] = obj[key];
} else {
transformed[key] = obj[key];
}
}
}
return transformed;
}
};
let transform = function (spec, options, scope) {
options = options || {};
scope = scope || {};
if (Array.isArray(spec)) {
if (spec.length > 0 && (typeof spec[0] === 'string') && spec[0].slice(0, 1) === '@') {
return templateFunctions[spec[0].slice(1)](spec.slice(1), options, scope);
}
let transformed = [];
for (let index = 0; index < spec.length; index += 1) {
transformed.push(transform(spec[index], options, scope));
}
return transformed;
}
if (spec === null) {
return spec;
}
if (spec instanceof Date) {
return spec;
}
if (typeof spec === 'object') {
let transformed = {};
for (let key in spec) {
if (Espec.hasOwnProperty(key)) {
transformed[key] = transform(spec[key], options, scope);
}
}
return transformed;
}
return spec;
};
let isObjectLiteral = function (object) {
return object && object.constructor && object.constructor.name === 'Object';
};
let isArrayLiteral = function (object) {
return object && object.constructor && object.constructor.name === 'Array';
};
let extend = function (defaults, options) {
if (options === undefined) {
return defaults;
}
if (isObjectLiteral(defaults)) {
let extended = {};
for (let prop in defaults) {
if (Object.prototype.hasOwnProperty.call(defaults, prop)) {
extended[prop] = extend(defaults[prop], options[prop]);
}
}
for (let prop in options) {
if (!Object.prototype.hasOwnProperty.call(defaults, prop)) {
extended[prop] = options[prop];
}
}
return extended;
}
if (isArrayLiteral(defaults)) {
let extended = [];
for (let index = 0; index < defaults.length; index += 1) {
extended.push(extend(defaults[index], options[index]));
}
if (isArrayLiteral(options)) {
for (let index = defaults.length; index < options.length; index += 1) {
extended.push(options[index]);
}
}
return extended;
}
return options;
};
let merge = function (defaults, options) {
if (options === undefined || options === null) {
return defaults;
}
if (defaults === undefined || defaults === null) {
return options;
}
if (isObjectLiteral(defaults)) {
let extended = {};
for (let prop in defaults) {
if (EObject.prototype.hasOwnProperty.call(defaults, prop)) {
extended[prop] = merge(defaults[prop], options[prop]);
}
}
for (let prop in options) {
if (!Object.prototype.hasOwnProperty.call(extended, prop)) {
extended[prop] = options[prop];
}
}
return extended;
}
if (isArrayLiteral(defaults)) {
let extended = [];
for (let index = 0; index < defaults.length; index += 1) {
extended.push(defaults[index]);
}
if (isArrayLiteral(options)) {
for (let index = 0; index < options.length; index += 1) {
extended.push(options[index]);
}
}
return extended;
}
return defaults;
};
let parseChart = function (spec, element, options) {
// Use element size to set size, unless size explicitly specified or
// element size is zero.
let el = d3.select(element)[0][0];
let sizeOptions = {};
const size = getElementSize(el);
const elWidth = size.width;
const elHeight = size.height;
if (elWidth !== 0 && elHeight !== 0) {
if (options.width === undefined) {
sizeOptions.width = elWidth;
}
if (options.height === undefined) {
sizeOptions.height = elHeight;
}
}
let curOptions = extend(options, sizeOptions);
// Options that go directly to Vega runtime
let vegaOptions = {
el: el,
renderer: curOptions.renderer
};
let vegaSpec = transform(spec, curOptions);
// Return a promise for the Vega chart object
return new Promise(resolve => {
vg.parse.spec(vegaSpec, chartObj => {
let chart = chartObj(vegaOptions);
chart.update();
resolve(chart);
});
});
};
export default {
transform,
parseChart
};
|