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';
  42. import * as graphic from '../../util/graphic';
  43. import { getECData } from '../../util/innerStore';
  44. import { enterEmphasis, leaveEmphasis, enableHoverEmphasis } from '../../util/states';
  45. import { getDefaultLabel } from './labelHelper';
  46. import { extend } from 'zrender/lib/core/util';
  47. import { setLabelStyle, getLabelStatesModels } from '../../label/labelStyle';
  48. import ZRImage from 'zrender/lib/graphic/Image';
  49. import { saveOldStyle } from '../../animation/basicTrasition';
  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) {
  122. var symbolPath = this.childAt(0);
  123. symbolPath.draggable = draggable;
  124. symbolPath.cursor = 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 remove animation manually if don't call initProps or updateProps.
  168. this.childAt(0).stopAnimation('remove');
  169. }
  170. this._seriesModel = seriesModel;
  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 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. }
  193. if (!seriesScope || data.hasItemOption) {
  194. var itemModel = seriesScope && seriesScope.itemModel ? seriesScope.itemModel : data.getItemModel(idx);
  195. var emphasisModel = itemModel.getModel('emphasis');
  196. emphasisItemStyle = emphasisModel.getModel('itemStyle').getItemStyle();
  197. selectItemStyle = itemModel.getModel(['select', 'itemStyle']).getItemStyle();
  198. blurItemStyle = itemModel.getModel(['blur', 'itemStyle']).getItemStyle();
  199. focus = emphasisModel.get('focus');
  200. blurScope = emphasisModel.get('blurScope');
  201. labelStatesModels = getLabelStatesModels(itemModel);
  202. hoverScale = emphasisModel.getShallow('scale');
  203. cursorStyle = itemModel.getShallow('cursor');
  204. }
  205. var symbolRotate = data.getItemVisual(idx, 'symbolRotate');
  206. symbolPath.attr('rotation', (symbolRotate || 0) * Math.PI / 180 || 0);
  207. var symbolOffset = normalizeSymbolOffset(data.getItemVisual(idx, 'symbolOffset'), symbolSize);
  208. if (symbolOffset) {
  209. symbolPath.x = symbolOffset[0];
  210. symbolPath.y = symbolOffset[1];
  211. }
  212. cursorStyle && symbolPath.attr('cursor', cursorStyle);
  213. var symbolStyle = data.getItemVisual(idx, 'style');
  214. var visualColor = symbolStyle.fill;
  215. if (symbolPath instanceof ZRImage) {
  216. var pathStyle = symbolPath.style;
  217. symbolPath.useStyle(extend({
  218. // TODO other properties like x, y ?
  219. image: pathStyle.image,
  220. x: pathStyle.x,
  221. y: pathStyle.y,
  222. width: pathStyle.width,
  223. height: pathStyle.height
  224. }, symbolStyle));
  225. } else {
  226. if (symbolPath.__isEmptyBrush) {
  227. // fill and stroke will be swapped if it's empty.
  228. // So we cloned a new style to avoid it affecting the original style in visual storage.
  229. // TODO Better implementation. No empty logic!
  230. symbolPath.useStyle(extend({}, symbolStyle));
  231. } else {
  232. symbolPath.useStyle(symbolStyle);
  233. } // Disable decal because symbol scale will been applied on the decal.
  234. symbolPath.style.decal = null;
  235. symbolPath.setColor(visualColor, opts && opts.symbolInnerColor);
  236. symbolPath.style.strokeNoScale = true;
  237. }
  238. var liftZ = data.getItemVisual(idx, 'liftZ');
  239. var z2Origin = this._z2;
  240. if (liftZ != null) {
  241. if (z2Origin == null) {
  242. this._z2 = symbolPath.z2;
  243. symbolPath.z2 += liftZ;
  244. }
  245. } else if (z2Origin != null) {
  246. symbolPath.z2 = z2Origin;
  247. this._z2 = null;
  248. }
  249. var useNameLabel = opts && opts.useNameLabel;
  250. setLabelStyle(symbolPath, labelStatesModels, {
  251. labelFetcher: seriesModel,
  252. labelDataIndex: idx,
  253. defaultText: getLabelDefaultText,
  254. inheritColor: visualColor,
  255. defaultOpacity: symbolStyle.opacity
  256. }); // Do not execute util needed.
  257. function getLabelDefaultText(idx) {
  258. return useNameLabel ? data.getName(idx) : getDefaultLabel(data, idx);
  259. }
  260. this._sizeX = symbolSize[0] / 2;
  261. this._sizeY = symbolSize[1] / 2;
  262. var emphasisState = symbolPath.ensureState('emphasis');
  263. emphasisState.style = emphasisItemStyle;
  264. symbolPath.ensureState('select').style = selectItemStyle;
  265. symbolPath.ensureState('blur').style = blurItemStyle;
  266. if (hoverScale) {
  267. var scaleRatio = Math.max(1.1, 3 / this._sizeY);
  268. emphasisState.scaleX = this._sizeX * scaleRatio;
  269. emphasisState.scaleY = this._sizeY * scaleRatio;
  270. }
  271. this.setSymbolScale(1);
  272. enableHoverEmphasis(this, focus, blurScope);
  273. };
  274. Symbol.prototype.setSymbolScale = function (scale) {
  275. this.scaleX = this.scaleY = scale;
  276. };
  277. Symbol.prototype.fadeOut = function (cb, opt) {
  278. var symbolPath = this.childAt(0);
  279. var seriesModel = this._seriesModel;
  280. var dataIndex = getECData(this).dataIndex;
  281. var animationOpt = opt && opt.animation; // Avoid mistaken hover when fading out
  282. this.silent = symbolPath.silent = true; // Not show text when animating
  283. if (opt && opt.fadeLabel) {
  284. var textContent = symbolPath.getTextContent();
  285. if (textContent) {
  286. graphic.removeElement(textContent, {
  287. style: {
  288. opacity: 0
  289. }
  290. }, seriesModel, {
  291. dataIndex: dataIndex,
  292. removeOpt: animationOpt,
  293. cb: function () {
  294. symbolPath.removeTextContent();
  295. }
  296. });
  297. }
  298. } else {
  299. symbolPath.removeTextContent();
  300. }
  301. graphic.removeElement(symbolPath, {
  302. style: {
  303. opacity: 0
  304. },
  305. scaleX: 0,
  306. scaleY: 0
  307. }, seriesModel, {
  308. dataIndex: dataIndex,
  309. cb: cb,
  310. removeOpt: animationOpt
  311. });
  312. };
  313. Symbol.getSymbolSize = function (data, idx) {
  314. return normalizeSymbolSize(data.getItemVisual(idx, 'symbolSize'));
  315. };
  316. return Symbol;
  317. }(graphic.Group);
  318. function driftSymbol(dx, dy) {
  319. this.parent.drift(dx, dy);
  320. }
  321. export default Symbol;