-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathapi.js
169 lines (148 loc) · 5.61 KB
/
api.js
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
import {enums, props, types} from './generate/schema';
import {capitalize, reduce} from './generate/util';
import schema from 'vega-lite/build/vega-lite-schema';
import {aggregateOps, timeUnitOps, windowOps} from './ops';
import {
transform, groupby, aggregateOp, timeUnitOp, windowOp,
field, fieldType, not, logical, repeat, value,
binding, expr, param, selection, selectionConfig, selectionRef,
selectionDeprecated,
projection, encoding, channel,
source, sourceFormat, generator, format,
data, lookupData, lookupSelection,
unit, mark, layer, spec
} from './types';
const markTypes = enums(schema, {$ref: '#/definitions/AnyMark'});
const dataFormats = types(schema, {$ref: '#/definitions/DataFormat'});
function apiOps(ops, method, ...params) {
return reduce(ops, v => method(...ops[v], ...params));
}
function formats() {
return reduce(dataFormats, v => format(v), k => `${k}Format`);
}
function sources() {
return reduce(dataFormats, v => sourceFormat(v));
}
function generators() {
const types = props(schema, {$ref: '#/definitions/Generator'});
const items = Object.keys(types).filter(t => t !== 'name');
return reduce(items, v => generator(v));
}
function marks() {
return reduce(markTypes, v => mark(v), k => `mark${capitalize(k)}`);
}
function channels() {
const items = props(schema, {$ref: '#/definitions/FacetedEncoding'});
return reduce(items, v => channel(v));
}
// retrieve selection types from VL schema
const selTypes = types(
schema,
{ $ref: '#/definitions/TopLevelSelectionParameter/properties/select' }
);
function selectionConfigs() {
return reduce(selTypes, v => selectionConfig(v), v => `config${capitalize(v)}`);
}
function selections() {
return reduce(selTypes, v => selection(v), v => `select${capitalize(v)}`);
}
export const api = {
// top-level specifications
mark: unit(markTypes),
...marks(),
layer: layer('...layer'),
concat: spec('Concatenate charts.', 'TopLevelConcatSpec', '...concat'),
hconcat: spec('Horizontally concatenate charts.', 'TopLevelHConcatSpec', '...hconcat'),
vconcat: spec('Vertically concatenate charts.', 'TopLevelVConcatSpec', '...vconcat'),
_repeat: spec('Repeat charts.', 'TopLevelRepeatSpec', 'repeat', 'spec'),
_facet: spec('Facet charts.', 'TopLevelFacetSpec', 'facet', 'spec'),
spec: spec('Create an arbitrary Vega-Lite specification.', 'TopLevelSpec'),
// externally defined exports
$register: {
desc: 'Register Vega and Vega-Lite with the API.',
doc: 'Utilities',
arg: ['vega', 'vegalite', 'options'],
src: '__view__'
},
$render: {
desc: 'Render a provided Vega-Lite specification.',
doc: 'Utilities',
src: '__view__'
},
$vega: {
desc: 'Access the registered Vega instance.',
doc: 'Utilities',
src: '__view__',
name: '_vega'
},
$vegalite: {
desc: 'Access the registered Vega-Lite instance.',
doc: 'Utilities',
src: '__view__',
name: '_vegalite'
},
// data specification
data: data(),
urlData: source('url', ['url']),
inlineData: source('inline', ['values'], true),
...generators(),
...sources(),
...formats(),
lookupData: lookupData(),
lookupSelection: lookupSelection(),
// encoding channels
...channels(),
// cartographic projection
projection: projection(),
// references
field: field(),
fieldN: fieldType('nominal'),
fieldO: fieldType('ordinal'),
fieldQ: fieldType('quantitative'),
fieldT: fieldType('temporal'),
encoding: encoding(),
repeat: repeat(),
value: value(),
expr: expr(),
// parameters and selections
param: param(),
...selections(),
selectSingle: selectionDeprecated({ toggle: false }),
selectMulti: selectionDeprecated(),
...selectionConfigs(),
_selRef: selectionRef(),
// bindings
checkbox: binding('BindCheckbox', 'checkbox'),
menu: binding('BindRadioSelect', 'select', ['...options']),
radio: binding('BindRadioSelect', 'radio', ['...options']),
slider: binding('BindRange', 'range', ['min', 'max', 'step']),
// logical operations
not: not(),
and: logical('and'),
or: logical('or'),
// transforms
aggregate: transform('aggregate', 'AggregateTransform', '...aggregate'),
bin: transform('bin', 'BinTransform', 'field', ['bin', true]),
calculate: transform('calculate', 'CalculateTransform', 'calculate'),
density: transform('density', 'DensityTransform', 'density'),
filter: transform('filter', 'FilterTransform', 'filter'),
flatten: transform('flatten', 'FlattenTransform', '...flatten'),
fold: transform('fold', 'FoldTransform', '...fold'),
impute: transform('impute', 'ImputeTransform', 'impute', 'key'),
joinaggregate: transform('joinaggregate', 'JoinAggregateTransform', '...joinaggregate'),
join: transform('join', 'JoinAggregateTransform', '...joinaggregate'),
loess: transform('loess', 'LoessTransform', 'loess'),
lookup: transform('lookup', 'LookupTransform', 'lookup'),
pivot: transform('pivot', 'PivotTransform', 'pivot'),
quantile: transform('quantile', 'QuantileTransform', 'quantile'),
regression: transform('regression', 'RegressionTransform', 'regression'),
sample: transform('sample', 'SampleTransform', 'sample'),
stack: transform('stack', 'StackTransform', 'stack'),
timeUnit: transform('timeUnit', 'TimeUnitTransform', 'timeUnit', 'field'),
window: transform('window', 'WindowTransform', '...window'),
groupby: groupby(),
// operations
...apiOps(aggregateOps, aggregateOp, 'as'),
...apiOps(windowOps, windowOp, 'as'),
...apiOps(timeUnitOps, timeUnitOp, 'field', 'as')
};