BaseAxisPointer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 * as zrUtil from 'zrender/lib/core/util.js';
  41. import * as graphic from '../../util/graphic.js';
  42. import * as axisPointerModelHelper from './modelHelper.js';
  43. import * as eventTool from 'zrender/lib/core/event.js';
  44. import * as throttleUtil from '../../util/throttle.js';
  45. import { makeInner } from '../../util/model.js';
  46. var inner = makeInner();
  47. var clone = zrUtil.clone;
  48. var bind = zrUtil.bind;
  49. /**
  50. * Base axis pointer class in 2D.
  51. */
  52. var BaseAxisPointer = /** @class */function () {
  53. function BaseAxisPointer() {
  54. this._dragging = false;
  55. /**
  56. * In px, arbitrary value. Do not set too small,
  57. * no animation is ok for most cases.
  58. */
  59. this.animationThreshold = 15;
  60. }
  61. /**
  62. * @implement
  63. */
  64. BaseAxisPointer.prototype.render = function (axisModel, axisPointerModel, api, forceRender) {
  65. var value = axisPointerModel.get('value');
  66. var status = axisPointerModel.get('status');
  67. // Bind them to `this`, not in closure, otherwise they will not
  68. // be replaced when user calling setOption in not merge mode.
  69. this._axisModel = axisModel;
  70. this._axisPointerModel = axisPointerModel;
  71. this._api = api;
  72. // Optimize: `render` will be called repeatedly during mouse move.
  73. // So it is power consuming if performing `render` each time,
  74. // especially on mobile device.
  75. if (!forceRender && this._lastValue === value && this._lastStatus === status) {
  76. return;
  77. }
  78. this._lastValue = value;
  79. this._lastStatus = status;
  80. var group = this._group;
  81. var handle = this._handle;
  82. if (!status || status === 'hide') {
  83. // Do not clear here, for animation better.
  84. group && group.hide();
  85. handle && handle.hide();
  86. return;
  87. }
  88. group && group.show();
  89. handle && handle.show();
  90. // Otherwise status is 'show'
  91. var elOption = {};
  92. this.makeElOption(elOption, value, axisModel, axisPointerModel, api);
  93. // Enable change axis pointer type.
  94. var graphicKey = elOption.graphicKey;
  95. if (graphicKey !== this._lastGraphicKey) {
  96. this.clear(api);
  97. }
  98. this._lastGraphicKey = graphicKey;
  99. var moveAnimation = this._moveAnimation = this.determineAnimation(axisModel, axisPointerModel);
  100. if (!group) {
  101. group = this._group = new graphic.Group();
  102. this.createPointerEl(group, elOption, axisModel, axisPointerModel);
  103. this.createLabelEl(group, elOption, axisModel, axisPointerModel);
  104. api.getZr().add(group);
  105. } else {
  106. var doUpdateProps = zrUtil.curry(updateProps, axisPointerModel, moveAnimation);
  107. this.updatePointerEl(group, elOption, doUpdateProps);
  108. this.updateLabelEl(group, elOption, doUpdateProps, axisPointerModel);
  109. }
  110. updateMandatoryProps(group, axisPointerModel, true);
  111. this._renderHandle(value);
  112. };
  113. /**
  114. * @implement
  115. */
  116. BaseAxisPointer.prototype.remove = function (api) {
  117. this.clear(api);
  118. };
  119. /**
  120. * @implement
  121. */
  122. BaseAxisPointer.prototype.dispose = function (api) {
  123. this.clear(api);
  124. };
  125. /**
  126. * @protected
  127. */
  128. BaseAxisPointer.prototype.determineAnimation = function (axisModel, axisPointerModel) {
  129. var animation = axisPointerModel.get('animation');
  130. var axis = axisModel.axis;
  131. var isCategoryAxis = axis.type === 'category';
  132. var useSnap = axisPointerModel.get('snap');
  133. // Value axis without snap always do not snap.
  134. if (!useSnap && !isCategoryAxis) {
  135. return false;
  136. }
  137. if (animation === 'auto' || animation == null) {
  138. var animationThreshold = this.animationThreshold;
  139. if (isCategoryAxis && axis.getBandWidth() > animationThreshold) {
  140. return true;
  141. }
  142. // It is important to auto animation when snap used. Consider if there is
  143. // a dataZoom, animation will be disabled when too many points exist, while
  144. // it will be enabled for better visual effect when little points exist.
  145. if (useSnap) {
  146. var seriesDataCount = axisPointerModelHelper.getAxisInfo(axisModel).seriesDataCount;
  147. var axisExtent = axis.getExtent();
  148. // Approximate band width
  149. return Math.abs(axisExtent[0] - axisExtent[1]) / seriesDataCount > animationThreshold;
  150. }
  151. return false;
  152. }
  153. return animation === true;
  154. };
  155. /**
  156. * add {pointer, label, graphicKey} to elOption
  157. * @protected
  158. */
  159. BaseAxisPointer.prototype.makeElOption = function (elOption, value, axisModel, axisPointerModel, api) {
  160. // Should be implemenented by sub-class.
  161. };
  162. /**
  163. * @protected
  164. */
  165. BaseAxisPointer.prototype.createPointerEl = function (group, elOption, axisModel, axisPointerModel) {
  166. var pointerOption = elOption.pointer;
  167. if (pointerOption) {
  168. var pointerEl = inner(group).pointerEl = new graphic[pointerOption.type](clone(elOption.pointer));
  169. group.add(pointerEl);
  170. }
  171. };
  172. /**
  173. * @protected
  174. */
  175. BaseAxisPointer.prototype.createLabelEl = function (group, elOption, axisModel, axisPointerModel) {
  176. if (elOption.label) {
  177. var labelEl = inner(group).labelEl = new graphic.Text(clone(elOption.label));
  178. group.add(labelEl);
  179. updateLabelShowHide(labelEl, axisPointerModel);
  180. }
  181. };
  182. /**
  183. * @protected
  184. */
  185. BaseAxisPointer.prototype.updatePointerEl = function (group, elOption, updateProps) {
  186. var pointerEl = inner(group).pointerEl;
  187. if (pointerEl && elOption.pointer) {
  188. pointerEl.setStyle(elOption.pointer.style);
  189. updateProps(pointerEl, {
  190. shape: elOption.pointer.shape
  191. });
  192. }
  193. };
  194. /**
  195. * @protected
  196. */
  197. BaseAxisPointer.prototype.updateLabelEl = function (group, elOption, updateProps, axisPointerModel) {
  198. var labelEl = inner(group).labelEl;
  199. if (labelEl) {
  200. labelEl.setStyle(elOption.label.style);
  201. updateProps(labelEl, {
  202. // Consider text length change in vertical axis, animation should
  203. // be used on shape, otherwise the effect will be weird.
  204. // TODOTODO
  205. // shape: elOption.label.shape,
  206. x: elOption.label.x,
  207. y: elOption.label.y
  208. });
  209. updateLabelShowHide(labelEl, axisPointerModel);
  210. }
  211. };
  212. /**
  213. * @private
  214. */
  215. BaseAxisPointer.prototype._renderHandle = function (value) {
  216. if (this._dragging || !this.updateHandleTransform) {
  217. return;
  218. }
  219. var axisPointerModel = this._axisPointerModel;
  220. var zr = this._api.getZr();
  221. var handle = this._handle;
  222. var handleModel = axisPointerModel.getModel('handle');
  223. var status = axisPointerModel.get('status');
  224. if (!handleModel.get('show') || !status || status === 'hide') {
  225. handle && zr.remove(handle);
  226. this._handle = null;
  227. return;
  228. }
  229. var isInit;
  230. if (!this._handle) {
  231. isInit = true;
  232. handle = this._handle = graphic.createIcon(handleModel.get('icon'), {
  233. cursor: 'move',
  234. draggable: true,
  235. onmousemove: function (e) {
  236. // For mobile device, prevent screen slider on the button.
  237. eventTool.stop(e.event);
  238. },
  239. onmousedown: bind(this._onHandleDragMove, this, 0, 0),
  240. drift: bind(this._onHandleDragMove, this),
  241. ondragend: bind(this._onHandleDragEnd, this)
  242. });
  243. zr.add(handle);
  244. }
  245. updateMandatoryProps(handle, axisPointerModel, false);
  246. // update style
  247. handle.setStyle(handleModel.getItemStyle(null, ['color', 'borderColor', 'borderWidth', 'opacity', 'shadowColor', 'shadowBlur', 'shadowOffsetX', 'shadowOffsetY']));
  248. // update position
  249. var handleSize = handleModel.get('size');
  250. if (!zrUtil.isArray(handleSize)) {
  251. handleSize = [handleSize, handleSize];
  252. }
  253. handle.scaleX = handleSize[0] / 2;
  254. handle.scaleY = handleSize[1] / 2;
  255. throttleUtil.createOrUpdate(this, '_doDispatchAxisPointer', handleModel.get('throttle') || 0, 'fixRate');
  256. this._moveHandleToValue(value, isInit);
  257. };
  258. BaseAxisPointer.prototype._moveHandleToValue = function (value, isInit) {
  259. updateProps(this._axisPointerModel, !isInit && this._moveAnimation, this._handle, getHandleTransProps(this.getHandleTransform(value, this._axisModel, this._axisPointerModel)));
  260. };
  261. BaseAxisPointer.prototype._onHandleDragMove = function (dx, dy) {
  262. var handle = this._handle;
  263. if (!handle) {
  264. return;
  265. }
  266. this._dragging = true;
  267. // Persistent for throttle.
  268. var trans = this.updateHandleTransform(getHandleTransProps(handle), [dx, dy], this._axisModel, this._axisPointerModel);
  269. this._payloadInfo = trans;
  270. handle.stopAnimation();
  271. handle.attr(getHandleTransProps(trans));
  272. inner(handle).lastProp = null;
  273. this._doDispatchAxisPointer();
  274. };
  275. /**
  276. * Throttled method.
  277. */
  278. BaseAxisPointer.prototype._doDispatchAxisPointer = function () {
  279. var handle = this._handle;
  280. if (!handle) {
  281. return;
  282. }
  283. var payloadInfo = this._payloadInfo;
  284. var axisModel = this._axisModel;
  285. this._api.dispatchAction({
  286. type: 'updateAxisPointer',
  287. x: payloadInfo.cursorPoint[0],
  288. y: payloadInfo.cursorPoint[1],
  289. tooltipOption: payloadInfo.tooltipOption,
  290. axesInfo: [{
  291. axisDim: axisModel.axis.dim,
  292. axisIndex: axisModel.componentIndex
  293. }]
  294. });
  295. };
  296. BaseAxisPointer.prototype._onHandleDragEnd = function () {
  297. this._dragging = false;
  298. var handle = this._handle;
  299. if (!handle) {
  300. return;
  301. }
  302. var value = this._axisPointerModel.get('value');
  303. // Consider snap or categroy axis, handle may be not consistent with
  304. // axisPointer. So move handle to align the exact value position when
  305. // drag ended.
  306. this._moveHandleToValue(value);
  307. // For the effect: tooltip will be shown when finger holding on handle
  308. // button, and will be hidden after finger left handle button.
  309. this._api.dispatchAction({
  310. type: 'hideTip'
  311. });
  312. };
  313. /**
  314. * @private
  315. */
  316. BaseAxisPointer.prototype.clear = function (api) {
  317. this._lastValue = null;
  318. this._lastStatus = null;
  319. var zr = api.getZr();
  320. var group = this._group;
  321. var handle = this._handle;
  322. if (zr && group) {
  323. this._lastGraphicKey = null;
  324. group && zr.remove(group);
  325. handle && zr.remove(handle);
  326. this._group = null;
  327. this._handle = null;
  328. this._payloadInfo = null;
  329. }
  330. throttleUtil.clear(this, '_doDispatchAxisPointer');
  331. };
  332. /**
  333. * @protected
  334. */
  335. BaseAxisPointer.prototype.doClear = function () {
  336. // Implemented by sub-class if necessary.
  337. };
  338. BaseAxisPointer.prototype.buildLabel = function (xy, wh, xDimIndex) {
  339. xDimIndex = xDimIndex || 0;
  340. return {
  341. x: xy[xDimIndex],
  342. y: xy[1 - xDimIndex],
  343. width: wh[xDimIndex],
  344. height: wh[1 - xDimIndex]
  345. };
  346. };
  347. return BaseAxisPointer;
  348. }();
  349. function updateProps(animationModel, moveAnimation, el, props) {
  350. // Animation optimize.
  351. if (!propsEqual(inner(el).lastProp, props)) {
  352. inner(el).lastProp = props;
  353. moveAnimation ? graphic.updateProps(el, props, animationModel) : (el.stopAnimation(), el.attr(props));
  354. }
  355. }
  356. function propsEqual(lastProps, newProps) {
  357. if (zrUtil.isObject(lastProps) && zrUtil.isObject(newProps)) {
  358. var equals_1 = true;
  359. zrUtil.each(newProps, function (item, key) {
  360. equals_1 = equals_1 && propsEqual(lastProps[key], item);
  361. });
  362. return !!equals_1;
  363. } else {
  364. return lastProps === newProps;
  365. }
  366. }
  367. function updateLabelShowHide(labelEl, axisPointerModel) {
  368. labelEl[axisPointerModel.get(['label', 'show']) ? 'show' : 'hide']();
  369. }
  370. function getHandleTransProps(trans) {
  371. return {
  372. x: trans.x || 0,
  373. y: trans.y || 0,
  374. rotation: trans.rotation || 0
  375. };
  376. }
  377. function updateMandatoryProps(group, axisPointerModel, silent) {
  378. var z = axisPointerModel.get('z');
  379. var zlevel = axisPointerModel.get('zlevel');
  380. group && group.traverse(function (el) {
  381. if (el.type !== 'group') {
  382. z != null && (el.z = z);
  383. zlevel != null && (el.zlevel = zlevel);
  384. el.silent = silent;
  385. }
  386. });
  387. }
  388. export default BaseAxisPointer;