-
Notifications
You must be signed in to change notification settings - Fork 616
/
pathoverlay.ts
187 lines (168 loc) · 5.91 KB
/
pathoverlay.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
import {SignalRef} from 'vega';
import {isObject} from 'vega-util';
import {Config} from '../config';
import {Encoding, normalizeEncoding} from '../encoding';
import {ExprRef} from '../expr';
import {AreaConfig, isMarkDef, LineConfig, Mark, MarkConfig, MarkDef} from '../mark';
import {GenericUnitSpec, NormalizedUnitSpec} from '../spec';
import {isUnitSpec} from '../spec/unit';
import {stack} from '../stack';
import {keys, omit, pick} from '../util';
import {NonFacetUnitNormalizer, NormalizeLayerOrUnit, NormalizerParams} from './base';
type UnitSpecWithPathOverlay = GenericUnitSpec<Encoding<string>, Mark | MarkDef<'line' | 'area' | 'rule' | 'trail'>>;
function dropLineAndPoint(markDef: MarkDef): MarkDef | Mark {
const {point: _point, line: _line, ...mark} = markDef;
return keys(mark).length > 1 ? mark : mark.type;
}
function dropLineAndPointFromConfig(config: Config<SignalRef>) {
for (const mark of ['line', 'area', 'rule', 'trail'] as const) {
if (config[mark]) {
config = {
...config,
// TODO: remove as any
[mark]: omit(config[mark], ['point', 'line'] as any)
};
}
}
return config;
}
function getPointOverlay(
markDef: MarkDef,
markConfig: LineConfig<ExprRef | SignalRef> = {},
encoding: Encoding<string>
): MarkConfig<ExprRef | SignalRef> {
if (markDef.point === 'transparent') {
return {opacity: 0};
} else if (markDef.point) {
// truthy : true or object
return isObject(markDef.point) ? markDef.point : {};
} else if (markDef.point !== undefined) {
// false or null
return null;
} else {
// undefined (not disabled)
if (markConfig.point || encoding.shape) {
// enable point overlay if config[mark].point is truthy or if encoding.shape is provided
return isObject(markConfig.point) ? markConfig.point : {};
}
// markDef.point is defined as falsy
return undefined;
}
}
function getLineOverlay(
markDef: MarkDef,
markConfig: AreaConfig<ExprRef | SignalRef> = {}
): MarkConfig<ExprRef | SignalRef> {
if (markDef.line) {
// true or object
return markDef.line === true ? {} : markDef.line;
} else if (markDef.line !== undefined) {
// false or null
return null;
} else {
// undefined (not disabled)
if (markConfig.line) {
// enable line overlay if config[mark].line is truthy
return markConfig.line === true ? {} : markConfig.line;
}
// markDef.point is defined as falsy
return undefined;
}
}
export class PathOverlayNormalizer implements NonFacetUnitNormalizer<UnitSpecWithPathOverlay> {
public name = 'path-overlay';
public hasMatchingType(spec: GenericUnitSpec<any, Mark | MarkDef>, config: Config): spec is UnitSpecWithPathOverlay {
if (isUnitSpec(spec)) {
const {mark, encoding} = spec;
const markDef = isMarkDef(mark) ? mark : {type: mark};
switch (markDef.type) {
case 'line':
case 'rule':
case 'trail':
return !!getPointOverlay(markDef, config[markDef.type], encoding);
case 'area':
return (
// false / null are also included as we want to remove the properties
!!getPointOverlay(markDef, config[markDef.type], encoding) ||
!!getLineOverlay(markDef, config[markDef.type])
);
}
}
return false;
}
public run(spec: UnitSpecWithPathOverlay, normParams: NormalizerParams, normalize: NormalizeLayerOrUnit) {
const {config} = normParams;
const {params, projection, mark, encoding: e, ...outerSpec} = spec;
// Need to call normalizeEncoding because we need the inferred types to correctly determine stack
const encoding = normalizeEncoding(e, config);
const markDef: MarkDef = isMarkDef(mark) ? mark : {type: mark};
const pointOverlay = getPointOverlay(markDef, config[markDef.type], encoding);
const lineOverlay = markDef.type === 'area' && getLineOverlay(markDef, config[markDef.type]);
const layer: NormalizedUnitSpec[] = [
{
...(params ? {params} : {}),
mark: dropLineAndPoint({
// TODO: extract this 0.7 to be shared with default opacity for point/tick/...
...(markDef.type === 'area' && markDef.opacity === undefined && markDef.fillOpacity === undefined
? {opacity: 0.7}
: {}),
...markDef
}),
// drop shape from encoding as this might be used to trigger point overlay
encoding: omit(encoding, ['shape'])
}
];
// FIXME: determine rules for applying selections.
// Need to copy stack config to overlayed layer
const stackProps = stack(markDef, encoding);
let overlayEncoding = encoding;
if (stackProps) {
const {fieldChannel: stackFieldChannel, offset} = stackProps;
overlayEncoding = {
...encoding,
[stackFieldChannel]: {
...encoding[stackFieldChannel],
...(offset ? {stack: offset} : {})
}
};
}
// overlay line layer should be on the edge of area but passing y2/x2 makes
// it as "rule" mark so that it draws unwanted vertical/horizontal lines.
// point overlay also should not have y2/x2 as it does not support.
overlayEncoding = omit(overlayEncoding, ['y2', 'x2']);
if (lineOverlay) {
layer.push({
...(projection ? {projection} : {}),
mark: {
type: 'line',
...pick(markDef, ['clip', 'interpolate', 'tension', 'tooltip']),
...lineOverlay
},
encoding: overlayEncoding
});
}
if (pointOverlay) {
layer.push({
...(projection ? {projection} : {}),
mark: {
type: 'point',
opacity: 1,
filled: true,
...pick(markDef, ['clip', 'tooltip']),
...pointOverlay
},
encoding: overlayEncoding
});
}
return normalize(
{
...outerSpec,
layer
},
{
...normParams,
config: dropLineAndPointFromConfig(config)
}
);
}
}