Symbol.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 } 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, 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: 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 isInit = symbolType !== this._symbolType;
  135. var disableAnimation = opts && opts.disableAnimation;
  136. if (isInit) {
  137. var keepAspect = data.getItemVisual(idx, 'symbolKeepAspect');
  138. this._createSymbol(symbolType, data, idx, symbolSize, keepAspect);
  139. } else {
  140. var symbolPath = this.childAt(0);
  141. symbolPath.silent = false;
  142. var target = {
  143. scaleX: symbolSize[0] / 2,
  144. scaleY: symbolSize[1] / 2
  145. };
  146. disableAnimation ? symbolPath.attr(target) : graphic.updateProps(symbolPath, target, seriesModel, idx);
  147. saveOldStyle(symbolPath);
  148. }
  149. this._updateCommon(data, idx, symbolSize, seriesScope, opts);
  150. if (isInit) {
  151. var symbolPath = this.childAt(0);
  152. if (!disableAnimation) {
  153. var target = {
  154. scaleX: this._sizeX,
  155. scaleY: this._sizeY,
  156. style: {
  157. // Always fadeIn. Because it has fadeOut animation when symbol is removed..
  158. opacity: symbolPath.style.opacity
  159. }
  160. };
  161. symbolPath.scaleX = symbolPath.scaleY = 0;
  162. symbolPath.style.opacity = 0;
  163. graphic.initProps(symbolPath, target, seriesModel, idx);
  164. }
  165. }
  166. if (disableAnimation) {
  167. // Must stop leave transition manually if don't call initProps or updateProps.
  168. this.childAt(0).stopAnimation('leave');
  169. }
  170. };
  171. Symbol.prototype._updateCommon = function (data, idx, symbolSize, seriesScope, opts) {
  172. var symbolPath = this.childAt(0);
  173. var seriesModel = data.hostModel;
  174. var emphasisItemStyle;
  175. var blurItemStyle;
  176. var selectItemStyle;
  177. var focus;
  178. var blurScope;
  179. var emphasisDisabled;
  180. var labelStatesModels;
  181. var hoverScale;
  182. var cursorStyle;
  183. if (seriesScope) {
  184. emphasisItemStyle = seriesScope.emphasisItemStyle;
  185. blurItemStyle = seriesScope.blurItemStyle;
  186. selectItemStyle = seriesScope.selectItemStyle;
  187. focus = seriesScope.focus;
  188. blurScope = seriesScope.blurScope;
  189. labelStatesModels = seriesScope.labelStatesModels;
  190. hoverScale = seriesScope.hoverScale;
  191. cursorStyle = seriesScope.cursorStyle;
  192. emphasisDisabled = seriesScope.emphasisDisabled;
  193. }
  194. if (!seriesScope || data.hasItemOption) {
  195. var itemModel = seriesScope && seriesScope.itemModel ? seriesScope.itemModel : data.getItemModel(idx);
  196. var emphasisModel = itemModel.getModel('emphasis');
  197. emphasisItemStyle = emphasisModel.getModel('itemStyle').getItemStyle();
  198. selectItemStyle = itemModel.getModel(['select', 'itemStyle']).getItemStyle();
  199. blurItemStyle = itemModel.getModel(['blur', 'itemStyle']).getItemStyle();
  200. focus = emphasisModel.get('focus');
  201. blurScope = emphasisModel.get('blurScope');
  202. emphasisDisabled = emphasisModel.get('disabled');
  203. labelStatesModels = getLabelStatesModels(itemModel);
  204. hoverScale = emphasisModel.getShallow('scale');
  205. cursorStyle = itemModel.getShallow('cursor');
  206. }
  207. var symbolRotate = data.getItemVisual(idx, 'symbolRotate');
  208. symbolPath.attr('rotation', (symbolRotate || 0) * Math.PI / 180 || 0);
  209. var symbolOffset = normalizeSymbolOffset(data.getItemVisual(idx, 'symbolOffset'), symbolSize);
  210. if (symbolOffset) {
  211. symbolPath.x = symbolOffset[0];
  212. symbolPath.y = symbolOffset[1];
  213. }
  214. cursorStyle && symbolPath.attr('cursor', cursorStyle);
  215. var symbolStyle = data.getItemVisual(idx, 'style');
  216. var visualColor = symbolStyle.fill;
  217. if (symbolPath instanceof ZRImage) {
  218. var pathStyle = symbolPath.style;
  219. symbolPath.useStyle(extend({
  220. // TODO other properties like x, y ?
  221. image: pathStyle.image,
  222. x: pathStyle.x,
  223. y: pathStyle.y,
  224. width: pathStyle.width,
  225. height: pathStyle.height
  226. }, symbolStyle));
  227. } else {
  228. if (symbolPath.__isEmptyBrush) {
  229. // fill and stroke will be swapped if it's empty.
  230. // So we cloned a new style to avoid it affecting the original style in visual storage.
  231. // TODO Better implementation. No empty logic!
  232. symbolPath.useStyle(extend({}, symbolStyle));
  233. } else {
  234. symbolPath.useStyle(symbolStyle);
  235. }
  236. // Disable decal because symbol scale will been applied on the decal.
  237. symbolPath.style.decal = null;
  238. symbolPath.setColor(visualColor, opts && opts.symbolInnerColor);
  239. symbolPath.style.strokeNoScale = true;
  240. }
  241. var liftZ = data.getItemVisual(idx, 'liftZ');
  242. var z2Origin = this._z2;
  243. if (liftZ != null) {
  244. if (z2Origin == null) {
  245. this._z2 = symbolPath.z2;
  246. symbolPath.z2 += liftZ;
  247. }
  248. } else if (z2Origin != null) {
  249. symbolPath.z2 = z2Origin;
  250. this._z2 = null;
  251. }
  252. var useNameLabel = opts && opts.useNameLabel;
  253. setLabelStyle(symbolPath, labelStatesModels, {
  254. labelFetcher: seriesModel,
  255. labelDataIndex: idx,
  256. defaultText: getLabelDefaultText,
  257. inheritColor: visualColor,
  258. defaultOpacity: symbolStyle.opacity
  259. });
  260. // Do not execute util needed.
  261. function getLabelDefaultText(idx) {
  262. return useNameLabel ? data.getName(idx) : getDefaultLabel(data, idx);
  263. }
  264. this._sizeX = symbolSize[0] / 2;
  265. this._sizeY = symbolSize[1] / 2;
  266. var emphasisState = symbolPath.ensureState('emphasis');
  267. emphasisState.style = emphasisItemStyle;
  268. symbolPath.ensureState('select').style = selectItemStyle;
  269. symbolPath.ensureState('blur').style = blurItemStyle;
  270. // null / undefined / true means to use default strategy.
  271. // 0 / false / negative number / NaN / Infinity means no scale.
  272. var scaleRatio = hoverScale == null || hoverScale === true ? Math.max(1.1, 3 / this._sizeY)
  273. // PENDING: restrict hoverScale > 1? It seems unreasonable to scale down
  274. : isFinite(hoverScale) && hoverScale > 0 ? +hoverScale : 1;
  275. // always set scale to allow resetting
  276. emphasisState.scaleX = this._sizeX * scaleRatio;
  277. emphasisState.scaleY = this._sizeY * scaleRatio;
  278. this.setSymbolScale(1);
  279. toggleHoverEmphasis(this, focus, blurScope, emphasisDisabled);
  280. };
  281. Symbol.prototype.setSymbolScale = function (scale) {
  282. this.scaleX = this.scaleY = scale;
  283. };
  284. Symbol.prototype.fadeOut = function (cb, seriesModel, opt) {
  285. var symbolPath = this.childAt(0);
  286. var dataIndex = getECData(this).dataIndex;
  287. var animationOpt = opt && opt.animation;
  288. // Avoid mistaken hover when fading out
  289. this.silent = symbolPath.silent = true;
  290. // Not show text when animating
  291. if (opt && opt.fadeLabel) {
  292. var textContent = symbolPath.getTextContent();
  293. if (textContent) {
  294. graphic.removeElement(textContent, {
  295. style: {
  296. opacity: 0
  297. }
  298. }, seriesModel, {
  299. dataIndex: dataIndex,
  300. removeOpt: animationOpt,
  301. cb: function () {
  302. symbolPath.removeTextContent();
  303. }
  304. });
  305. }
  306. } else {
  307. symbolPath.removeTextContent();
  308. }
  309. graphic.removeElement(symbolPath, {
  310. style: {
  311. opacity: 0
  312. },
  313. scaleX: 0,
  314. scaleY: 0
  315. }, seriesModel, {
  316. dataIndex: dataIndex,
  317. cb: cb,
  318. removeOpt: animationOpt
  319. });
  320. };
  321. Symbol.getSymbolSize = function (data, idx) {
  322. return normalizeSymbolSize(data.getItemVisual(idx, 'symbolSize'));
  323. };
  324. return Symbol;
  325. }(graphic.Group);
  326. function driftSymbol(dx, dy) {
  327. this.parent.drift(dx, dy);
  328. }
  329. export default Symbol;