vega 0.3.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/LICENSE.txt +1 -1
- data/README.md +13 -20
- data/lib/vega/chart.rb +1 -1
- data/lib/vega/engine.rb +1 -1
- data/lib/vega/lite_chart.rb +1 -1
- data/lib/vega/spec.rb +20 -10
- data/lib/vega/version.rb +1 -1
- data/vendor/assets/javascripts/vega-embed.js +3 -5151
- data/vendor/assets/javascripts/vega-interpreter.js +297 -287
- data/vendor/assets/javascripts/vega-lite.js +1 -23000
- data/vendor/assets/javascripts/vega.js +92 -77
- metadata +4 -8
@@ -1,304 +1,314 @@
|
|
1
|
-
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.vega = global.vega || {}));
|
5
|
-
})(this, (function (exports) { 'use strict';
|
1
|
+
import { ascending, isString } from 'vega-util';
|
6
2
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
}
|
16
|
-
item.width = item.x2 - item.x;
|
17
|
-
} else {
|
18
|
-
item.x = item.x2 - (item.width || 0);
|
3
|
+
function adjustSpatial (item, encode, swap) {
|
4
|
+
let t;
|
5
|
+
if (encode.x2) {
|
6
|
+
if (encode.x) {
|
7
|
+
if (swap && item.x > item.x2) {
|
8
|
+
t = item.x;
|
9
|
+
item.x = item.x2;
|
10
|
+
item.x2 = t;
|
19
11
|
}
|
12
|
+
item.width = item.x2 - item.x;
|
13
|
+
} else {
|
14
|
+
item.x = item.x2 - (item.width || 0);
|
20
15
|
}
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
item.height = item.y2 - item.y;
|
32
|
-
} else {
|
33
|
-
item.y = item.y2 - (item.height || 0);
|
16
|
+
}
|
17
|
+
if (encode.xc) {
|
18
|
+
item.x = item.xc - (item.width || 0) / 2;
|
19
|
+
}
|
20
|
+
if (encode.y2) {
|
21
|
+
if (encode.y) {
|
22
|
+
if (swap && item.y > item.y2) {
|
23
|
+
t = item.y;
|
24
|
+
item.y = item.y2;
|
25
|
+
item.y2 = t;
|
34
26
|
}
|
27
|
+
item.height = item.y2 - item.y;
|
28
|
+
} else {
|
29
|
+
item.y = item.y2 - (item.height || 0);
|
35
30
|
}
|
36
|
-
if (encode.yc) {
|
37
|
-
item.y = item.yc - (item.height || 0) / 2;
|
38
|
-
}
|
39
31
|
}
|
32
|
+
if (encode.yc) {
|
33
|
+
item.y = item.yc - (item.height || 0) / 2;
|
34
|
+
}
|
35
|
+
}
|
40
36
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
37
|
+
var Constants = {
|
38
|
+
NaN: NaN,
|
39
|
+
E: Math.E,
|
40
|
+
LN2: Math.LN2,
|
41
|
+
LN10: Math.LN10,
|
42
|
+
LOG2E: Math.LOG2E,
|
43
|
+
LOG10E: Math.LOG10E,
|
44
|
+
PI: Math.PI,
|
45
|
+
SQRT1_2: Math.SQRT1_2,
|
46
|
+
SQRT2: Math.SQRT2,
|
47
|
+
MIN_VALUE: Number.MIN_VALUE,
|
48
|
+
MAX_VALUE: Number.MAX_VALUE
|
49
|
+
};
|
54
50
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
51
|
+
var Ops = {
|
52
|
+
'*': (a, b) => a * b,
|
53
|
+
'+': (a, b) => a + b,
|
54
|
+
'-': (a, b) => a - b,
|
55
|
+
'/': (a, b) => a / b,
|
56
|
+
'%': (a, b) => a % b,
|
57
|
+
'>': (a, b) => a > b,
|
58
|
+
'<': (a, b) => a < b,
|
59
|
+
'<=': (a, b) => a <= b,
|
60
|
+
'>=': (a, b) => a >= b,
|
61
|
+
'==': (a, b) => a == b,
|
62
|
+
'!=': (a, b) => a != b,
|
63
|
+
'===': (a, b) => a === b,
|
64
|
+
'!==': (a, b) => a !== b,
|
65
|
+
'&': (a, b) => a & b,
|
66
|
+
'|': (a, b) => a | b,
|
67
|
+
'^': (a, b) => a ^ b,
|
68
|
+
'<<': (a, b) => a << b,
|
69
|
+
'>>': (a, b) => a >> b,
|
70
|
+
'>>>': (a, b) => a >>> b
|
71
|
+
};
|
76
72
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
73
|
+
var Unary = {
|
74
|
+
'+': a => +a,
|
75
|
+
'-': a => -a,
|
76
|
+
'~': a => ~a,
|
77
|
+
'!': a => !a
|
78
|
+
};
|
83
79
|
|
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
|
-
|
80
|
+
const slice = Array.prototype.slice;
|
81
|
+
const apply = (m, args, cast) => {
|
82
|
+
const obj = cast ? cast(args[0]) : args[0];
|
83
|
+
return obj[m].apply(obj, slice.call(args, 1));
|
84
|
+
};
|
85
|
+
const datetime = function (yearOrTimestring) {
|
86
|
+
let m = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
87
|
+
let d = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
|
88
|
+
let H = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
|
89
|
+
let M = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
|
90
|
+
let S = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0;
|
91
|
+
let ms = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 0;
|
92
|
+
return isString(yearOrTimestring) ? new Date(yearOrTimestring) : new Date(yearOrTimestring, m, d, H, M, S, ms);
|
93
|
+
};
|
94
|
+
var Functions = {
|
95
|
+
// math functions
|
96
|
+
isNaN: Number.isNaN,
|
97
|
+
isFinite: Number.isFinite,
|
98
|
+
abs: Math.abs,
|
99
|
+
acos: Math.acos,
|
100
|
+
asin: Math.asin,
|
101
|
+
atan: Math.atan,
|
102
|
+
atan2: Math.atan2,
|
103
|
+
ceil: Math.ceil,
|
104
|
+
cos: Math.cos,
|
105
|
+
exp: Math.exp,
|
106
|
+
floor: Math.floor,
|
107
|
+
log: Math.log,
|
108
|
+
max: Math.max,
|
109
|
+
min: Math.min,
|
110
|
+
pow: Math.pow,
|
111
|
+
random: Math.random,
|
112
|
+
round: Math.round,
|
113
|
+
sin: Math.sin,
|
114
|
+
sqrt: Math.sqrt,
|
115
|
+
tan: Math.tan,
|
116
|
+
clamp: (a, b, c) => Math.max(b, Math.min(c, a)),
|
117
|
+
// date functions
|
118
|
+
now: Date.now,
|
119
|
+
utc: Date.UTC,
|
120
|
+
datetime: datetime,
|
121
|
+
date: d => new Date(d).getDate(),
|
122
|
+
day: d => new Date(d).getDay(),
|
123
|
+
year: d => new Date(d).getFullYear(),
|
124
|
+
month: d => new Date(d).getMonth(),
|
125
|
+
hours: d => new Date(d).getHours(),
|
126
|
+
minutes: d => new Date(d).getMinutes(),
|
127
|
+
seconds: d => new Date(d).getSeconds(),
|
128
|
+
milliseconds: d => new Date(d).getMilliseconds(),
|
129
|
+
time: d => new Date(d).getTime(),
|
130
|
+
timezoneoffset: d => new Date(d).getTimezoneOffset(),
|
131
|
+
utcdate: d => new Date(d).getUTCDate(),
|
132
|
+
utcday: d => new Date(d).getUTCDay(),
|
133
|
+
utcyear: d => new Date(d).getUTCFullYear(),
|
134
|
+
utcmonth: d => new Date(d).getUTCMonth(),
|
135
|
+
utchours: d => new Date(d).getUTCHours(),
|
136
|
+
utcminutes: d => new Date(d).getUTCMinutes(),
|
137
|
+
utcseconds: d => new Date(d).getUTCSeconds(),
|
138
|
+
utcmilliseconds: d => new Date(d).getUTCMilliseconds(),
|
139
|
+
// sequence functions
|
140
|
+
length: x => x.length,
|
141
|
+
join: function () {
|
142
|
+
return apply('join', arguments);
|
143
|
+
},
|
144
|
+
indexof: function () {
|
145
|
+
return apply('indexOf', arguments);
|
146
|
+
},
|
147
|
+
lastindexof: function () {
|
148
|
+
return apply('lastIndexOf', arguments);
|
149
|
+
},
|
150
|
+
slice: function () {
|
151
|
+
return apply('slice', arguments);
|
152
|
+
},
|
153
|
+
reverse: x => x.slice().reverse(),
|
154
|
+
sort: x => x.slice().sort(ascending),
|
155
|
+
// string functions
|
156
|
+
parseFloat: parseFloat,
|
157
|
+
parseInt: parseInt,
|
158
|
+
upper: x => String(x).toUpperCase(),
|
159
|
+
lower: x => String(x).toLowerCase(),
|
160
|
+
substring: function () {
|
161
|
+
return apply('substring', arguments, String);
|
162
|
+
},
|
163
|
+
split: function () {
|
164
|
+
return apply('split', arguments, String);
|
165
|
+
},
|
166
|
+
replace: function () {
|
167
|
+
return apply('replace', arguments, String);
|
168
|
+
},
|
169
|
+
trim: x => String(x).trim(),
|
170
|
+
// Base64 encode/decode
|
171
|
+
// Convert binary string to base64-encoded ascii
|
172
|
+
btoa: x => btoa(x),
|
173
|
+
// Convert base64-encoded ascii to binary string
|
174
|
+
atob: x => atob(x),
|
175
|
+
// regexp functions
|
176
|
+
regexp: RegExp,
|
177
|
+
test: (r, t) => RegExp(r).test(t)
|
178
|
+
};
|
169
179
|
|
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
|
-
|
180
|
+
const EventFunctions = ['view', 'item', 'group', 'xy', 'x', 'y'];
|
181
|
+
const DisallowedMethods = new Set([Function, eval, setTimeout, setInterval]);
|
182
|
+
if (typeof setImmediate === 'function') DisallowedMethods.add(setImmediate);
|
183
|
+
const Visitors = {
|
184
|
+
Literal: ($, n) => n.value,
|
185
|
+
Identifier: ($, n) => {
|
186
|
+
const id = n.name;
|
187
|
+
return $.memberDepth > 0 ? id : id === 'datum' ? $.datum : id === 'event' ? $.event : id === 'item' ? $.item : Constants[id] || $.params['$' + id];
|
188
|
+
},
|
189
|
+
MemberExpression: ($, n) => {
|
190
|
+
const d = !n.computed,
|
191
|
+
o = $(n.object);
|
192
|
+
if (d) $.memberDepth += 1;
|
193
|
+
const p = $(n.property);
|
194
|
+
if (d) $.memberDepth -= 1;
|
195
|
+
if (DisallowedMethods.has(o[p])) {
|
196
|
+
// eslint-disable-next-line no-console
|
197
|
+
console.error(`Prevented interpretation of member "${p}" which could lead to insecure code execution`);
|
198
|
+
return;
|
199
|
+
}
|
200
|
+
return o[p];
|
201
|
+
},
|
202
|
+
CallExpression: ($, n) => {
|
203
|
+
const args = n.arguments;
|
204
|
+
let name = n.callee.name;
|
195
205
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
206
|
+
// handle special internal functions used by encoders
|
207
|
+
// re-route to corresponding standard function
|
208
|
+
if (name.startsWith('_')) {
|
209
|
+
name = name.slice(1);
|
210
|
+
}
|
201
211
|
|
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
|
-
|
212
|
+
// special case "if" due to conditional evaluation of branches
|
213
|
+
return name === 'if' ? $(args[0]) ? $(args[1]) : $(args[2]) : ($.fn[name] || Functions[name]).apply($.fn, args.map($));
|
214
|
+
},
|
215
|
+
ArrayExpression: ($, n) => n.elements.map($),
|
216
|
+
BinaryExpression: ($, n) => Ops[n.operator]($(n.left), $(n.right)),
|
217
|
+
UnaryExpression: ($, n) => Unary[n.operator]($(n.argument)),
|
218
|
+
ConditionalExpression: ($, n) => $(n.test) ? $(n.consequent) : $(n.alternate),
|
219
|
+
LogicalExpression: ($, n) => n.operator === '&&' ? $(n.left) && $(n.right) : $(n.left) || $(n.right),
|
220
|
+
ObjectExpression: ($, n) => n.properties.reduce((o, p) => {
|
221
|
+
$.memberDepth += 1;
|
222
|
+
const k = $(p.key);
|
223
|
+
$.memberDepth -= 1;
|
224
|
+
if (DisallowedMethods.has($(p.value))) {
|
225
|
+
// eslint-disable-next-line no-console
|
226
|
+
console.error(`Prevented interpretation of property "${k}" which could lead to insecure code execution`);
|
227
|
+
} else {
|
228
|
+
o[k] = $(p.value);
|
229
|
+
}
|
230
|
+
return o;
|
231
|
+
}, {})
|
232
|
+
};
|
233
|
+
function interpret (ast, fn, params, datum, event, item) {
|
234
|
+
const $ = n => Visitors[n.type]($, n);
|
235
|
+
$.memberDepth = 0;
|
236
|
+
$.fn = Object.create(fn);
|
237
|
+
$.params = params;
|
238
|
+
$.datum = datum;
|
239
|
+
$.event = event;
|
240
|
+
$.item = item;
|
231
241
|
|
232
|
-
|
233
|
-
|
234
|
-
return
|
235
|
-
}
|
242
|
+
// route event functions to annotated vega event context
|
243
|
+
EventFunctions.forEach(f => $.fn[f] = function () {
|
244
|
+
return event.vega[f](...arguments);
|
245
|
+
});
|
246
|
+
return $(ast);
|
247
|
+
}
|
236
248
|
|
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
|
-
if (marktype !== 'rule') {
|
295
|
-
adjustSpatial(item, channels, swap);
|
249
|
+
var expression = {
|
250
|
+
/**
|
251
|
+
* Parse an expression used to update an operator value.
|
252
|
+
*/
|
253
|
+
operator(ctx, expr) {
|
254
|
+
const ast = expr.ast,
|
255
|
+
fn = ctx.functions;
|
256
|
+
return _ => interpret(ast, fn, _);
|
257
|
+
},
|
258
|
+
/**
|
259
|
+
* Parse an expression provided as an operator parameter value.
|
260
|
+
*/
|
261
|
+
parameter(ctx, expr) {
|
262
|
+
const ast = expr.ast,
|
263
|
+
fn = ctx.functions;
|
264
|
+
return (datum, _) => interpret(ast, fn, _, datum);
|
265
|
+
},
|
266
|
+
/**
|
267
|
+
* Parse an expression applied to an event stream.
|
268
|
+
*/
|
269
|
+
event(ctx, expr) {
|
270
|
+
const ast = expr.ast,
|
271
|
+
fn = ctx.functions;
|
272
|
+
return event => interpret(ast, fn, undefined, undefined, event);
|
273
|
+
},
|
274
|
+
/**
|
275
|
+
* Parse an expression used to handle an event-driven operator update.
|
276
|
+
*/
|
277
|
+
handler(ctx, expr) {
|
278
|
+
const ast = expr.ast,
|
279
|
+
fn = ctx.functions;
|
280
|
+
return (_, event) => {
|
281
|
+
const datum = event.item && event.item.datum;
|
282
|
+
return interpret(ast, fn, _, datum, event);
|
283
|
+
};
|
284
|
+
},
|
285
|
+
/**
|
286
|
+
* Parse an expression that performs visual encoding.
|
287
|
+
*/
|
288
|
+
encode(ctx, encode) {
|
289
|
+
const {
|
290
|
+
marktype,
|
291
|
+
channels
|
292
|
+
} = encode,
|
293
|
+
fn = ctx.functions,
|
294
|
+
swap = marktype === 'group' || marktype === 'image' || marktype === 'rect';
|
295
|
+
return (item, _) => {
|
296
|
+
const datum = item.datum;
|
297
|
+
let m = 0,
|
298
|
+
v;
|
299
|
+
for (const name in channels) {
|
300
|
+
v = interpret(channels[name].ast, fn, _, datum, undefined, item);
|
301
|
+
if (item[name] !== v) {
|
302
|
+
item[name] = v;
|
303
|
+
m = 1;
|
296
304
|
}
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
305
|
+
}
|
306
|
+
if (marktype !== 'rule') {
|
307
|
+
adjustSpatial(item, channels, swap);
|
308
|
+
}
|
309
|
+
return m;
|
310
|
+
};
|
311
|
+
}
|
312
|
+
};
|
303
313
|
|
304
|
-
}
|
314
|
+
export { expression as expressionInterpreter };
|