View.js
8.21 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
/*
* 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.
*/
var zrUtil = require("zrender/lib/core/util");
var vector = require("zrender/lib/core/vector");
var matrix = require("zrender/lib/core/matrix");
var BoundingRect = require("zrender/lib/core/BoundingRect");
var Transformable = require("zrender/lib/mixin/Transformable");
/*
* 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.
*/
/**
* Simple view coordinate system
* Mapping given x, y to transformd view x, y
*/
var v2ApplyTransform = vector.applyTransform; // Dummy transform node
function TransformDummy() {
Transformable.call(this);
}
zrUtil.mixin(TransformDummy, Transformable);
function View(name) {
/**
* @type {string}
*/
this.name = name;
/**
* @type {Object}
*/
this.zoomLimit;
Transformable.call(this);
this._roamTransformable = new TransformDummy();
this._rawTransformable = new TransformDummy();
this._center;
this._zoom;
}
View.prototype = {
constructor: View,
type: 'view',
/**
* @param {Array.<string>}
* @readOnly
*/
dimensions: ['x', 'y'],
/**
* Set bounding rect
* @param {number} x
* @param {number} y
* @param {number} width
* @param {number} height
*/
// PENDING to getRect
setBoundingRect: function (x, y, width, height) {
this._rect = new BoundingRect(x, y, width, height);
return this._rect;
},
/**
* @return {module:zrender/core/BoundingRect}
*/
// PENDING to getRect
getBoundingRect: function () {
return this._rect;
},
/**
* @param {number} x
* @param {number} y
* @param {number} width
* @param {number} height
*/
setViewRect: function (x, y, width, height) {
this.transformTo(x, y, width, height);
this._viewRect = new BoundingRect(x, y, width, height);
},
/**
* Transformed to particular position and size
* @param {number} x
* @param {number} y
* @param {number} width
* @param {number} height
*/
transformTo: function (x, y, width, height) {
var rect = this.getBoundingRect();
var rawTransform = this._rawTransformable;
rawTransform.transform = rect.calculateTransform(new BoundingRect(x, y, width, height));
rawTransform.decomposeTransform();
this._updateTransform();
},
/**
* Set center of view
* @param {Array.<number>} [centerCoord]
*/
setCenter: function (centerCoord) {
if (!centerCoord) {
return;
}
this._center = centerCoord;
this._updateCenterAndZoom();
},
/**
* @param {number} zoom
*/
setZoom: function (zoom) {
zoom = zoom || 1;
var zoomLimit = this.zoomLimit;
if (zoomLimit) {
if (zoomLimit.max != null) {
zoom = Math.min(zoomLimit.max, zoom);
}
if (zoomLimit.min != null) {
zoom = Math.max(zoomLimit.min, zoom);
}
}
this._zoom = zoom;
this._updateCenterAndZoom();
},
/**
* Get default center without roam
*/
getDefaultCenter: function () {
// Rect before any transform
var rawRect = this.getBoundingRect();
var cx = rawRect.x + rawRect.width / 2;
var cy = rawRect.y + rawRect.height / 2;
return [cx, cy];
},
getCenter: function () {
return this._center || this.getDefaultCenter();
},
getZoom: function () {
return this._zoom || 1;
},
/**
* @return {Array.<number}
*/
getRoamTransform: function () {
return this._roamTransformable.getLocalTransform();
},
/**
* Remove roam
*/
_updateCenterAndZoom: function () {
// Must update after view transform updated
var rawTransformMatrix = this._rawTransformable.getLocalTransform();
var roamTransform = this._roamTransformable;
var defaultCenter = this.getDefaultCenter();
var center = this.getCenter();
var zoom = this.getZoom();
center = vector.applyTransform([], center, rawTransformMatrix);
defaultCenter = vector.applyTransform([], defaultCenter, rawTransformMatrix);
roamTransform.origin = center;
roamTransform.position = [defaultCenter[0] - center[0], defaultCenter[1] - center[1]];
roamTransform.scale = [zoom, zoom];
this._updateTransform();
},
/**
* Update transform from roam and mapLocation
* @private
*/
_updateTransform: function () {
var roamTransformable = this._roamTransformable;
var rawTransformable = this._rawTransformable;
rawTransformable.parent = roamTransformable;
roamTransformable.updateTransform();
rawTransformable.updateTransform();
matrix.copy(this.transform || (this.transform = []), rawTransformable.transform || matrix.create());
this._rawTransform = rawTransformable.getLocalTransform();
this.invTransform = this.invTransform || [];
matrix.invert(this.invTransform, this.transform);
this.decomposeTransform();
},
/**
* @return {module:zrender/core/BoundingRect}
*/
getViewRect: function () {
return this._viewRect;
},
/**
* Get view rect after roam transform
* @return {module:zrender/core/BoundingRect}
*/
getViewRectAfterRoam: function () {
var rect = this.getBoundingRect().clone();
rect.applyTransform(this.transform);
return rect;
},
/**
* Convert a single (lon, lat) data item to (x, y) point.
* @param {Array.<number>} data
* @param {boolean} noRoam
* @param {Array.<number>} [out]
* @return {Array.<number>}
*/
dataToPoint: function (data, noRoam, out) {
var transform = noRoam ? this._rawTransform : this.transform;
out = out || [];
return transform ? v2ApplyTransform(out, data, transform) : vector.copy(out, data);
},
/**
* Convert a (x, y) point to (lon, lat) data
* @param {Array.<number>} point
* @return {Array.<number>}
*/
pointToData: function (point) {
var invTransform = this.invTransform;
return invTransform ? v2ApplyTransform([], point, invTransform) : [point[0], point[1]];
},
/**
* @implements
* see {module:echarts/CoodinateSystem}
*/
convertToPixel: zrUtil.curry(doConvert, 'dataToPoint'),
/**
* @implements
* see {module:echarts/CoodinateSystem}
*/
convertFromPixel: zrUtil.curry(doConvert, 'pointToData'),
/**
* @implements
* see {module:echarts/CoodinateSystem}
*/
containPoint: function (point) {
return this.getViewRectAfterRoam().contain(point[0], point[1]);
}
/**
* @return {number}
*/
// getScalarScale: function () {
// // Use determinant square root of transform to mutiply scalar
// var m = this.transform;
// var det = Math.sqrt(Math.abs(m[0] * m[3] - m[2] * m[1]));
// return det;
// }
};
zrUtil.mixin(View, Transformable);
function doConvert(methodName, ecModel, finder, value) {
var seriesModel = finder.seriesModel;
var coordSys = seriesModel ? seriesModel.coordinateSystem : null; // e.g., graph.
return coordSys === this ? coordSys[methodName](value) : null;
}
var _default = View;
module.exports = _default;