Symbol.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. import { createSymbol, normalizeSymbolOffset, normalizeSymbolSize } from '../../util/symbol.js';
  42. import * as graphic from '../../util/graphic.js';
  43. import { getECData } from '../../util/innerStore.js';
  44. import { enterEmphasis, leaveEmphasis, toggleHoverEmphasis } from '../../util/states.js';
  45. import { getDefaultLabel } from './labelHelper.js';
  46. import { extend, retrieve2 } from 'zrender/lib/core/util.js';
  47. import { setLabelStyle, getLabelStatesModels } from '../../label/labelStyle.js';
  48. import ZRImage from 'zrender/lib/graphic/Image.js';
  49. import { saveOldStyle } from '../../animation/basicTransition.js';
  50. var Symbol = /** @class */function (_super) {
  51. __extends(Symbol, _super);
  52. function Symbol(data, idx, seriesScope, opts) {
  53. var _this = _super.call(this) || this;
  54. _this.updateData(data, idx, seriesScope, opts);
  55. return _this;
  56. }
  57. Symbol.prototype._createSymbol = function (symbolType, data, idx, symbolSize, z2, keepAspect) {
  58. // Remove paths created before
  59. this.removeAll();
  60. // let symbolPath = createSymbol(
  61. // symbolType, -0.5, -0.5, 1, 1, color
  62. // );
  63. // If width/height are set too small (e.g., set to 1) on ios10
  64. // and macOS Sierra, a circle stroke become a rect, no matter what
  65. // the scale is set. So we set width/height as 2. See #4150.
  66. var symbolPath = createSymbol(symbolType, -1, -1, 2, 2, null, keepAspect);
  67. symbolPath.attr({
  68. z2: retrieve2(z2, 100),
  69. culling: true,
  70. scaleX: symbolSize[0] / 2,
  71. scaleY: symbolSize[1] / 2
  72. });
  73. // Rewrite drift method
  74. symbolPath.drift = driftSymbol;
  75. this._symbolType = symbolType;
  76. this.add(symbolPath);
  77. };
  78. /**
  79. * Stop animation
  80. * @param {boolean} toLastFrame
  81. */
  82. Symbol.prototype.stopSymbolAnimation = function (toLastFrame) {
  83. this.childAt(0).stopAnimation(null, toLastFrame);
  84. };
  85. Symbol.prototype.getSymbolType = function () {
  86. return this._symbolType;
  87. };
  88. /**
  89. * FIXME:
  90. * Caution: This method breaks the encapsulation of this module,
  91. * but it indeed brings convenience. So do not use the method
  92. * unless you detailedly know all the implements of `Symbol`,
  93. * especially animation.
  94. *
  95. * Get symbol path element.
  96. */
  97. Symbol.prototype.getSymbolPath = function () {
  98. return this.childAt(0);
  99. };
  100. /**
  101. * Highlight symbol
  102. */
  103. Symbol.prototype.highlight = function () {
  104. enterEmphasis(this.childAt(0));
  105. };
  106. /**
  107. * Downplay symbol
  108. */
  109. Symbol.prototype.downplay = function () {
  110. leaveEmphasis(this.childAt(0));
  111. };
  112. /**
  113. * @param {number} zlevel
  114. * @param {number} z
  115. */
  116. Symbol.prototype.setZ = function (zlevel, z) {
  117. var symbolPath = this.childAt(0);
  118. symbolPath.zlevel = zlevel;
  119. symbolPath.z = z;
  120. };
  121. Symbol.prototype.setDraggable = function (draggable, hasCursorOption) {
  122. var symbolPath = this.childAt(0);
  123. symbolPath.draggable = draggable;
  124. symbolPath.cursor = !hasCursorOption && draggable ? 'move' : symbolPath.cursor;
  125. };
  126. /**
  127. * Update symbol properties
  128. */
  129. Symbol.prototype.updateData = function (data, idx, seriesScope, opts) {
  130. this.silent = false;
  131. var symbolType = data.getItemVisual(idx, 'symbol') || 'circle';
  132. var seriesModel = data.hostModel;
  133. var symbolSize = Symbol.getSymbolSize(data, idx);
  134. var z2 = Symbol.getSymbolZ2(data, idx);
  135. var isInit = symbolType !== this._symbolType;
  136. var disableAnimation = opts && opts.disableAnimation;
  137. if (isInit) {
  138. var keepAspect = data.getItemVisual(idx, 'symbolKeepAspect');
  139. this._createSymbol(symbolType, data, idx, symbolSize, z2, keepAspect);
  140. } else {
  141. var symbolPath = this.childAt(0);
  142. symbolPath.silent = false;
  143. var target = {
  144. scaleX: symbolSize[0] / 2,
  145. scaleY: symbolSize[1] / 2
  146. };
  147. disableAnimation ? symbolPath.attr(target) : graphic.updateProps(symbolPath, target, seriesModel, idx);
  148. saveOldStyle(symbolPath);
  149. }
  150. this._updateCommon(data, idx, symbolSize, seriesScope, opts);
  151. if (isInit) {
  152. var symbolPath = this.childAt(0);
  153. if (!disableAnimation) {
  154. var target = {
  155. scaleX: this._sizeX,
  156. scaleY: this._sizeY,
  157. style: {
  158. // Always fadeIn. Because it has fadeOut animation when symbol is removed..
  159. opacity: symbolPath.style.opacity
  160. }
  161. };
  162. symbolPath.scaleX = symbolPath.scaleY = 0;
  163. symbolPath.style.opacity = 0;
  164. graphic.initProps(symbolPath, target, seriesModel, idx);
  165. }
  166. }
  167. if (disableAnimation) {
  168. // Must stop leave transition manually if don't call initProps or updateProps.
  169. this.childAt(0).stopAnimation('leave');
  170. }
  171. };
  172. Symbol.prototype._updateCommon = function (data, idx, symbolSize, seriesScope, opts) {
  173. var symbolPath = this.childAt(0);
  174. var seriesModel = data.hostModel;
  175. var emphasisItemStyle;
  176. var blurItemStyle;
  177. var selectItemStyle;
  178. var focus;
  179. var blurScope;
  180. var emphasisDisabled;
  181. var labelStatesModels;
  182. var hoverScale;
  183. var cursorStyle;
  184. if (seriesScope) {
  185. emphasisItemStyle = seriesScope.emphasisItemStyle;
  186. blurItemStyle = seriesScope.blurItemStyle;
  187. selectItemStyle = seriesScope.selectItemStyle;
  188. focus = seriesScope.focus;
  189. blurScope = seriesScope.blurScope;
  190. labelStatesModels = seriesScope.labelStatesModels;
  191. hoverScale = seriesScope.hoverScale;
  192. cursorStyle = seriesScope.cursorStyle;
  193. emphasisDisabled = seriesScope.emphasisDisabled;
  194. }
  195. if (!seriesScope || data.hasItemOption) {
  196. var itemModel = seriesScope && seriesScope.itemModel ? seriesScope.itemModel : data.getItemModel(idx);
  197. var emphasisModel = itemModel.getModel('emphasis');
  198. emphasisItemStyle = emphasisModel.getModel('itemStyle').getItemStyle();
  199. selectItemStyle = itemModel.getModel(['select', 'itemStyle']).getItemStyle();
  200. blurItemStyle = itemModel.getModel(['blur', 'itemStyle']).getItemStyle();
  201. focus = emphasisModel.get('focus');
  202. blurScope = emphasisModel.get('blurScope');
  203. emphasisDisabled = emphasisModel.get('disabled');
  204. labelStatesModels = getLabelStatesModels(itemModel);
  205. hoverScale = emphasisModel.getShallow('scale');
  206. cursorStyle = itemModel.getShallow('cursor');
  207. }
  208. var symbolRotate = data.getItemVisual(idx, 'symbolRotate');
  209. symbolPath.attr('rotation', (symbolRotate || 0) * Math.PI / 180 || 0);
  210. var symbolOffset = normalizeSymbolOffset(data.getItemVisual(idx, 'symbolOffset'), symbolSize);
  211. if (symbolOffset) {
  212. symbolPath.x = symbolOffset[0];
  213. symbolPath.y = symbolOffset[1];
  214. }
  215. cursorStyle && symbolPath.attr('cursor', cursorStyle);
  216. var symbolStyle = data.getItemVisual(idx, 'style');
  217. var visualColor = symbolStyle.fill;
  218. if (symbolPath instanceof ZRImage) {
  219. var pathStyle = symbolPath.style;
  220. symbolPath.useStyle(extend({
  221. // TODO other properties like x, y ?
  222. image: pathStyle.image,
  223. x: pathStyle.x,
  224. y: pathStyle.y,
  225. width: pathStyle.width,
  226. height: pathStyle.height
  227. }, symbolStyle));
  228. } else {
  229. if (symbolPath.__isEmptyBrush) {
  230. // fill and stroke will be swapped if it's empty.
  231. // So we cloned a new style to avoid it affecting the original style in visual storage.
  232. // TODO Better implementation. No empty logic!
  233. symbolPath.useStyle(extend({}, symbolStyle));
  234. } else {
  235. symbolPath.useStyle(symbolStyle);
  236. }
  237. // Disable decal because symbol scale will been applied on the decal.
  238. symbolPath.style.decal = null;
  239. symbolPath.setColor(visualColor, opts && opts.symbolInnerColor);
  240. symbolPath.style.strokeNoScale = true;
  241. }
  242. var liftZ = data.getItemVisual(idx, 'liftZ');
  243. var z2Origin = this._z2;
  244. if (liftZ != null) {
  245. if (z2Origin == null) {
  246. this._z2 = symbolPath.z2;
  247. symbolPath.z2 += liftZ;
  248. }
  249. } else if (z2Origin != null) {
  250. symbolPath.z2 = z2Origin;
  251. this._z2 = null;
  252. }
  253. var useNameLabel = opts && opts.useNameLabel;
  254. setLabelStyle(symbolPath, labelStatesModels, {
  255. labelFetcher: seriesModel,
  256. labelDataIndex: idx,
  257. defaultText: getLabelDefaultText,
  258. inheritColor: visualColor,
  259. defaultOpacity: symbolStyle.opacity
  260. });
  261. // Do not execute util needed.
  262. function getLabelDefaultText(idx) {
  263. return useNameLabel ? data.getName(idx) : getDefaultLabel(data, idx);
  264. }
  265. this._sizeX = symbolSize[0] / 2;
  266. this._sizeY = symbolSize[1] / 2;
  267. var emphasisState = symbolPath.ensureState('emphasis');
  268. emphasisState.style = emphasisItemStyle;
  269. symbolPath.ensureState('select').style = selectItemStyle;
  270. symbolPath.ensureState('blur').style = blurItemStyle;
  271. // null / undefined / true means to use default strategy.
  272. // 0 / false / negative number / NaN / Infinity means no scale.
  273. var scaleRatio = hoverScale == null || hoverScale === true ? Math.max(1.1, 3 / this._sizeY)
  274. // PENDING: restrict hoverScale > 1? It seems unreasonable to scale down
  275. : isFinite(hoverScale) && hoverScale > 0 ? +hoverScale : 1;
  276. // always set scale to allow resetting
  277. emphasisState.scaleX = this._sizeX * scaleRatio;
  278. emphasisState.scaleY = this._sizeY * scaleRatio;
  279. this.setSymbolScale(1);
  280. toggleHoverEmphasis(this, focus, blurScope, emphasisDisabled);
  281. };
  282. Symbol.prototype.setSymbolScale = function (scale) {
  283. this.scaleX = this.scaleY = scale;
  284. };
  285. Symbol.prototype.fadeOut = function (cb, seriesModel, opt) {
  286. var symbolPath = this.childAt(0);
  287. var dataIndex = getECData(this).dataIndex;
  288. var animationOpt = opt && opt.animation;
  289. // Avoid mistaken hover when fading out
  290. this.silent = symbolPath.silent = true;
  291. // Not show text when animating
  292. if (opt && opt.fadeLabel) {
  293. var textContent = symbolPath.getTextContent();
  294. if (textContent) {
  295. graphic.removeElement(textContent, {
  296. style: {
  297. opacity: 0
  298. }
  299. }, seriesModel, {
  300. dataIndex: dataIndex,
  301. removeOpt: animationOpt,
  302. cb: function () {
  303. symbolPath.removeTextContent();
  304. }
  305. });
  306. }
  307. } else {
  308. symbolPath.removeTextContent();
  309. }
  310. graphic.removeElement(symbolPath, {
  311. style: {
  312. opacity: 0
  313. },
  314. scaleX: 0,
  315. scaleY: 0
  316. }, seriesModel, {
  317. dataIndex: dataIndex,
  318. cb: cb,
  319. removeOpt: animationOpt
  320. });
  321. };
  322. Symbol.getSymbolSize = function (data, idx) {
  323. return normalizeSymbolSize(data.getItemVisual(idx, 'symbolSize'));
  324. };
  325. Symbol.getSymbolZ2 = function (data, idx) {
  326. return data.getItemVisual(idx, 'z2');
  327. };
  328. return Symbol;
  329. }(graphic.Group);
  330. function driftSymbol(dx, dy) {
  331. this.parent.drift(dx, dy);
  332. }
  333. export default Symbol;