-
Notifications
You must be signed in to change notification settings - Fork 19.8k
Expand file tree
/
Copy pathInsideZoomView.ts
More file actions
332 lines (286 loc) · 11.4 KB
/
InsideZoomView.ts
File metadata and controls
332 lines (286 loc) · 11.4 KB
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import DataZoomView from './DataZoomView';
import sliderMove from '../helper/sliderMove';
import * as roams from './roams';
import InsideZoomModel from './InsideZoomModel';
import GlobalModel from '../../model/Global';
import ExtensionAPI from '../../core/ExtensionAPI';
import { bind } from 'zrender/src/core/util';
import RoamController, {RoamEventParams, WheelAxisType} from '../helper/RoamController';
import { AxisBaseModel } from '../../coord/AxisBaseModel';
import Polar from '../../coord/polar/Polar';
import SingleAxis from '../../coord/single/SingleAxis';
import { DataZoomCoordSysMainType, DataZoomReferCoordSysInfo } from './helper';
class InsideZoomView extends DataZoomView {
static type = 'dataZoom.inside';
type = 'dataZoom.inside';
/**
* 'throttle' is used in this.dispatchAction, so we save range
* to avoid missing some 'pan' info.
*/
range: number[];
render(dataZoomModel: InsideZoomModel, ecModel: GlobalModel, api: ExtensionAPI) {
super.render.apply(this, arguments as any);
if (dataZoomModel.noTarget()) {
this._clear();
return;
}
// Hence the `throttle` util ensures to preserve command order,
// here simply updating range all the time will not cause missing
// any of the the roam change.
this.range = dataZoomModel.getPercentRange();
// Reset controllers.
roams.setViewInfoToCoordSysRecord(
api,
dataZoomModel,
{
pan: bind(getRangeHandlers.pan, this),
zoom: bind(getRangeHandlers.zoom, this),
scrollMove: bind(getRangeHandlers.scrollMove, this)
}
);
}
dispose() {
this._clear();
super.dispose.apply(this, arguments as any);
}
private _clear() {
roams.disposeCoordSysRecordIfNeeded(this.api, this.dataZoomModel as InsideZoomModel);
this.range = null;
}
}
interface DataZoomGetRangeHandler<
T extends RoamEventParams['zoom'] | RoamEventParams['scrollMove'] | RoamEventParams['pan']
> {
(
coordSysInfo: DataZoomReferCoordSysInfo,
coordSysMainType: DataZoomCoordSysMainType,
controller: RoamController,
e: T
): [number, number]
}
const getRangeHandlers: {
pan: DataZoomGetRangeHandler<RoamEventParams['pan']>
zoom: DataZoomGetRangeHandler<RoamEventParams['zoom']>
scrollMove: DataZoomGetRangeHandler<RoamEventParams['scrollMove']>
} & ThisType<InsideZoomView> = {
zoom(coordSysInfo, coordSysMainType, controller, e: RoamEventParams['zoom']) {
const lastRange = this.range;
const range = lastRange.slice() as [number, number];
// Calculate transform by the first axis.
const axisModel = coordSysInfo.axisModels[0];
if (!axisModel) {
return;
}
// `zoomOnMouseWheelAxis` restricts the zoom to one wheel axis;
// unset falls back to the combined `scale` for backward
// compatibility.
const effectiveScale = pickWheelAxisValue(
(this.dataZoomModel as InsideZoomModel).get('zoomOnMouseWheelAxis', true),
e.scaleX, e.scaleY, e.scale
);
const directionInfo = getDirectionInfo[coordSysMainType](
null, [e.originX, e.originY], axisModel, controller, coordSysInfo
);
const percentPoint = (
directionInfo.signal > 0
? (directionInfo.pixelStart + directionInfo.pixelLength - directionInfo.pixel)
: (directionInfo.pixel - directionInfo.pixelStart)
) / directionInfo.pixelLength * (range[1] - range[0]) + range[0];
const scale = Math.max(1 / effectiveScale, 0);
range[0] = (range[0] - percentPoint) * scale + percentPoint;
range[1] = (range[1] - percentPoint) * scale + percentPoint;
// Restrict range.
const minMaxSpan = this.dataZoomModel.findRepresentativeAxisProxy().getMinMaxSpan();
sliderMove(0, range, [0, 100], 0, minMaxSpan.minSpan, minMaxSpan.maxSpan);
this.range = range;
if (lastRange[0] !== range[0] || lastRange[1] !== range[1]) {
return range;
}
},
pan: makeMover(function (range, axisModel, coordSysInfo, coordSysMainType, controller, e: RoamEventParams['pan']) {
const directionInfo = getDirectionInfo[coordSysMainType](
[e.oldX, e.oldY], [e.newX, e.newY], axisModel, controller, coordSysInfo
);
return directionInfo.signal
* (range[1] - range[0])
* directionInfo.pixel / directionInfo.pixelLength;
}),
scrollMove: makeMover(
function (
this: InsideZoomView,
range, axisModel, coordSysInfo, coordSysMainType, controller,
e: RoamEventParams['scrollMove']
) {
// `moveOnMouseWheelAxis` restricts the pan to one wheel axis;
// unset falls back to the combined `scrollDelta` for backward
// compatibility.
const effectiveDelta = pickWheelAxisValue(
(this.dataZoomModel as InsideZoomModel).get('moveOnMouseWheelAxis', true),
e.scrollDeltaX, e.scrollDeltaY, e.scrollDelta
);
const directionInfo = getDirectionInfo[coordSysMainType](
[0, 0], [effectiveDelta, effectiveDelta],
axisModel, controller, coordSysInfo
);
// Only `directionInfo.signal` is used here — the scalar already
// encodes the magnitude. `directionInfo.pixel` would go through
// `pointToCoord` for polar and no longer match the scalar.
return directionInfo.signal * (range[1] - range[0]) * effectiveDelta;
})
};
/**
* Picks the wheel-derived value for this dataZoom given a
* `moveOnMouseWheelAxis` / `zoomOnMouseWheelAxis` setting. Unset falls
* back to the caller-supplied scalar so existing configurations keep
* their pre-existing behavior.
*/
function pickWheelAxisValue(
wheelAxis: WheelAxisType | undefined,
axisHorizontal: number,
axisVertical: number,
fallback: number
): number {
if (wheelAxis === 'horizontal') {
return axisHorizontal;
}
if (wheelAxis === 'vertical') {
return axisVertical;
}
return fallback;
}
export type DataZoomGetRangeHandlers = typeof getRangeHandlers;
function makeMover(
getPercentDelta: (
this: InsideZoomView,
range: [number, number],
axisModel: AxisBaseModel,
coordSysInfo: DataZoomReferCoordSysInfo,
coordSysMainType: DataZoomCoordSysMainType,
controller: RoamController,
e: RoamEventParams['scrollMove']| RoamEventParams['pan']
) => number
) {
return function (
this: InsideZoomView,
coordSysInfo: DataZoomReferCoordSysInfo,
coordSysMainType: DataZoomCoordSysMainType,
controller: RoamController,
e: RoamEventParams['scrollMove']| RoamEventParams['pan']
): [number, number] {
const lastRange = this.range;
const range = lastRange.slice() as [number, number];
// Calculate transform by the first axis.
const axisModel = coordSysInfo.axisModels[0];
if (!axisModel) {
return;
}
const percentDelta = getPercentDelta.call(
this, range, axisModel, coordSysInfo, coordSysMainType, controller, e
);
sliderMove(percentDelta, range, [0, 100], 'all');
this.range = range;
if (lastRange[0] !== range[0] || lastRange[1] !== range[1]) {
return range;
}
};
}
interface DirectionInfo {
pixel: number
pixelLength: number
pixelStart: number
signal: -1 | 1
}
interface GetDirectionInfo {
(
oldPoint: number[],
newPoint: number[],
axisModel: AxisBaseModel,
controller: RoamController,
coordSysInfo: DataZoomReferCoordSysInfo
): DirectionInfo
}
const getDirectionInfo: Record<'grid' | 'polar' | 'singleAxis', GetDirectionInfo> = {
grid(oldPoint, newPoint, axisModel, controller, coordSysInfo) {
const axis = axisModel.axis;
const ret = {} as DirectionInfo;
const rect = coordSysInfo.model.coordinateSystem.getRect();
oldPoint = oldPoint || [0, 0];
if (axis.dim === 'x') {
ret.pixel = newPoint[0] - oldPoint[0];
ret.pixelLength = rect.width;
ret.pixelStart = rect.x;
ret.signal = axis.inverse ? 1 : -1;
}
else { // axis.dim === 'y'
ret.pixel = newPoint[1] - oldPoint[1];
ret.pixelLength = rect.height;
ret.pixelStart = rect.y;
ret.signal = axis.inverse ? -1 : 1;
}
return ret;
},
polar(oldPoint, newPoint, axisModel, controller, coordSysInfo) {
const axis = axisModel.axis;
const ret = {} as DirectionInfo;
const polar = coordSysInfo.model.coordinateSystem as Polar;
const radiusExtent = polar.getRadiusAxis().getExtent();
const angleExtent = polar.getAngleAxis().getExtent();
oldPoint = oldPoint ? polar.pointToCoord(oldPoint) : [0, 0];
newPoint = polar.pointToCoord(newPoint);
if (axisModel.mainType === 'radiusAxis') {
ret.pixel = newPoint[0] - oldPoint[0];
// ret.pixelLength = Math.abs(radiusExtent[1] - radiusExtent[0]);
// ret.pixelStart = Math.min(radiusExtent[0], radiusExtent[1]);
ret.pixelLength = radiusExtent[1] - radiusExtent[0];
ret.pixelStart = radiusExtent[0];
ret.signal = axis.inverse ? 1 : -1;
}
else { // 'angleAxis'
ret.pixel = newPoint[1] - oldPoint[1];
// ret.pixelLength = Math.abs(angleExtent[1] - angleExtent[0]);
// ret.pixelStart = Math.min(angleExtent[0], angleExtent[1]);
ret.pixelLength = angleExtent[1] - angleExtent[0];
ret.pixelStart = angleExtent[0];
ret.signal = axis.inverse ? -1 : 1;
}
return ret;
},
singleAxis(oldPoint, newPoint, axisModel, controller, coordSysInfo) {
const axis = axisModel.axis as SingleAxis;
const rect = coordSysInfo.model.coordinateSystem.getRect();
const ret = {} as DirectionInfo;
oldPoint = oldPoint || [0, 0];
if (axis.orient === 'horizontal') {
ret.pixel = newPoint[0] - oldPoint[0];
ret.pixelLength = rect.width;
ret.pixelStart = rect.x;
ret.signal = axis.inverse ? 1 : -1;
}
else { // 'vertical'
ret.pixel = newPoint[1] - oldPoint[1];
ret.pixelLength = rect.height;
ret.pixelStart = rect.y;
ret.signal = axis.inverse ? -1 : 1;
}
return ret;
}
};
export default InsideZoomView;