View.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. /**
  42. * Simple view coordinate system
  43. * Mapping given x, y to transformd view x, y
  44. */
  45. import * as vector from 'zrender/lib/core/vector';
  46. import * as matrix from 'zrender/lib/core/matrix';
  47. import BoundingRect from 'zrender/lib/core/BoundingRect';
  48. import Transformable from 'zrender/lib/core/Transformable';
  49. var v2ApplyTransform = vector.applyTransform;
  50. var View =
  51. /** @class */
  52. function (_super) {
  53. __extends(View, _super);
  54. function View(name) {
  55. var _this = _super.call(this) || this;
  56. _this.type = 'view';
  57. _this.dimensions = ['x', 'y'];
  58. /**
  59. * Represents the transform brought by roam/zoom.
  60. * If `View['_viewRect']` applies roam transform,
  61. * we can get the final displayed rect.
  62. */
  63. _this._roamTransformable = new Transformable();
  64. /**
  65. * Represents the transform from `View['_rect']` to `View['_viewRect']`.
  66. */
  67. _this._rawTransformable = new Transformable();
  68. _this.name = name;
  69. return _this;
  70. }
  71. View.prototype.setBoundingRect = function (x, y, width, height) {
  72. this._rect = new BoundingRect(x, y, width, height);
  73. return this._rect;
  74. };
  75. /**
  76. * @return {module:zrender/core/BoundingRect}
  77. */
  78. View.prototype.getBoundingRect = function () {
  79. return this._rect;
  80. };
  81. View.prototype.setViewRect = function (x, y, width, height) {
  82. this._transformTo(x, y, width, height);
  83. this._viewRect = new BoundingRect(x, y, width, height);
  84. };
  85. /**
  86. * Transformed to particular position and size
  87. */
  88. View.prototype._transformTo = function (x, y, width, height) {
  89. var rect = this.getBoundingRect();
  90. var rawTransform = this._rawTransformable;
  91. rawTransform.transform = rect.calculateTransform(new BoundingRect(x, y, width, height));
  92. var rawParent = rawTransform.parent;
  93. rawTransform.parent = null;
  94. rawTransform.decomposeTransform();
  95. rawTransform.parent = rawParent;
  96. this._updateTransform();
  97. };
  98. /**
  99. * Set center of view
  100. */
  101. View.prototype.setCenter = function (centerCoord) {
  102. if (!centerCoord) {
  103. return;
  104. }
  105. this._center = centerCoord;
  106. this._updateCenterAndZoom();
  107. };
  108. View.prototype.setZoom = function (zoom) {
  109. zoom = zoom || 1;
  110. var zoomLimit = this.zoomLimit;
  111. if (zoomLimit) {
  112. if (zoomLimit.max != null) {
  113. zoom = Math.min(zoomLimit.max, zoom);
  114. }
  115. if (zoomLimit.min != null) {
  116. zoom = Math.max(zoomLimit.min, zoom);
  117. }
  118. }
  119. this._zoom = zoom;
  120. this._updateCenterAndZoom();
  121. };
  122. /**
  123. * Get default center without roam
  124. */
  125. View.prototype.getDefaultCenter = function () {
  126. // Rect before any transform
  127. var rawRect = this.getBoundingRect();
  128. var cx = rawRect.x + rawRect.width / 2;
  129. var cy = rawRect.y + rawRect.height / 2;
  130. return [cx, cy];
  131. };
  132. View.prototype.getCenter = function () {
  133. return this._center || this.getDefaultCenter();
  134. };
  135. View.prototype.getZoom = function () {
  136. return this._zoom || 1;
  137. };
  138. View.prototype.getRoamTransform = function () {
  139. return this._roamTransformable.getLocalTransform();
  140. };
  141. /**
  142. * Remove roam
  143. */
  144. View.prototype._updateCenterAndZoom = function () {
  145. // Must update after view transform updated
  146. var rawTransformMatrix = this._rawTransformable.getLocalTransform();
  147. var roamTransform = this._roamTransformable;
  148. var defaultCenter = this.getDefaultCenter();
  149. var center = this.getCenter();
  150. var zoom = this.getZoom();
  151. center = vector.applyTransform([], center, rawTransformMatrix);
  152. defaultCenter = vector.applyTransform([], defaultCenter, rawTransformMatrix);
  153. roamTransform.originX = center[0];
  154. roamTransform.originY = center[1];
  155. roamTransform.x = defaultCenter[0] - center[0];
  156. roamTransform.y = defaultCenter[1] - center[1];
  157. roamTransform.scaleX = roamTransform.scaleY = zoom;
  158. this._updateTransform();
  159. };
  160. /**
  161. * Update transform props on `this` based on the current
  162. * `this._roamTransformable` and `this._rawTransformable`.
  163. */
  164. View.prototype._updateTransform = function () {
  165. var roamTransformable = this._roamTransformable;
  166. var rawTransformable = this._rawTransformable;
  167. rawTransformable.parent = roamTransformable;
  168. roamTransformable.updateTransform();
  169. rawTransformable.updateTransform();
  170. matrix.copy(this.transform || (this.transform = []), rawTransformable.transform || matrix.create());
  171. this._rawTransform = rawTransformable.getLocalTransform();
  172. this.invTransform = this.invTransform || [];
  173. matrix.invert(this.invTransform, this.transform);
  174. this.decomposeTransform();
  175. };
  176. View.prototype.getTransformInfo = function () {
  177. var rawTransformable = this._rawTransformable;
  178. var roamTransformable = this._roamTransformable; // Becuase roamTransformabel has `originX/originY` modified,
  179. // but the caller of `getTransformInfo` can not handle `originX/originY`,
  180. // so need to recalcualte them.
  181. var dummyTransformable = new Transformable();
  182. dummyTransformable.transform = roamTransformable.transform;
  183. dummyTransformable.decomposeTransform();
  184. return {
  185. roam: {
  186. x: dummyTransformable.x,
  187. y: dummyTransformable.y,
  188. scaleX: dummyTransformable.scaleX,
  189. scaleY: dummyTransformable.scaleY
  190. },
  191. raw: {
  192. x: rawTransformable.x,
  193. y: rawTransformable.y,
  194. scaleX: rawTransformable.scaleX,
  195. scaleY: rawTransformable.scaleY
  196. }
  197. };
  198. };
  199. View.prototype.getViewRect = function () {
  200. return this._viewRect;
  201. };
  202. /**
  203. * Get view rect after roam transform
  204. */
  205. View.prototype.getViewRectAfterRoam = function () {
  206. var rect = this.getBoundingRect().clone();
  207. rect.applyTransform(this.transform);
  208. return rect;
  209. };
  210. /**
  211. * Convert a single (lon, lat) data item to (x, y) point.
  212. */
  213. View.prototype.dataToPoint = function (data, noRoam, out) {
  214. var transform = noRoam ? this._rawTransform : this.transform;
  215. out = out || [];
  216. return transform ? v2ApplyTransform(out, data, transform) : vector.copy(out, data);
  217. };
  218. /**
  219. * Convert a (x, y) point to (lon, lat) data
  220. */
  221. View.prototype.pointToData = function (point) {
  222. var invTransform = this.invTransform;
  223. return invTransform ? v2ApplyTransform([], point, invTransform) : [point[0], point[1]];
  224. };
  225. View.prototype.convertToPixel = function (ecModel, finder, value) {
  226. var coordSys = getCoordSys(finder);
  227. return coordSys === this ? coordSys.dataToPoint(value) : null;
  228. };
  229. View.prototype.convertFromPixel = function (ecModel, finder, pixel) {
  230. var coordSys = getCoordSys(finder);
  231. return coordSys === this ? coordSys.pointToData(pixel) : null;
  232. };
  233. /**
  234. * @implements
  235. */
  236. View.prototype.containPoint = function (point) {
  237. return this.getViewRectAfterRoam().contain(point[0], point[1]);
  238. };
  239. View.dimensions = ['x', 'y'];
  240. return View;
  241. }(Transformable);
  242. function getCoordSys(finder) {
  243. var seriesModel = finder.seriesModel;
  244. return seriesModel ? seriesModel.coordinateSystem : null; // e.g., graph.
  245. }
  246. export default View;