BaseAxisPointer.js 13 KB

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