-
Notifications
You must be signed in to change notification settings - Fork 592
/
mixins.js
377 lines (377 loc) · 16.2 KB
/
mixins.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
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
import { array, isArray, isObject, isString } from 'vega-util';
import { isBinned, isBinning } from '../../bin';
import { SCALE_CHANNELS, X, X2, Y2 } from '../../channel';
import { getTypedFieldDef, isConditionalSelection, isFieldDef, isValueDef } from '../../channeldef';
import * as log from '../../log';
import { isPathMark } from '../../mark';
import { hasContinuousDomain } from '../../scale';
import { contains, getFirstDefined, keys } from '../../util';
import { VG_MARK_CONFIGS } from '../../vega.schema';
import { getMarkConfig } from '../common';
import { expression } from '../predicate';
import { assembleSelectionPredicate } from '../selection/assemble';
import * as ref from './valueref';
import { fieldInvalidPredicate } from './valueref';
function isVisible(c) {
return c !== 'transparent' && c !== null && c !== undefined;
}
export function color(model) {
const { markDef, encoding, config } = model;
const { filled, type: markType } = markDef;
const configValue = {
fill: getMarkConfig('fill', markDef, config),
stroke: getMarkConfig('stroke', markDef, config),
color: getMarkConfig('color', markDef, config)
};
const transparentIfNeeded = contains(['bar', 'point', 'circle', 'square', 'geoshape'], markType)
? 'transparent'
: undefined;
const defaultFill = getFirstDefined(markDef.fill, configValue.fill,
// If there is no fill, always fill symbols, bar, geoshape
// with transparent fills https://github.com/vega/vega-lite/issues/1316
transparentIfNeeded);
const defaultStroke = getFirstDefined(markDef.stroke, configValue.stroke);
const colorVgChannel = filled ? 'fill' : 'stroke';
const fillStrokeMarkDefAndConfig = Object.assign({}, (defaultFill ? { fill: { value: defaultFill } } : {}), (defaultStroke ? { stroke: { value: defaultStroke } } : {}));
if (encoding.fill || encoding.stroke) {
// ignore encoding.color, markDef.color, config.color
if (markDef.color) {
// warn for markDef.color (no need to warn encoding.color as it will be dropped in normalized already)
log.warn(log.message.droppingColor('property', { fill: 'fill' in encoding, stroke: 'stroke' in encoding }));
}
return Object.assign({}, nonPosition('fill', model, { defaultValue: getFirstDefined(defaultFill, transparentIfNeeded) }), nonPosition('stroke', model, { defaultValue: defaultStroke }));
}
else if (encoding.color) {
return Object.assign({}, fillStrokeMarkDefAndConfig, nonPosition('color', model, {
vgChannel: colorVgChannel,
// apply default fill/stroke first, then color config, then transparent if needed.
defaultValue: getFirstDefined(markDef[colorVgChannel], markDef.color, configValue[colorVgChannel], configValue.color, filled ? transparentIfNeeded : undefined)
}));
}
else if (isVisible(markDef.fill) || isVisible(markDef.stroke)) {
// Ignore markDef.color
if (markDef.color) {
log.warn(log.message.droppingColor('property', { fill: 'fill' in markDef, stroke: 'stroke' in markDef }));
}
return fillStrokeMarkDefAndConfig;
}
else if (markDef.color) {
return Object.assign({}, fillStrokeMarkDefAndConfig, {
// override config with markDef.color
[colorVgChannel]: { value: markDef.color } });
}
else if (isVisible(configValue.fill) || isVisible(configValue.stroke)) {
// ignore config.color
return fillStrokeMarkDefAndConfig;
}
else if (configValue.color) {
return Object.assign({}, (transparentIfNeeded ? { fill: { value: 'transparent' } } : {}), { [colorVgChannel]: { value: configValue.color } });
}
return {};
}
export function baseEncodeEntry(model, ignore) {
const { fill, stroke } = color(model);
return Object.assign({}, markDefProperties(model.markDef, ignore), wrapAllFieldsInvalid(model, 'fill', fill), wrapAllFieldsInvalid(model, 'stroke', stroke), nonPosition('opacity', model), nonPosition('fillOpacity', model), nonPosition('strokeOpacity', model), nonPosition('strokeWidth', model), tooltip(model), text(model, 'href'));
}
function wrapAllFieldsInvalid(model, channel, valueRef) {
const { config, mark } = model;
if (config.invalidValues === 'hide' && valueRef && !isPathMark(mark)) {
// For non-path marks, we have to exclude invalid values (null and NaN) for scales with continuous domains.
// For path marks, we will use "defined" property and skip these values instead.
const test = allFieldsInvalidPredicate(model, { invalid: true, channels: SCALE_CHANNELS });
if (test) {
return {
[channel]: [
// prepend the invalid case
// TODO: support custom value
{ test, value: null },
...array(valueRef)
]
};
}
}
return valueRef ? { [channel]: valueRef } : {};
}
function markDefProperties(mark, ignore) {
return VG_MARK_CONFIGS.reduce((m, prop) => {
if (mark[prop] !== undefined && ignore[prop] !== 'ignore') {
m[prop] = { value: mark[prop] };
}
return m;
}, {});
}
export function valueIfDefined(prop, value) {
if (value !== undefined) {
return { [prop]: { value: value } };
}
return undefined;
}
function allFieldsInvalidPredicate(model, { invalid = false, channels }) {
const filterIndex = channels.reduce((aggregator, channel) => {
const scaleComponent = model.getScaleComponent(channel);
if (scaleComponent) {
const scaleType = scaleComponent.get('type');
const field = model.vgField(channel, { expr: 'datum' });
// While discrete domain scales can handle invalid values, continuous scales can't.
if (field && hasContinuousDomain(scaleType)) {
aggregator[field] = true;
}
}
return aggregator;
}, {});
const fields = keys(filterIndex);
if (fields.length > 0) {
const op = invalid ? '||' : '&&';
return fields.map(field => fieldInvalidPredicate(field, invalid)).join(` ${op} `);
}
return undefined;
}
export function defined(model) {
if (model.config.invalidValues) {
const signal = allFieldsInvalidPredicate(model, { channels: ['x', 'y'] });
if (signal) {
return { defined: { signal } };
}
}
return {};
}
/**
* Return mixins for non-positional channels with scales. (Text doesn't have scale.)
*/
export function nonPosition(channel, model, opt = {}) {
const { markDef, encoding, config } = model;
const { vgChannel = channel } = opt;
let { defaultRef, defaultValue } = opt;
if (defaultRef === undefined) {
// prettier-ignore
defaultValue = defaultValue ||
(vgChannel === channel
? // When vl channel is the same as Vega's, no need to read from config as Vega will apply them correctly
markDef[channel]
: // However, when they are different (e.g, vl's text size is vg fontSize), need to read "size" from configs
getFirstDefined(markDef[channel], markDef[vgChannel], getMarkConfig(channel, markDef, config, { vgChannel })));
defaultRef = defaultValue ? { value: defaultValue } : undefined;
}
const channelDef = encoding[channel];
return wrapCondition(model, channelDef, vgChannel, cDef => {
return ref.midPoint({
channel,
channelDef: cDef,
scaleName: model.scaleName(channel),
scale: model.getScaleComponent(channel),
stack: null,
defaultRef
});
});
}
/**
* Return a mixin that include a Vega production rule for a Vega-Lite conditional channel definition.
* or a simple mixin if channel def has no condition.
*/
export function wrapCondition(model, channelDef, vgChannel, refFn) {
const condition = channelDef && channelDef.condition;
const valueRef = refFn(channelDef);
if (condition) {
const conditions = isArray(condition) ? condition : [condition];
const vgConditions = conditions.map(c => {
const conditionValueRef = refFn(c);
const test = isConditionalSelection(c)
? assembleSelectionPredicate(model, c.selection)
: expression(model, c.test);
return Object.assign({ test }, conditionValueRef);
});
return {
[vgChannel]: [...vgConditions, ...(valueRef !== undefined ? [valueRef] : [])]
};
}
else {
return valueRef !== undefined ? { [vgChannel]: valueRef } : {};
}
}
export function tooltip(model, opt = {}) {
const { encoding, markDef, config } = model;
const channelDef = encoding.tooltip;
if (isArray(channelDef)) {
return { tooltip: ref.tooltipForEncoding({ tooltip: channelDef }, config, opt) };
}
else {
return wrapCondition(model, channelDef, 'tooltip', cDef => {
// use valueRef based on channelDef first
const tooltipRefFromChannelDef = ref.text(cDef, model.config);
if (tooltipRefFromChannelDef) {
return tooltipRefFromChannelDef;
}
if (cDef === null) {
// Allow using encoding.tooltip = null to disable tooltip
return undefined;
}
// If tooltipDef does not exist, then use value from markDef or config
const markTooltip = getFirstDefined(markDef.tooltip, getMarkConfig('tooltip', markDef, config));
if (isString(markTooltip)) {
return { value: markTooltip };
}
else if (isObject(markTooltip)) {
// `tooltip` is `{fields: 'encodings' | 'fields'}`
if (markTooltip.content === 'encoding') {
return ref.tooltipForEncoding(encoding, config, opt);
}
else {
return { signal: 'datum' };
}
}
return undefined;
});
}
}
export function text(model, channel = 'text') {
const channelDef = model.encoding[channel];
return wrapCondition(model, channelDef, channel, cDef => ref.text(cDef, model.config));
}
export function bandPosition(fieldDef, channel, model, defaultSizeRef) {
const scaleName = model.scaleName(channel);
const sizeChannel = channel === 'x' ? 'width' : 'height';
if (model.encoding.size ||
model.markDef.size !== undefined ||
(defaultSizeRef && defaultSizeRef.value !== undefined)) {
const orient = model.markDef.orient;
if (orient) {
const centeredBandPositionMixins = {
// Use xc/yc and place the mark at the middle of the band
// This way we never have to deal with size's condition for x/y position.
[channel + 'c']: ref.fieldRef(fieldDef, scaleName, {}, { band: 0.5 })
};
if (getTypedFieldDef(model.encoding.size)) {
return Object.assign({}, centeredBandPositionMixins, nonPosition('size', model, { vgChannel: sizeChannel }));
}
else if (isValueDef(model.encoding.size)) {
return Object.assign({}, centeredBandPositionMixins, nonPosition('size', model, { vgChannel: sizeChannel }));
}
else if (model.markDef.size !== undefined) {
return Object.assign({}, centeredBandPositionMixins, { [sizeChannel]: { value: model.markDef.size } });
}
else if (defaultSizeRef && defaultSizeRef.value !== undefined) {
return Object.assign({}, centeredBandPositionMixins, { [sizeChannel]: defaultSizeRef });
}
}
else {
log.warn(log.message.cannotApplySizeToNonOrientedMark(model.markDef.type));
}
}
return {
// FIXME: make offset works correctly here when we support group bar (https://github.com/vega/vega-lite/issues/396)
[channel]: ref.fieldRef(fieldDef, scaleName, { binSuffix: 'range' }, {}),
[sizeChannel]: defaultSizeRef || ref.bandRef(scaleName)
};
}
export function centeredPointPositionWithSize(channel, model, defaultPosRef, defaultSizeRef) {
const centerChannel = channel === 'x' ? 'xc' : 'yc';
const sizeChannel = channel === 'x' ? 'width' : 'height';
return Object.assign({}, pointPosition(channel, model, defaultPosRef, centerChannel), nonPosition('size', model, { defaultRef: defaultSizeRef, vgChannel: sizeChannel }));
}
export function binPosition({ fieldDef, fieldDef2, channel, scaleName, mark, spacing = 0, reverse }) {
const binSpacing = {
x: reverse ? spacing : 0,
x2: reverse ? 0 : spacing,
y: reverse ? 0 : spacing,
y2: reverse ? spacing : 0
};
const channel2 = channel === X ? X2 : Y2;
if (isBinning(fieldDef.bin)) {
return {
[channel2]: ref.bin({
channel,
fieldDef,
scaleName,
mark,
side: 'start',
offset: binSpacing[`${channel}2`]
}),
[channel]: ref.bin({ channel, fieldDef, scaleName, mark, side: 'end', offset: binSpacing[channel] })
};
}
else if (isBinned(fieldDef.bin) && isFieldDef(fieldDef2)) {
return {
[channel2]: ref.fieldRef(fieldDef, scaleName, {}, { offset: binSpacing[`${channel}2`] }),
[channel]: ref.fieldRef(fieldDef2, scaleName, {}, { offset: binSpacing[channel] })
};
}
else {
log.warn(log.message.channelRequiredForBinned(channel2));
return undefined;
}
}
/**
* Return mixins for point (non-band) position channels.
*/
export function pointPosition(channel, model, defaultRef, vgChannel) {
// TODO: refactor how refer to scale as discussed in https://github.com/vega/vega-lite/pull/1613
const { encoding, mark, markDef, config, stack } = model;
const channelDef = encoding[channel];
const channel2Def = encoding[channel === X ? X2 : Y2];
const scaleName = model.scaleName(channel);
const scale = model.getScaleComponent(channel);
const offset = ref.getOffset(channel, model.markDef);
const valueRef = !channelDef && (encoding.latitude || encoding.longitude)
? // use geopoint output if there are lat/long and there is no point position overriding lat/long.
{ field: model.getName(channel) }
: ref.position({
channel,
channelDef,
channel2Def,
scaleName,
scale,
stack,
mark,
offset,
defaultRef: ref.positionDefault({
markDef,
config,
defaultRef,
channel,
scaleName,
scale,
mark,
checkBarAreaWithoutZero: !channel2Def // only check for non-ranged marks
})
});
return {
[vgChannel || channel]: valueRef
};
}
/**
* Return mixins for x2, y2.
* If channel is not specified, return one channel based on orientation.
*/
export function pointPosition2(model, defaultRef, channel) {
const { encoding, mark, markDef, stack, config } = model;
const baseChannel = channel === 'x2' ? 'x' : 'y';
const channelDef = encoding[baseChannel];
const scaleName = model.scaleName(baseChannel);
const scale = model.getScaleComponent(baseChannel);
const offset = ref.getOffset(channel, model.markDef);
const valueRef = !channelDef && (encoding.latitude || encoding.longitude)
? // use geopoint output if there are lat2/long2 and there is no point position2 overriding lat2/long2.
{ field: model.getName(channel) }
: ref.position2({
channel,
channelDef,
channel2Def: encoding[channel],
scaleName,
scale,
stack,
mark,
offset,
defaultRef: ref.positionDefault({
markDef,
config,
defaultRef,
channel,
scaleName,
scale,
mark,
checkBarAreaWithoutZero: !encoding[channel] // only check for non-ranged marks
})
});
return { [channel]: valueRef };
}
//# sourceMappingURL=mixins.js.map