Symbol.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 =
  51. /** @class */
  52. function (_super) {
  53. __extends(Symbol, _super);
  54. function Symbol(data, idx, seriesScope, opts) {
  55. var _this = _super.call(this) || this;
  56. _this.updateData(data, idx, seriesScope, opts);
  57. return _this;
  58. }
  59. Symbol.prototype._createSymbol = function (symbolType, data, idx, symbolSize, keepAspect) {
  60. // Remove paths created before
  61. this.removeAll(); // let symbolPath = createSymbol(
  62. // symbolType, -0.5, -0.5, 1, 1, color
  63. // );
  64. // If width/height are set too small (e.g., set to 1) on ios10
  65. // and macOS Sierra, a circle stroke become a rect, no matter what
  66. // the scale is set. So we set width/height as 2. See #4150.
  67. var symbolPath = createSymbol(symbolType, -1, -1, 2, 2, null, keepAspect);
  68. symbolPath.attr({
  69. z2: 100,
  70. culling: true,
  71. scaleX: symbolSize[0] / 2,
  72. scaleY: symbolSize[1] / 2
  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. } // Disable decal because symbol scale will been applied on the decal.
  236. symbolPath.style.decal = null;
  237. symbolPath.setColor(visualColor, opts && opts.symbolInnerColor);
  238. symbolPath.style.strokeNoScale = true;
  239. }
  240. var liftZ = data.getItemVisual(idx, 'liftZ');
  241. var z2Origin = this._z2;
  242. if (liftZ != null) {
  243. if (z2Origin == null) {
  244. this._z2 = symbolPath.z2;
  245. symbolPath.z2 += liftZ;
  246. }
  247. } else if (z2Origin != null) {
  248. symbolPath.z2 = z2Origin;
  249. this._z2 = null;
  250. }
  251. var useNameLabel = opts && opts.useNameLabel;
  252. setLabelStyle(symbolPath, labelStatesModels, {
  253. labelFetcher: seriesModel,
  254. labelDataIndex: idx,
  255. defaultText: getLabelDefaultText,
  256. inheritColor: visualColor,
  257. defaultOpacity: symbolStyle.opacity
  258. }); // Do not execute util needed.
  259. function getLabelDefaultText(idx) {
  260. return useNameLabel ? data.getName(idx) : getDefaultLabel(data, idx);
  261. }
  262. this._sizeX = symbolSize[0] / 2;
  263. this._sizeY = symbolSize[1] / 2;
  264. var emphasisState = symbolPath.ensureState('emphasis');
  265. emphasisState.style = emphasisItemStyle;
  266. symbolPath.ensureState('select').style = selectItemStyle;
  267. symbolPath.ensureState('blur').style = blurItemStyle; // null / undefined / true means to use default strategy.
  268. // 0 / false / negative number / NaN / Infinity means no scale.
  269. var scaleRatio = hoverScale == null || hoverScale === true ? Math.max(1.1, 3 / this._sizeY) // PENDING: restrict hoverScale > 1? It seems unreasonable to scale down
  270. : isFinite(hoverScale) && hoverScale > 0 ? +hoverScale : 1; // always set scale to allow resetting
  271. emphasisState.scaleX = this._sizeX * scaleRatio;
  272. emphasisState.scaleY = this._sizeY * scaleRatio;
  273. this.setSymbolScale(1);
  274. toggleHoverEmphasis(this, focus, blurScope, emphasisDisabled);
  275. };
  276. Symbol.prototype.setSymbolScale = function (scale) {
  277. this.scaleX = this.scaleY = scale;
  278. };
  279. Symbol.prototype.fadeOut = function (cb, seriesModel, opt) {
  280. var symbolPath = this.childAt(0);
  281. var dataIndex = getECData(this).dataIndex;
  282. var animationOpt = opt && opt.animation; // Avoid mistaken hover when fading out
  283. this.silent = symbolPath.silent = true; // Not show text when animating
  284. if (opt && opt.fadeLabel) {
  285. var textContent = symbolPath.getTextContent();
  286. if (textContent) {
  287. graphic.removeElement(textContent, {
  288. style: {
  289. opacity: 0
  290. }
  291. }, seriesModel, {
  292. dataIndex: dataIndex,
  293. removeOpt: animationOpt,
  294. cb: function () {
  295. symbolPath.removeTextContent();
  296. }
  297. });
  298. }
  299. } else {
  300. symbolPath.removeTextContent();
  301. }
  302. graphic.removeElement(symbolPath, {
  303. style: {
  304. opacity: 0
  305. },
  306. scaleX: 0,
  307. scaleY: 0
  308. }, seriesModel, {
  309. dataIndex: dataIndex,
  310. cb: cb,
  311. removeOpt: animationOpt
  312. });
  313. };
  314. Symbol.getSymbolSize = function (data, idx) {
  315. return normalizeSymbolSize(data.getItemVisual(idx, 'symbolSize'));
  316. };
  317. return Symbol;
  318. }(graphic.Group);
  319. function driftSymbol(dx, dy) {
  320. this.parent.drift(dx, dy);
  321. }
  322. export default Symbol;