View.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. import { clone } from 'zrender/lib/core/util.js';
  51. import { clampByZoomLimit } from '../component/helper/roamHelper.js';
  52. var v2ApplyTransform = vector.applyTransform;
  53. var View = /** @class */function (_super) {
  54. __extends(View, _super);
  55. function View(name, opt) {
  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. _this._opt = opt;
  71. return _this;
  72. }
  73. View.prototype.setBoundingRect = function (x, y, width, height) {
  74. this._rect = new BoundingRect(x, y, width, height);
  75. this._updateCenterAndZoom();
  76. return this._rect;
  77. };
  78. View.prototype.getBoundingRect = function () {
  79. return this._rect;
  80. };
  81. /**
  82. * If no need to transform `View['_rect']` to `View['_viewRect']`, the calling of
  83. * `setViewRect` can be omitted.
  84. */
  85. View.prototype.setViewRect = function (x, y, width, height) {
  86. this._transformTo(x, y, width, height);
  87. this._viewRect = new BoundingRect(x, y, width, height);
  88. };
  89. /**
  90. * Transformed to particular position and size
  91. */
  92. View.prototype._transformTo = function (x, y, width, height) {
  93. var rect = this.getBoundingRect();
  94. var rawTransform = this._rawTransformable;
  95. rawTransform.transform = rect.calculateTransform(new BoundingRect(x, y, width, height));
  96. var rawParent = rawTransform.parent;
  97. rawTransform.parent = null;
  98. rawTransform.decomposeTransform();
  99. rawTransform.parent = rawParent;
  100. this._updateTransform();
  101. };
  102. /**
  103. * [NOTICE]
  104. * The definition of this center has always been irrelevant to some other series center like
  105. * 'series-pie.center' - this center is a point on the same coord sys as `View['_rect'].x/y`,
  106. * rather than canvas viewport, and the unit is not necessarily pixel (e.g., in geo case).
  107. * @see {View['_center']} for details.
  108. */
  109. View.prototype.setCenter = function (centerCoord) {
  110. // #16904 introcuded percentage string here, such as '33%'. But it was based on canvas
  111. // width/height, which is not reasonable - the unit may incorrect, and it is unpredictable if
  112. // the `View['_rect']` is not calculated based on the current canvas rect. Therefore the percentage
  113. // value is changed to based on `View['_rect'].width/height` since v6. Under this definition, users
  114. // can use '0%' to map the top-left of `View['_rect']` to the center of `View['_viewRect']`.
  115. var opt = this._opt;
  116. if (opt && opt.api && opt.ecModel && opt.ecModel.getShallow('legacyViewCoordSysCenterBase') && centerCoord) {
  117. centerCoord = [parsePercent(centerCoord[0], opt.api.getWidth()), parsePercent(centerCoord[1], opt.api.getWidth())];
  118. }
  119. this._centerOption = clone(centerCoord);
  120. this._updateCenterAndZoom();
  121. };
  122. View.prototype.setZoom = function (zoom) {
  123. this._zoom = clampByZoomLimit(zoom || 1, this.zoomLimit);
  124. this._updateCenterAndZoom();
  125. };
  126. /**
  127. * Get default center without roam
  128. */
  129. View.prototype.getDefaultCenter = function () {
  130. // Rect before any transform
  131. var rawRect = this.getBoundingRect();
  132. var cx = rawRect.x + rawRect.width / 2;
  133. var cy = rawRect.y + rawRect.height / 2;
  134. return [cx, cy];
  135. };
  136. View.prototype.getCenter = function () {
  137. return this._center || this.getDefaultCenter();
  138. };
  139. View.prototype.getZoom = function () {
  140. return this._zoom || 1;
  141. };
  142. View.prototype.getRoamTransform = function () {
  143. return this._roamTransformable.getLocalTransform();
  144. };
  145. /**
  146. * Ensure this method is idempotent, since it should be called when
  147. * every relevant prop (e.g. _centerOption/_zoom/_rect/_viewRect) changed.
  148. */
  149. View.prototype._updateCenterAndZoom = function () {
  150. var centerOption = this._centerOption;
  151. var rect = this._rect;
  152. if (centerOption && rect) {
  153. this._center = [parsePercent(centerOption[0], rect.width, rect.x), parsePercent(centerOption[1], rect.height, rect.y)];
  154. }
  155. // Must update after view transform updated
  156. var rawTransformMatrix = this._rawTransformable.getLocalTransform();
  157. var roamTransform = this._roamTransformable;
  158. var defaultCenter = this.getDefaultCenter();
  159. var center = this.getCenter();
  160. var zoom = this.getZoom();
  161. center = vector.applyTransform([], center, rawTransformMatrix);
  162. defaultCenter = vector.applyTransform([], defaultCenter, rawTransformMatrix);
  163. roamTransform.originX = center[0];
  164. roamTransform.originY = center[1];
  165. roamTransform.x = defaultCenter[0] - center[0];
  166. roamTransform.y = defaultCenter[1] - center[1];
  167. roamTransform.scaleX = roamTransform.scaleY = zoom;
  168. this._updateTransform();
  169. };
  170. /**
  171. * Update transform props on `this` based on the current
  172. * `this._roamTransformable` and `this._rawTransformable`.
  173. */
  174. View.prototype._updateTransform = function () {
  175. var roamTransformable = this._roamTransformable;
  176. var rawTransformable = this._rawTransformable;
  177. rawTransformable.parent = roamTransformable;
  178. roamTransformable.updateTransform();
  179. rawTransformable.updateTransform();
  180. matrix.copy(this.transform || (this.transform = []), rawTransformable.transform || matrix.create());
  181. this._rawTransform = rawTransformable.getLocalTransform();
  182. this.invTransform = this.invTransform || [];
  183. matrix.invert(this.invTransform, this.transform);
  184. this.decomposeTransform();
  185. };
  186. View.prototype.getTransformInfo = function () {
  187. var rawTransformable = this._rawTransformable;
  188. var roamTransformable = this._roamTransformable;
  189. // Because roamTransformabel has `originX/originY` modified,
  190. // but the caller of `getTransformInfo` can not handle `originX/originY`,
  191. // so need to recalculate them.
  192. var dummyTransformable = new Transformable();
  193. dummyTransformable.transform = roamTransformable.transform;
  194. dummyTransformable.decomposeTransform();
  195. return {
  196. roam: {
  197. x: dummyTransformable.x,
  198. y: dummyTransformable.y,
  199. scaleX: dummyTransformable.scaleX,
  200. scaleY: dummyTransformable.scaleY
  201. },
  202. raw: {
  203. x: rawTransformable.x,
  204. y: rawTransformable.y,
  205. scaleX: rawTransformable.scaleX,
  206. scaleY: rawTransformable.scaleY
  207. }
  208. };
  209. };
  210. View.prototype.getViewRect = function () {
  211. return this._viewRect;
  212. };
  213. /**
  214. * Get view rect after roam transform
  215. */
  216. View.prototype.getViewRectAfterRoam = function () {
  217. var rect = this.getBoundingRect().clone();
  218. rect.applyTransform(this.transform);
  219. return rect;
  220. };
  221. /**
  222. * Convert a single (lon, lat) data item to (x, y) point.
  223. */
  224. View.prototype.dataToPoint = function (data, noRoam, out) {
  225. var transform = noRoam ? this._rawTransform : this.transform;
  226. out = out || [];
  227. return transform ? v2ApplyTransform(out, data, transform) : vector.copy(out, data);
  228. };
  229. /**
  230. * Convert a (x, y) point to (lon, lat) data
  231. */
  232. View.prototype.pointToData = function (point, reserved, out) {
  233. out = out || [];
  234. var invTransform = this.invTransform;
  235. return invTransform ? v2ApplyTransform(out, point, invTransform) : (out[0] = point[0], out[1] = point[1], out);
  236. };
  237. View.prototype.convertToPixel = function (ecModel, finder, value) {
  238. var coordSys = getCoordSys(finder);
  239. return coordSys === this ? coordSys.dataToPoint(value) : null;
  240. };
  241. View.prototype.convertFromPixel = function (ecModel, finder, pixel) {
  242. var coordSys = getCoordSys(finder);
  243. return coordSys === this ? coordSys.pointToData(pixel) : null;
  244. };
  245. /**
  246. * @implements
  247. */
  248. View.prototype.containPoint = function (point) {
  249. return this.getViewRectAfterRoam().contain(point[0], point[1]);
  250. };
  251. View.dimensions = ['x', 'y'];
  252. return View;
  253. }(Transformable);
  254. function getCoordSys(finder) {
  255. var seriesModel = finder.seriesModel;
  256. return seriesModel ? seriesModel.coordinateSystem : null; // e.g., graph.
  257. }
  258. export default View;