View.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.js';
  46. import * as matrix from 'zrender/lib/core/matrix.js';
  47. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  48. import Transformable from 'zrender/lib/core/Transformable.js';
  49. import { parsePercent } from '../util/number.js';
  50. var v2ApplyTransform = vector.applyTransform;
  51. var View = /** @class */function (_super) {
  52. __extends(View, _super);
  53. function View(name) {
  54. var _this = _super.call(this) || this;
  55. _this.type = 'view';
  56. _this.dimensions = ['x', 'y'];
  57. /**
  58. * Represents the transform brought by roam/zoom.
  59. * If `View['_viewRect']` applies roam transform,
  60. * we can get the final displayed rect.
  61. */
  62. _this._roamTransformable = new Transformable();
  63. /**
  64. * Represents the transform from `View['_rect']` to `View['_viewRect']`.
  65. */
  66. _this._rawTransformable = new Transformable();
  67. _this.name = name;
  68. return _this;
  69. }
  70. View.prototype.setBoundingRect = function (x, y, width, height) {
  71. this._rect = new BoundingRect(x, y, width, height);
  72. return this._rect;
  73. };
  74. /**
  75. * @return {module:zrender/core/BoundingRect}
  76. */
  77. View.prototype.getBoundingRect = function () {
  78. return this._rect;
  79. };
  80. View.prototype.setViewRect = function (x, y, width, height) {
  81. this._transformTo(x, y, width, height);
  82. this._viewRect = new BoundingRect(x, y, width, height);
  83. };
  84. /**
  85. * Transformed to particular position and size
  86. */
  87. View.prototype._transformTo = function (x, y, width, height) {
  88. var rect = this.getBoundingRect();
  89. var rawTransform = this._rawTransformable;
  90. rawTransform.transform = rect.calculateTransform(new BoundingRect(x, y, width, height));
  91. var rawParent = rawTransform.parent;
  92. rawTransform.parent = null;
  93. rawTransform.decomposeTransform();
  94. rawTransform.parent = rawParent;
  95. this._updateTransform();
  96. };
  97. /**
  98. * Set center of view
  99. */
  100. View.prototype.setCenter = function (centerCoord, api) {
  101. if (!centerCoord) {
  102. return;
  103. }
  104. this._center = [parsePercent(centerCoord[0], api.getWidth()), parsePercent(centerCoord[1], api.getHeight())];
  105. this._updateCenterAndZoom();
  106. };
  107. View.prototype.setZoom = function (zoom) {
  108. zoom = zoom || 1;
  109. var zoomLimit = this.zoomLimit;
  110. if (zoomLimit) {
  111. if (zoomLimit.max != null) {
  112. zoom = Math.min(zoomLimit.max, zoom);
  113. }
  114. if (zoomLimit.min != null) {
  115. zoom = Math.max(zoomLimit.min, zoom);
  116. }
  117. }
  118. this._zoom = zoom;
  119. this._updateCenterAndZoom();
  120. };
  121. /**
  122. * Get default center without roam
  123. */
  124. View.prototype.getDefaultCenter = function () {
  125. // Rect before any transform
  126. var rawRect = this.getBoundingRect();
  127. var cx = rawRect.x + rawRect.width / 2;
  128. var cy = rawRect.y + rawRect.height / 2;
  129. return [cx, cy];
  130. };
  131. View.prototype.getCenter = function () {
  132. return this._center || this.getDefaultCenter();
  133. };
  134. View.prototype.getZoom = function () {
  135. return this._zoom || 1;
  136. };
  137. View.prototype.getRoamTransform = function () {
  138. return this._roamTransformable.getLocalTransform();
  139. };
  140. /**
  141. * Remove roam
  142. */
  143. View.prototype._updateCenterAndZoom = function () {
  144. // Must update after view transform updated
  145. var rawTransformMatrix = this._rawTransformable.getLocalTransform();
  146. var roamTransform = this._roamTransformable;
  147. var defaultCenter = this.getDefaultCenter();
  148. var center = this.getCenter();
  149. var zoom = this.getZoom();
  150. center = vector.applyTransform([], center, rawTransformMatrix);
  151. defaultCenter = vector.applyTransform([], defaultCenter, rawTransformMatrix);
  152. roamTransform.originX = center[0];
  153. roamTransform.originY = center[1];
  154. roamTransform.x = defaultCenter[0] - center[0];
  155. roamTransform.y = defaultCenter[1] - center[1];
  156. roamTransform.scaleX = roamTransform.scaleY = zoom;
  157. this._updateTransform();
  158. };
  159. /**
  160. * Update transform props on `this` based on the current
  161. * `this._roamTransformable` and `this._rawTransformable`.
  162. */
  163. View.prototype._updateTransform = function () {
  164. var roamTransformable = this._roamTransformable;
  165. var rawTransformable = this._rawTransformable;
  166. rawTransformable.parent = roamTransformable;
  167. roamTransformable.updateTransform();
  168. rawTransformable.updateTransform();
  169. matrix.copy(this.transform || (this.transform = []), rawTransformable.transform || matrix.create());
  170. this._rawTransform = rawTransformable.getLocalTransform();
  171. this.invTransform = this.invTransform || [];
  172. matrix.invert(this.invTransform, this.transform);
  173. this.decomposeTransform();
  174. };
  175. View.prototype.getTransformInfo = function () {
  176. var rawTransformable = this._rawTransformable;
  177. var roamTransformable = this._roamTransformable;
  178. // Because roamTransformabel has `originX/originY` modified,
  179. // but the caller of `getTransformInfo` can not handle `originX/originY`,
  180. // so need to recalculate 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;