View.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 =
  52. /** @class */
  53. function (_super) {
  54. __extends(View, _super);
  55. function View(name) {
  56. var _this = _super.call(this) || this;
  57. _this.type = 'view';
  58. _this.dimensions = ['x', 'y'];
  59. /**
  60. * Represents the transform brought by roam/zoom.
  61. * If `View['_viewRect']` applies roam transform,
  62. * we can get the final displayed rect.
  63. */
  64. _this._roamTransformable = new Transformable();
  65. /**
  66. * Represents the transform from `View['_rect']` to `View['_viewRect']`.
  67. */
  68. _this._rawTransformable = new Transformable();
  69. _this.name = name;
  70. return _this;
  71. }
  72. View.prototype.setBoundingRect = function (x, y, width, height) {
  73. this._rect = new BoundingRect(x, y, width, height);
  74. return this._rect;
  75. };
  76. /**
  77. * @return {module:zrender/core/BoundingRect}
  78. */
  79. View.prototype.getBoundingRect = function () {
  80. return this._rect;
  81. };
  82. View.prototype.setViewRect = function (x, y, width, height) {
  83. this._transformTo(x, y, width, height);
  84. this._viewRect = new BoundingRect(x, y, width, height);
  85. };
  86. /**
  87. * Transformed to particular position and size
  88. */
  89. View.prototype._transformTo = function (x, y, width, height) {
  90. var rect = this.getBoundingRect();
  91. var rawTransform = this._rawTransformable;
  92. rawTransform.transform = rect.calculateTransform(new BoundingRect(x, y, width, height));
  93. var rawParent = rawTransform.parent;
  94. rawTransform.parent = null;
  95. rawTransform.decomposeTransform();
  96. rawTransform.parent = rawParent;
  97. this._updateTransform();
  98. };
  99. /**
  100. * Set center of view
  101. */
  102. View.prototype.setCenter = function (centerCoord, api) {
  103. if (!centerCoord) {
  104. return;
  105. }
  106. this._center = [parsePercent(centerCoord[0], api.getWidth()), parsePercent(centerCoord[1], api.getHeight())];
  107. this._updateCenterAndZoom();
  108. };
  109. View.prototype.setZoom = function (zoom) {
  110. zoom = zoom || 1;
  111. var zoomLimit = this.zoomLimit;
  112. if (zoomLimit) {
  113. if (zoomLimit.max != null) {
  114. zoom = Math.min(zoomLimit.max, zoom);
  115. }
  116. if (zoomLimit.min != null) {
  117. zoom = Math.max(zoomLimit.min, zoom);
  118. }
  119. }
  120. this._zoom = zoom;
  121. this._updateCenterAndZoom();
  122. };
  123. /**
  124. * Get default center without roam
  125. */
  126. View.prototype.getDefaultCenter = function () {
  127. // Rect before any transform
  128. var rawRect = this.getBoundingRect();
  129. var cx = rawRect.x + rawRect.width / 2;
  130. var cy = rawRect.y + rawRect.height / 2;
  131. return [cx, cy];
  132. };
  133. View.prototype.getCenter = function () {
  134. return this._center || this.getDefaultCenter();
  135. };
  136. View.prototype.getZoom = function () {
  137. return this._zoom || 1;
  138. };
  139. View.prototype.getRoamTransform = function () {
  140. return this._roamTransformable.getLocalTransform();
  141. };
  142. /**
  143. * Remove roam
  144. */
  145. View.prototype._updateCenterAndZoom = function () {
  146. // Must update after view transform updated
  147. var rawTransformMatrix = this._rawTransformable.getLocalTransform();
  148. var roamTransform = this._roamTransformable;
  149. var defaultCenter = this.getDefaultCenter();
  150. var center = this.getCenter();
  151. var zoom = this.getZoom();
  152. center = vector.applyTransform([], center, rawTransformMatrix);
  153. defaultCenter = vector.applyTransform([], defaultCenter, rawTransformMatrix);
  154. roamTransform.originX = center[0];
  155. roamTransform.originY = center[1];
  156. roamTransform.x = defaultCenter[0] - center[0];
  157. roamTransform.y = defaultCenter[1] - center[1];
  158. roamTransform.scaleX = roamTransform.scaleY = zoom;
  159. this._updateTransform();
  160. };
  161. /**
  162. * Update transform props on `this` based on the current
  163. * `this._roamTransformable` and `this._rawTransformable`.
  164. */
  165. View.prototype._updateTransform = function () {
  166. var roamTransformable = this._roamTransformable;
  167. var rawTransformable = this._rawTransformable;
  168. rawTransformable.parent = roamTransformable;
  169. roamTransformable.updateTransform();
  170. rawTransformable.updateTransform();
  171. matrix.copy(this.transform || (this.transform = []), rawTransformable.transform || matrix.create());
  172. this._rawTransform = rawTransformable.getLocalTransform();
  173. this.invTransform = this.invTransform || [];
  174. matrix.invert(this.invTransform, this.transform);
  175. this.decomposeTransform();
  176. };
  177. View.prototype.getTransformInfo = function () {
  178. var rawTransformable = this._rawTransformable;
  179. var roamTransformable = this._roamTransformable; // Because roamTransformabel has `originX/originY` modified,
  180. // but the caller of `getTransformInfo` can not handle `originX/originY`,
  181. // so need to recalculate them.
  182. var dummyTransformable = new Transformable();
  183. dummyTransformable.transform = roamTransformable.transform;
  184. dummyTransformable.decomposeTransform();
  185. return {
  186. roam: {
  187. x: dummyTransformable.x,
  188. y: dummyTransformable.y,
  189. scaleX: dummyTransformable.scaleX,
  190. scaleY: dummyTransformable.scaleY
  191. },
  192. raw: {
  193. x: rawTransformable.x,
  194. y: rawTransformable.y,
  195. scaleX: rawTransformable.scaleX,
  196. scaleY: rawTransformable.scaleY
  197. }
  198. };
  199. };
  200. View.prototype.getViewRect = function () {
  201. return this._viewRect;
  202. };
  203. /**
  204. * Get view rect after roam transform
  205. */
  206. View.prototype.getViewRectAfterRoam = function () {
  207. var rect = this.getBoundingRect().clone();
  208. rect.applyTransform(this.transform);
  209. return rect;
  210. };
  211. /**
  212. * Convert a single (lon, lat) data item to (x, y) point.
  213. */
  214. View.prototype.dataToPoint = function (data, noRoam, out) {
  215. var transform = noRoam ? this._rawTransform : this.transform;
  216. out = out || [];
  217. return transform ? v2ApplyTransform(out, data, transform) : vector.copy(out, data);
  218. };
  219. /**
  220. * Convert a (x, y) point to (lon, lat) data
  221. */
  222. View.prototype.pointToData = function (point) {
  223. var invTransform = this.invTransform;
  224. return invTransform ? v2ApplyTransform([], point, invTransform) : [point[0], point[1]];
  225. };
  226. View.prototype.convertToPixel = function (ecModel, finder, value) {
  227. var coordSys = getCoordSys(finder);
  228. return coordSys === this ? coordSys.dataToPoint(value) : null;
  229. };
  230. View.prototype.convertFromPixel = function (ecModel, finder, pixel) {
  231. var coordSys = getCoordSys(finder);
  232. return coordSys === this ? coordSys.pointToData(pixel) : null;
  233. };
  234. /**
  235. * @implements
  236. */
  237. View.prototype.containPoint = function (point) {
  238. return this.getViewRectAfterRoam().contain(point[0], point[1]);
  239. };
  240. View.dimensions = ['x', 'y'];
  241. return View;
  242. }(Transformable);
  243. function getCoordSys(finder) {
  244. var seriesModel = finder.seriesModel;
  245. return seriesModel ? seriesModel.coordinateSystem : null; // e.g., graph.
  246. }
  247. export default View;