View.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var zrUtil = require("zrender/lib/core/util");
  20. var vector = require("zrender/lib/core/vector");
  21. var matrix = require("zrender/lib/core/matrix");
  22. var BoundingRect = require("zrender/lib/core/BoundingRect");
  23. var Transformable = require("zrender/lib/mixin/Transformable");
  24. /*
  25. * Licensed to the Apache Software Foundation (ASF) under one
  26. * or more contributor license agreements. See the NOTICE file
  27. * distributed with this work for additional information
  28. * regarding copyright ownership. The ASF licenses this file
  29. * to you under the Apache License, Version 2.0 (the
  30. * "License"); you may not use this file except in compliance
  31. * with the License. You may obtain a copy of the License at
  32. *
  33. * http://www.apache.org/licenses/LICENSE-2.0
  34. *
  35. * Unless required by applicable law or agreed to in writing,
  36. * software distributed under the License is distributed on an
  37. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  38. * KIND, either express or implied. See the License for the
  39. * specific language governing permissions and limitations
  40. * under the License.
  41. */
  42. /**
  43. * Simple view coordinate system
  44. * Mapping given x, y to transformd view x, y
  45. */
  46. var v2ApplyTransform = vector.applyTransform; // Dummy transform node
  47. function TransformDummy() {
  48. Transformable.call(this);
  49. }
  50. zrUtil.mixin(TransformDummy, Transformable);
  51. function View(name) {
  52. /**
  53. * @type {string}
  54. */
  55. this.name = name;
  56. /**
  57. * @type {Object}
  58. */
  59. this.zoomLimit;
  60. Transformable.call(this);
  61. this._roamTransformable = new TransformDummy();
  62. this._rawTransformable = new TransformDummy();
  63. this._center;
  64. this._zoom;
  65. }
  66. View.prototype = {
  67. constructor: View,
  68. type: 'view',
  69. /**
  70. * @param {Array.<string>}
  71. * @readOnly
  72. */
  73. dimensions: ['x', 'y'],
  74. /**
  75. * Set bounding rect
  76. * @param {number} x
  77. * @param {number} y
  78. * @param {number} width
  79. * @param {number} height
  80. */
  81. // PENDING to getRect
  82. setBoundingRect: function (x, y, width, height) {
  83. this._rect = new BoundingRect(x, y, width, height);
  84. return this._rect;
  85. },
  86. /**
  87. * @return {module:zrender/core/BoundingRect}
  88. */
  89. // PENDING to getRect
  90. getBoundingRect: function () {
  91. return this._rect;
  92. },
  93. /**
  94. * @param {number} x
  95. * @param {number} y
  96. * @param {number} width
  97. * @param {number} height
  98. */
  99. setViewRect: function (x, y, width, height) {
  100. this.transformTo(x, y, width, height);
  101. this._viewRect = new BoundingRect(x, y, width, height);
  102. },
  103. /**
  104. * Transformed to particular position and size
  105. * @param {number} x
  106. * @param {number} y
  107. * @param {number} width
  108. * @param {number} height
  109. */
  110. transformTo: function (x, y, width, height) {
  111. var rect = this.getBoundingRect();
  112. var rawTransform = this._rawTransformable;
  113. rawTransform.transform = rect.calculateTransform(new BoundingRect(x, y, width, height));
  114. rawTransform.decomposeTransform();
  115. this._updateTransform();
  116. },
  117. /**
  118. * Set center of view
  119. * @param {Array.<number>} [centerCoord]
  120. */
  121. setCenter: function (centerCoord) {
  122. if (!centerCoord) {
  123. return;
  124. }
  125. this._center = centerCoord;
  126. this._updateCenterAndZoom();
  127. },
  128. /**
  129. * @param {number} zoom
  130. */
  131. setZoom: function (zoom) {
  132. zoom = zoom || 1;
  133. var zoomLimit = this.zoomLimit;
  134. if (zoomLimit) {
  135. if (zoomLimit.max != null) {
  136. zoom = Math.min(zoomLimit.max, zoom);
  137. }
  138. if (zoomLimit.min != null) {
  139. zoom = Math.max(zoomLimit.min, zoom);
  140. }
  141. }
  142. this._zoom = zoom;
  143. this._updateCenterAndZoom();
  144. },
  145. /**
  146. * Get default center without roam
  147. */
  148. getDefaultCenter: function () {
  149. // Rect before any transform
  150. var rawRect = this.getBoundingRect();
  151. var cx = rawRect.x + rawRect.width / 2;
  152. var cy = rawRect.y + rawRect.height / 2;
  153. return [cx, cy];
  154. },
  155. getCenter: function () {
  156. return this._center || this.getDefaultCenter();
  157. },
  158. getZoom: function () {
  159. return this._zoom || 1;
  160. },
  161. /**
  162. * @return {Array.<number}
  163. */
  164. getRoamTransform: function () {
  165. return this._roamTransformable.getLocalTransform();
  166. },
  167. /**
  168. * Remove roam
  169. */
  170. _updateCenterAndZoom: function () {
  171. // Must update after view transform updated
  172. var rawTransformMatrix = this._rawTransformable.getLocalTransform();
  173. var roamTransform = this._roamTransformable;
  174. var defaultCenter = this.getDefaultCenter();
  175. var center = this.getCenter();
  176. var zoom = this.getZoom();
  177. center = vector.applyTransform([], center, rawTransformMatrix);
  178. defaultCenter = vector.applyTransform([], defaultCenter, rawTransformMatrix);
  179. roamTransform.origin = center;
  180. roamTransform.position = [defaultCenter[0] - center[0], defaultCenter[1] - center[1]];
  181. roamTransform.scale = [zoom, zoom];
  182. this._updateTransform();
  183. },
  184. /**
  185. * Update transform from roam and mapLocation
  186. * @private
  187. */
  188. _updateTransform: function () {
  189. var roamTransformable = this._roamTransformable;
  190. var rawTransformable = this._rawTransformable;
  191. rawTransformable.parent = roamTransformable;
  192. roamTransformable.updateTransform();
  193. rawTransformable.updateTransform();
  194. matrix.copy(this.transform || (this.transform = []), rawTransformable.transform || matrix.create());
  195. this._rawTransform = rawTransformable.getLocalTransform();
  196. this.invTransform = this.invTransform || [];
  197. matrix.invert(this.invTransform, this.transform);
  198. this.decomposeTransform();
  199. },
  200. getTransformInfo: function () {
  201. var roamTransform = this._roamTransformable.transform;
  202. var rawTransformable = this._rawTransformable;
  203. return {
  204. roamTransform: roamTransform ? zrUtil.slice(roamTransform) : matrix.create(),
  205. rawScale: zrUtil.slice(rawTransformable.scale),
  206. rawPosition: zrUtil.slice(rawTransformable.position)
  207. };
  208. },
  209. /**
  210. * @return {module:zrender/core/BoundingRect}
  211. */
  212. getViewRect: function () {
  213. return this._viewRect;
  214. },
  215. /**
  216. * Get view rect after roam transform
  217. * @return {module:zrender/core/BoundingRect}
  218. */
  219. getViewRectAfterRoam: function () {
  220. var rect = this.getBoundingRect().clone();
  221. rect.applyTransform(this.transform);
  222. return rect;
  223. },
  224. /**
  225. * Convert a single (lon, lat) data item to (x, y) point.
  226. * @param {Array.<number>} data
  227. * @param {boolean} noRoam
  228. * @param {Array.<number>} [out]
  229. * @return {Array.<number>}
  230. */
  231. dataToPoint: function (data, noRoam, out) {
  232. var transform = noRoam ? this._rawTransform : this.transform;
  233. out = out || [];
  234. return transform ? v2ApplyTransform(out, data, transform) : vector.copy(out, data);
  235. },
  236. /**
  237. * Convert a (x, y) point to (lon, lat) data
  238. * @param {Array.<number>} point
  239. * @return {Array.<number>}
  240. */
  241. pointToData: function (point) {
  242. var invTransform = this.invTransform;
  243. return invTransform ? v2ApplyTransform([], point, invTransform) : [point[0], point[1]];
  244. },
  245. /**
  246. * @implements
  247. * see {module:echarts/CoodinateSystem}
  248. */
  249. convertToPixel: zrUtil.curry(doConvert, 'dataToPoint'),
  250. /**
  251. * @implements
  252. * see {module:echarts/CoodinateSystem}
  253. */
  254. convertFromPixel: zrUtil.curry(doConvert, 'pointToData'),
  255. /**
  256. * @implements
  257. * see {module:echarts/CoodinateSystem}
  258. */
  259. containPoint: function (point) {
  260. return this.getViewRectAfterRoam().contain(point[0], point[1]);
  261. }
  262. /**
  263. * @return {number}
  264. */
  265. // getScalarScale: function () {
  266. // // Use determinant square root of transform to mutiply scalar
  267. // var m = this.transform;
  268. // var det = Math.sqrt(Math.abs(m[0] * m[3] - m[2] * m[1]));
  269. // return det;
  270. // }
  271. };
  272. zrUtil.mixin(View, Transformable);
  273. function doConvert(methodName, ecModel, finder, value) {
  274. var seriesModel = finder.seriesModel;
  275. var coordSys = seriesModel ? seriesModel.coordinateSystem : null; // e.g., graph.
  276. return coordSys === this ? coordSys[methodName](value) : null;
  277. }
  278. var _default = View;
  279. module.exports = _default;