-
Notifications
You must be signed in to change notification settings - Fork 592
/
zoom.ts
115 lines (104 loc) · 3.23 KB
/
zoom.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
import {NewSignal} from 'vega';
import {parseSelector} from 'vega-event-selector';
import {stringValue} from 'vega-util';
import {SelectionComponent} from '.';
import {ScaleChannel, X, Y} from '../../channel';
import {UnitModel} from '../unit';
import {BRUSH as INTERVAL_BRUSH} from './interval';
import {SelectionProjection} from './project';
import {default as scalesCompiler, domain} from './scales';
import {SelectionCompiler} from '.';
const ANCHOR = '_zoom_anchor';
const DELTA = '_zoom_delta';
const zoom: SelectionCompiler<'interval'> = {
defined: selCmpt => {
return selCmpt.type === 'interval' && selCmpt.zoom;
},
signals: (model, selCmpt, signals) => {
const name = selCmpt.name;
const boundScales = scalesCompiler.defined(selCmpt);
const delta = name + DELTA;
const {x, y} = selCmpt.project.hasChannel;
const sx = stringValue(model.scaleName(X));
const sy = stringValue(model.scaleName(Y));
let events = parseSelector(selCmpt.zoom, 'scope');
if (!boundScales) {
events = events.map(e => ((e.markname = name + INTERVAL_BRUSH), e));
}
signals.push(
{
name: name + ANCHOR,
on: [
{
events,
update: !boundScales
? `{x: x(unit), y: y(unit)}`
: '{' +
[sx ? `x: invert(${sx}, x(unit))` : '', sy ? `y: invert(${sy}, y(unit))` : '']
.filter(expr => expr)
.join(', ') +
'}'
}
]
},
{
name: delta,
on: [
{
events,
force: true,
update: 'pow(1.001, event.deltaY * pow(16, event.deltaMode))'
}
]
}
);
if (x !== undefined) {
onDelta(model, selCmpt, x, 'width', signals);
}
if (y !== undefined) {
onDelta(model, selCmpt, y, 'height', signals);
}
return signals;
}
};
export default zoom;
function onDelta(
model: UnitModel,
selCmpt: SelectionComponent,
proj: SelectionProjection,
size: 'width' | 'height',
signals: NewSignal[]
) {
const name = selCmpt.name;
const channel = proj.channel as ScaleChannel;
const boundScales = scalesCompiler.defined(selCmpt);
const signal = signals.filter(s => s.name === proj.signals[boundScales ? 'data' : 'visual'])[0];
const sizeSg = model.getSizeSignalRef(size).signal;
const scaleCmpt = model.getScaleComponent(channel);
const scaleType = scaleCmpt && scaleCmpt.get('type');
const base = boundScales ? domain(model, channel) : signal.name;
const delta = name + DELTA;
const anchor = `${name}${ANCHOR}.${channel}`;
const zoomFn =
!boundScales || !scaleCmpt
? 'zoomLinear'
: scaleType === 'log'
? 'zoomLog'
: scaleType === 'symlog'
? 'zoomSymlog'
: scaleType === 'pow'
? 'zoomPow'
: 'zoomLinear';
const arg = !boundScales
? ''
: scaleType === 'pow'
? `, ${scaleCmpt.get('exponent') ?? 1}`
: scaleType === 'symlog'
? `, ${scaleCmpt.get('constant') ?? 1}`
: '';
const update = `${zoomFn}(${base}, ${anchor}, ${delta}${arg})`;
signal.on.push({
events: {signal: delta},
update: boundScales ? update : `clampRange(${update}, 0, ${sizeSg})`
});
}