/
properties.ts
458 lines (415 loc) · 15.5 KB
/
properties.ts
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import {SignalRef, TimeInterval} from 'vega';
import {isArray} from 'vega-util';
import {isBinned, isBinning, isBinParams} from '../../bin';
import {
COLOR,
FILL,
getSecondaryRangeChannel,
isXorY,
isXorYOffset,
POLAR_POSITION_SCALE_CHANNELS,
POSITION_SCALE_CHANNELS,
ScaleChannel,
STROKE
} from '../../channel';
import {
getFieldDef,
getFieldOrDatumDef,
isFieldDef,
ScaleDatumDef,
ScaleFieldDef,
TypedFieldDef,
valueExpr
} from '../../channeldef';
import {Config} from '../../config';
import {isDateTime} from '../../datetime';
import {channelHasNestedOffsetScale} from '../../encoding';
import * as log from '../../log';
import {Mark, MarkDef, RectConfig} from '../../mark';
import {
channelScalePropertyIncompatability,
Domain,
hasContinuousDomain,
isContinuousToContinuous,
isContinuousToDiscrete,
Scale,
ScaleConfig,
ScaleType,
scaleTypeSupportProperty
} from '../../scale';
import {Sort} from '../../sort';
import {Type} from '../../type';
import * as util from '../../util';
import {contains, getFirstDefined, keys} from '../../util';
import {isSignalRef, VgScale} from '../../vega.schema';
import {getBinSignalName} from '../data/bin';
import {isUnitModel, Model} from '../model';
import {SignalRefWrapper} from '../signal';
import {Explicit, mergeValuesWithExplicit, tieBreakByComparing} from '../split';
import {UnitModel} from '../unit';
import {ScaleComponentIndex, ScaleComponentProps} from './component';
import {parseUnitScaleRange} from './range';
export function parseScaleProperty(model: Model, property: Exclude<keyof (Scale | ScaleComponentProps), 'range'>) {
if (isUnitModel(model)) {
parseUnitScaleProperty(model, property);
} else {
parseNonUnitScaleProperty(model, property);
}
}
function parseUnitScaleProperty(model: UnitModel, property: Exclude<keyof (Scale | ScaleComponentProps), 'range'>) {
const localScaleComponents: ScaleComponentIndex = model.component.scales;
const {config, encoding, markDef, specifiedScales} = model;
for (const channel of keys(localScaleComponents)) {
const specifiedScale = specifiedScales[channel];
const localScaleCmpt = localScaleComponents[channel];
const mergedScaleCmpt = model.getScaleComponent(channel);
const fieldOrDatumDef = getFieldOrDatumDef(encoding[channel]) as ScaleFieldDef<string, Type> | ScaleDatumDef;
const specifiedValue = specifiedScale[property];
const scaleType = mergedScaleCmpt.get('type');
const scalePadding = mergedScaleCmpt.get('padding');
const scalePaddingInner = mergedScaleCmpt.get('paddingInner');
const supportedByScaleType = scaleTypeSupportProperty(scaleType, property);
const channelIncompatability = channelScalePropertyIncompatability(channel, property);
if (specifiedValue !== undefined) {
// If there is a specified value, check if it is compatible with scale type and channel
if (!supportedByScaleType) {
log.warn(log.message.scalePropertyNotWorkWithScaleType(scaleType, property, channel));
} else if (channelIncompatability) {
// channel
log.warn(channelIncompatability);
}
}
if (supportedByScaleType && channelIncompatability === undefined) {
if (specifiedValue !== undefined) {
const timeUnit = fieldOrDatumDef['timeUnit'];
const type = fieldOrDatumDef.type;
switch (property) {
// domainMax/Min to signal if the value is a datetime object
case 'domainMax':
case 'domainMin':
if (isDateTime(specifiedScale[property]) || type === 'temporal' || timeUnit) {
localScaleCmpt.set(property, {signal: valueExpr(specifiedScale[property], {type, timeUnit})}, true);
} else {
localScaleCmpt.set(property, specifiedScale[property] as any, true);
}
break;
default:
localScaleCmpt.copyKeyFromObject<Omit<ScaleComponentProps, 'range' | 'domainMin' | 'domainMax'>>(
property,
specifiedScale
);
}
} else {
const value =
property in scaleRules
? scaleRules[property]({
model,
channel,
fieldOrDatumDef,
scaleType,
scalePadding,
scalePaddingInner,
domain: specifiedScale.domain,
domainMin: specifiedScale.domainMin,
domainMax: specifiedScale.domainMax,
markDef,
config,
hasNestedOffsetScale: channelHasNestedOffsetScale(encoding, channel),
hasSecondaryRangeChannel: !!encoding[getSecondaryRangeChannel(channel)]
})
: config.scale[property];
if (value !== undefined) {
localScaleCmpt.set(property, value, false);
}
}
}
}
}
export interface ScaleRuleParams {
model: Model;
channel: ScaleChannel;
fieldOrDatumDef: ScaleFieldDef<string, Type> | ScaleDatumDef;
hasNestedOffsetScale: boolean;
scaleType: ScaleType;
scalePadding: number | SignalRef;
scalePaddingInner: number | SignalRef;
domain: Domain;
domainMin: Scale['domainMin'];
domainMax: Scale['domainMax'];
markDef: MarkDef<Mark, SignalRef>;
config: Config<SignalRef>;
hasSecondaryRangeChannel: boolean;
}
export const scaleRules: {
[k in keyof Scale]?: (params: ScaleRuleParams) => Scale[k];
} = {
bins: ({model, fieldOrDatumDef}) => (isFieldDef(fieldOrDatumDef) ? bins(model, fieldOrDatumDef) : undefined),
interpolate: ({channel, fieldOrDatumDef}) => interpolate(channel, fieldOrDatumDef.type),
nice: ({scaleType, channel, domain, domainMin, domainMax, fieldOrDatumDef}) =>
nice(scaleType, channel, domain, domainMin, domainMax, fieldOrDatumDef),
padding: ({channel, scaleType, fieldOrDatumDef, markDef, config}) =>
padding(channel, scaleType, config.scale, fieldOrDatumDef, markDef, config.bar),
paddingInner: ({scalePadding, channel, markDef, scaleType, config, hasNestedOffsetScale}) =>
paddingInner(scalePadding, channel, markDef.type, scaleType, config.scale, hasNestedOffsetScale),
paddingOuter: ({scalePadding, channel, scaleType, scalePaddingInner, config, hasNestedOffsetScale}) =>
paddingOuter(scalePadding, channel, scaleType, scalePaddingInner, config.scale, hasNestedOffsetScale),
reverse: ({fieldOrDatumDef, scaleType, channel, config}) => {
const sort = isFieldDef(fieldOrDatumDef) ? fieldOrDatumDef.sort : undefined;
return reverse(scaleType, sort, channel, config.scale);
},
zero: ({channel, fieldOrDatumDef, domain, markDef, scaleType, config, hasSecondaryRangeChannel}) =>
zero(channel, fieldOrDatumDef, domain, markDef, scaleType, config.scale, hasSecondaryRangeChannel)
};
// This method is here rather than in range.ts to avoid circular dependency.
export function parseScaleRange(model: Model) {
if (isUnitModel(model)) {
parseUnitScaleRange(model);
} else {
parseNonUnitScaleProperty(model, 'range');
}
}
export function parseNonUnitScaleProperty(model: Model, property: keyof (Scale | ScaleComponentProps)) {
const localScaleComponents: ScaleComponentIndex = model.component.scales;
for (const child of model.children) {
if (property === 'range') {
parseScaleRange(child);
} else {
parseScaleProperty(child, property);
}
}
for (const channel of keys(localScaleComponents)) {
let valueWithExplicit: Explicit<any>;
for (const child of model.children) {
const childComponent = child.component.scales[channel];
if (childComponent) {
const childValueWithExplicit = childComponent.getWithExplicit(property);
valueWithExplicit = mergeValuesWithExplicit<VgScale, any>(
valueWithExplicit,
childValueWithExplicit,
property,
'scale',
tieBreakByComparing<VgScale, any>((v1, v2) => {
switch (property) {
case 'range':
// For step, prefer larger step
if (v1.step && v2.step) {
return v1.step - v2.step;
}
return 0;
// TODO: precedence rule for other properties
}
return 0;
})
);
}
}
localScaleComponents[channel].setWithExplicit(property, valueWithExplicit);
}
}
export function bins(model: Model, fieldDef: TypedFieldDef<string>) {
const bin = fieldDef.bin;
if (isBinning(bin)) {
const binSignal = getBinSignalName(model, fieldDef.field, bin);
return new SignalRefWrapper(() => {
return model.getSignalName(binSignal);
});
} else if (isBinned(bin) && isBinParams(bin) && bin.step !== undefined) {
// start and stop will be determined from the scale domain
return {
step: bin.step
};
}
return undefined;
}
export function interpolate(channel: ScaleChannel, type: Type): Scale['interpolate'] {
if (contains([COLOR, FILL, STROKE], channel) && type !== 'nominal') {
return 'hcl';
}
return undefined;
}
export function nice(
scaleType: ScaleType,
channel: ScaleChannel,
specifiedDomain: Domain,
domainMin: Scale['domainMin'],
domainMax: Scale['domainMax'],
fieldOrDatumDef: TypedFieldDef<string> | ScaleDatumDef
): boolean | TimeInterval {
if (
getFieldDef(fieldOrDatumDef)?.bin ||
isArray(specifiedDomain) ||
domainMax != null ||
domainMin != null ||
util.contains([ScaleType.TIME, ScaleType.UTC], scaleType)
) {
return undefined;
}
return isXorY(channel) ? true : undefined;
}
export function padding(
channel: ScaleChannel,
scaleType: ScaleType,
scaleConfig: ScaleConfig<SignalRef>,
fieldOrDatumDef: TypedFieldDef<string> | ScaleDatumDef,
markDef: MarkDef<Mark, SignalRef>,
barConfig: RectConfig<SignalRef>
) {
if (isXorY(channel)) {
if (isContinuousToContinuous(scaleType)) {
if (scaleConfig.continuousPadding !== undefined) {
return scaleConfig.continuousPadding;
}
const {type, orient} = markDef;
if (type === 'bar' && !(isFieldDef(fieldOrDatumDef) && (fieldOrDatumDef.bin || fieldOrDatumDef.timeUnit))) {
if ((orient === 'vertical' && channel === 'x') || (orient === 'horizontal' && channel === 'y')) {
return barConfig.continuousBandSize;
}
}
}
if (scaleType === ScaleType.POINT) {
return scaleConfig.pointPadding;
}
}
return undefined;
}
export function paddingInner(
paddingValue: number | SignalRef,
channel: ScaleChannel,
mark: Mark,
scaleType: ScaleType,
scaleConfig: ScaleConfig<SignalRef>,
hasNestedOffsetScale = false
) {
if (paddingValue !== undefined) {
// If user has already manually specified "padding", no need to add default paddingInner.
return undefined;
}
if (isXorY(channel)) {
// Padding is only set for X and Y by default.
// Basically it doesn't make sense to add padding for color and size.
// paddingOuter would only be called if it's a band scale, just return the default for bandScale.
const {bandPaddingInner, barBandPaddingInner, rectBandPaddingInner, bandWithNestedOffsetPaddingInner} = scaleConfig;
if (hasNestedOffsetScale) {
return bandWithNestedOffsetPaddingInner;
}
return getFirstDefined(bandPaddingInner, mark === 'bar' ? barBandPaddingInner : rectBandPaddingInner);
} else if (isXorYOffset(channel)) {
if (scaleType === ScaleType.BAND) {
return scaleConfig.offsetBandPaddingInner;
}
}
return undefined;
}
export function paddingOuter(
paddingValue: number | SignalRef,
channel: ScaleChannel,
scaleType: ScaleType,
paddingInnerValue: number | SignalRef,
scaleConfig: ScaleConfig<SignalRef>,
hasNestedOffsetScale = false
) {
if (paddingValue !== undefined) {
// If user has already manually specified "padding", no need to add default paddingOuter.
return undefined;
}
if (isXorY(channel)) {
const {bandPaddingOuter, bandWithNestedOffsetPaddingOuter} = scaleConfig;
if (hasNestedOffsetScale) {
return bandWithNestedOffsetPaddingOuter;
}
// Padding is only set for X and Y by default.
// Basically it doesn't make sense to add padding for color and size.
if (scaleType === ScaleType.BAND) {
return getFirstDefined(
bandPaddingOuter,
/* By default, paddingOuter is paddingInner / 2. The reason is that
size (width/height) = step * (cardinality - paddingInner + 2 * paddingOuter).
and we want the width/height to be integer by default.
Note that step (by default) and cardinality are integers.) */
isSignalRef(paddingInnerValue) ? {signal: `${paddingInnerValue.signal}/2`} : paddingInnerValue / 2
);
}
} else if (isXorYOffset(channel)) {
if (scaleType === ScaleType.POINT) {
return 0.5; // so the point positions align with centers of band scales.
} else if (scaleType === ScaleType.BAND) {
return scaleConfig.offsetBandPaddingOuter;
}
}
return undefined;
}
export function reverse(
scaleType: ScaleType,
sort: Sort<string>,
channel: ScaleChannel,
scaleConfig: ScaleConfig<SignalRef>
) {
if (channel === 'x' && scaleConfig.xReverse !== undefined) {
if (hasContinuousDomain(scaleType) && sort === 'descending') {
if (isSignalRef(scaleConfig.xReverse)) {
return {signal: `!${scaleConfig.xReverse.signal}`};
} else {
return !scaleConfig.xReverse;
}
}
return scaleConfig.xReverse;
}
if (hasContinuousDomain(scaleType) && sort === 'descending') {
// For continuous domain scales, Vega does not support domain sort.
// Thus, we reverse range instead if sort is descending
return true;
}
return undefined;
}
export function zero(
channel: ScaleChannel,
fieldDef: TypedFieldDef<string> | ScaleDatumDef,
specifiedDomain: Domain,
markDef: MarkDef,
scaleType: ScaleType,
scaleConfig: ScaleConfig<SignalRef>,
hasSecondaryRangeChannel: boolean
) {
// If users explicitly provide a domain, we should not augment zero as that will be unexpected.
const hasCustomDomain = !!specifiedDomain && specifiedDomain !== 'unaggregated';
if (hasCustomDomain) {
if (hasContinuousDomain(scaleType)) {
if (isArray(specifiedDomain)) {
const first = specifiedDomain[0];
const last = specifiedDomain[specifiedDomain.length - 1];
if (first <= 0 && last >= 0) {
// if the domain includes zero, make zero remains true
return true;
}
}
return false;
}
}
// If there is no custom domain, return configZero value (=`true` as default) only for the following cases:
// 1) using quantitative field with size
// While this can be either ratio or interval fields, our assumption is that
// ratio are more common. However, if the scaleType is discretizing scale, we want to return
// false so that range doesn't start at zero
if (channel === 'size' && fieldDef.type === 'quantitative' && !isContinuousToDiscrete(scaleType)) {
return true;
}
// 2) non-binned, quantitative x-scale or y-scale
// (For binning, we should not include zero by default because binning are calculated without zero.)
// (For area/bar charts with ratio scale chart, we should always include zero.)
if (
!(isFieldDef(fieldDef) && fieldDef.bin) &&
util.contains([...POSITION_SCALE_CHANNELS, ...POLAR_POSITION_SCALE_CHANNELS], channel)
) {
const {orient, type} = markDef;
if (contains(['bar', 'area', 'line', 'trail'], type)) {
if ((orient === 'horizontal' && channel === 'y') || (orient === 'vertical' && channel === 'x')) {
return false;
}
}
if (contains(['bar', 'area'], type) && !hasSecondaryRangeChannel) {
return true;
}
return scaleConfig?.zero;
}
return false;
}