PictorialBarView.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 * as zrUtil from 'zrender/lib/core/util.js';
  42. import * as graphic from '../../util/graphic.js';
  43. import { toggleHoverEmphasis } from '../../util/states.js';
  44. import { createSymbol, normalizeSymbolOffset } from '../../util/symbol.js';
  45. import { parsePercent, isNumeric } from '../../util/number.js';
  46. import ChartView from '../../view/Chart.js';
  47. import { getDefaultLabel } from '../helper/labelHelper.js';
  48. import { setLabelStyle, getLabelStatesModels } from '../../label/labelStyle.js';
  49. import ZRImage from 'zrender/lib/graphic/Image.js';
  50. import { getECData } from '../../util/innerStore.js';
  51. import { createClipPath } from '../helper/createClipPathFromCoordSys.js';
  52. var BAR_BORDER_WIDTH_QUERY = ['itemStyle', 'borderWidth'];
  53. // index: +isHorizontal
  54. var LAYOUT_ATTRS = [{
  55. xy: 'x',
  56. wh: 'width',
  57. index: 0,
  58. posDesc: ['left', 'right']
  59. }, {
  60. xy: 'y',
  61. wh: 'height',
  62. index: 1,
  63. posDesc: ['top', 'bottom']
  64. }];
  65. var pathForLineWidth = new graphic.Circle();
  66. var PictorialBarView = /** @class */function (_super) {
  67. __extends(PictorialBarView, _super);
  68. function PictorialBarView() {
  69. var _this = _super !== null && _super.apply(this, arguments) || this;
  70. _this.type = PictorialBarView.type;
  71. return _this;
  72. }
  73. PictorialBarView.prototype.render = function (seriesModel, ecModel, api) {
  74. var group = this.group;
  75. var data = seriesModel.getData();
  76. var oldData = this._data;
  77. var cartesian = seriesModel.coordinateSystem;
  78. var baseAxis = cartesian.getBaseAxis();
  79. var isHorizontal = baseAxis.isHorizontal();
  80. var coordSysRect = cartesian.master.getRect();
  81. var opt = {
  82. ecSize: {
  83. width: api.getWidth(),
  84. height: api.getHeight()
  85. },
  86. seriesModel: seriesModel,
  87. coordSys: cartesian,
  88. coordSysExtent: [[coordSysRect.x, coordSysRect.x + coordSysRect.width], [coordSysRect.y, coordSysRect.y + coordSysRect.height]],
  89. isHorizontal: isHorizontal,
  90. valueDim: LAYOUT_ATTRS[+isHorizontal],
  91. categoryDim: LAYOUT_ATTRS[1 - +isHorizontal]
  92. };
  93. data.diff(oldData).add(function (dataIndex) {
  94. if (!data.hasValue(dataIndex)) {
  95. return;
  96. }
  97. var itemModel = getItemModel(data, dataIndex);
  98. var symbolMeta = getSymbolMeta(data, dataIndex, itemModel, opt);
  99. var bar = createBar(data, opt, symbolMeta);
  100. data.setItemGraphicEl(dataIndex, bar);
  101. group.add(bar);
  102. updateCommon(bar, opt, symbolMeta);
  103. }).update(function (newIndex, oldIndex) {
  104. var bar = oldData.getItemGraphicEl(oldIndex);
  105. if (!data.hasValue(newIndex)) {
  106. group.remove(bar);
  107. return;
  108. }
  109. var itemModel = getItemModel(data, newIndex);
  110. var symbolMeta = getSymbolMeta(data, newIndex, itemModel, opt);
  111. var pictorialShapeStr = getShapeStr(data, symbolMeta);
  112. if (bar && pictorialShapeStr !== bar.__pictorialShapeStr) {
  113. group.remove(bar);
  114. data.setItemGraphicEl(newIndex, null);
  115. bar = null;
  116. }
  117. if (bar) {
  118. updateBar(bar, opt, symbolMeta);
  119. } else {
  120. bar = createBar(data, opt, symbolMeta, true);
  121. }
  122. data.setItemGraphicEl(newIndex, bar);
  123. bar.__pictorialSymbolMeta = symbolMeta;
  124. // Add back
  125. group.add(bar);
  126. updateCommon(bar, opt, symbolMeta);
  127. }).remove(function (dataIndex) {
  128. var bar = oldData.getItemGraphicEl(dataIndex);
  129. bar && removeBar(oldData, dataIndex, bar.__pictorialSymbolMeta.animationModel, bar);
  130. }).execute();
  131. // Do clipping
  132. var clipPath = seriesModel.get('clip', true) ? createClipPath(seriesModel.coordinateSystem, false, seriesModel) : null;
  133. if (clipPath) {
  134. group.setClipPath(clipPath);
  135. } else {
  136. group.removeClipPath();
  137. }
  138. this._data = data;
  139. return this.group;
  140. };
  141. PictorialBarView.prototype.remove = function (ecModel, api) {
  142. var group = this.group;
  143. var data = this._data;
  144. if (ecModel.get('animation')) {
  145. if (data) {
  146. data.eachItemGraphicEl(function (bar) {
  147. removeBar(data, getECData(bar).dataIndex, ecModel, bar);
  148. });
  149. }
  150. } else {
  151. group.removeAll();
  152. }
  153. };
  154. PictorialBarView.type = 'pictorialBar';
  155. return PictorialBarView;
  156. }(ChartView);
  157. // Set or calculate default value about symbol, and calculate layout info.
  158. function getSymbolMeta(data, dataIndex, itemModel, opt) {
  159. var layout = data.getItemLayout(dataIndex);
  160. var symbolRepeat = itemModel.get('symbolRepeat');
  161. var symbolClip = itemModel.get('symbolClip');
  162. var symbolPosition = itemModel.get('symbolPosition') || 'start';
  163. var symbolRotate = itemModel.get('symbolRotate');
  164. var rotation = (symbolRotate || 0) * Math.PI / 180 || 0;
  165. var symbolPatternSize = itemModel.get('symbolPatternSize') || 2;
  166. var isAnimationEnabled = itemModel.isAnimationEnabled();
  167. var symbolMeta = {
  168. dataIndex: dataIndex,
  169. layout: layout,
  170. itemModel: itemModel,
  171. symbolType: data.getItemVisual(dataIndex, 'symbol') || 'circle',
  172. style: data.getItemVisual(dataIndex, 'style'),
  173. symbolClip: symbolClip,
  174. symbolRepeat: symbolRepeat,
  175. symbolRepeatDirection: itemModel.get('symbolRepeatDirection'),
  176. symbolPatternSize: symbolPatternSize,
  177. rotation: rotation,
  178. animationModel: isAnimationEnabled ? itemModel : null,
  179. hoverScale: isAnimationEnabled && itemModel.get(['emphasis', 'scale']),
  180. z2: itemModel.getShallow('z', true) || 0
  181. };
  182. prepareBarLength(itemModel, symbolRepeat, layout, opt, symbolMeta);
  183. prepareSymbolSize(data, dataIndex, layout, symbolRepeat, symbolClip, symbolMeta.boundingLength, symbolMeta.pxSign, symbolPatternSize, opt, symbolMeta);
  184. prepareLineWidth(itemModel, symbolMeta.symbolScale, rotation, opt, symbolMeta);
  185. var symbolSize = symbolMeta.symbolSize;
  186. var symbolOffset = normalizeSymbolOffset(itemModel.get('symbolOffset'), symbolSize);
  187. prepareLayoutInfo(itemModel, symbolSize, layout, symbolRepeat, symbolClip, symbolOffset, symbolPosition, symbolMeta.valueLineWidth, symbolMeta.boundingLength, symbolMeta.repeatCutLength, opt, symbolMeta);
  188. return symbolMeta;
  189. }
  190. // bar length can be negative.
  191. function prepareBarLength(itemModel, symbolRepeat, layout, opt, outputSymbolMeta) {
  192. var valueDim = opt.valueDim;
  193. var symbolBoundingData = itemModel.get('symbolBoundingData');
  194. var valueAxis = opt.coordSys.getOtherAxis(opt.coordSys.getBaseAxis());
  195. var zeroPx = valueAxis.toGlobalCoord(valueAxis.dataToCoord(0));
  196. var pxSignIdx = 1 - +(layout[valueDim.wh] <= 0);
  197. var boundingLength;
  198. if (zrUtil.isArray(symbolBoundingData)) {
  199. var symbolBoundingExtent = [convertToCoordOnAxis(valueAxis, symbolBoundingData[0]) - zeroPx, convertToCoordOnAxis(valueAxis, symbolBoundingData[1]) - zeroPx];
  200. symbolBoundingExtent[1] < symbolBoundingExtent[0] && symbolBoundingExtent.reverse();
  201. boundingLength = symbolBoundingExtent[pxSignIdx];
  202. } else if (symbolBoundingData != null) {
  203. boundingLength = convertToCoordOnAxis(valueAxis, symbolBoundingData) - zeroPx;
  204. } else if (symbolRepeat) {
  205. boundingLength = opt.coordSysExtent[valueDim.index][pxSignIdx] - zeroPx;
  206. } else {
  207. boundingLength = layout[valueDim.wh];
  208. }
  209. outputSymbolMeta.boundingLength = boundingLength;
  210. if (symbolRepeat) {
  211. outputSymbolMeta.repeatCutLength = layout[valueDim.wh];
  212. }
  213. // if 'pxSign' means sign of pixel, it can't be zero, or symbolScale will be zero
  214. // and when borderWidth be settled, the actual linewidth will be NaN
  215. outputSymbolMeta.pxSign = boundingLength > 0 ? 1 : -1;
  216. }
  217. function convertToCoordOnAxis(axis, value) {
  218. return axis.toGlobalCoord(axis.dataToCoord(axis.scale.parse(value)));
  219. }
  220. // Support ['100%', '100%']
  221. function prepareSymbolSize(data, dataIndex, layout, symbolRepeat, symbolClip, boundingLength, pxSign, symbolPatternSize, opt, outputSymbolMeta) {
  222. var valueDim = opt.valueDim;
  223. var categoryDim = opt.categoryDim;
  224. var categorySize = Math.abs(layout[categoryDim.wh]);
  225. var symbolSize = data.getItemVisual(dataIndex, 'symbolSize');
  226. var parsedSymbolSize;
  227. if (zrUtil.isArray(symbolSize)) {
  228. parsedSymbolSize = symbolSize.slice();
  229. } else {
  230. if (symbolSize == null) {
  231. // will parse to number below
  232. parsedSymbolSize = ['100%', '100%'];
  233. } else {
  234. parsedSymbolSize = [symbolSize, symbolSize];
  235. }
  236. }
  237. // Note: percentage symbolSize (like '100%') do not consider lineWidth, because it is
  238. // to complicated to calculate real percent value if considering scaled lineWidth.
  239. // So the actual size will bigger than layout size if lineWidth is bigger than zero,
  240. // which can be tolerated in pictorial chart.
  241. parsedSymbolSize[categoryDim.index] = parsePercent(parsedSymbolSize[categoryDim.index], categorySize);
  242. parsedSymbolSize[valueDim.index] = parsePercent(parsedSymbolSize[valueDim.index], symbolRepeat ? categorySize : Math.abs(boundingLength));
  243. outputSymbolMeta.symbolSize = parsedSymbolSize;
  244. // If x or y is less than zero, show reversed shape.
  245. var symbolScale = outputSymbolMeta.symbolScale = [parsedSymbolSize[0] / symbolPatternSize, parsedSymbolSize[1] / symbolPatternSize];
  246. // Follow convention, 'right' and 'top' is the normal scale.
  247. symbolScale[valueDim.index] *= (opt.isHorizontal ? -1 : 1) * pxSign;
  248. }
  249. function prepareLineWidth(itemModel, symbolScale, rotation, opt, outputSymbolMeta) {
  250. // In symbols are drawn with scale, so do not need to care about the case that width
  251. // or height are too small. But symbol use strokeNoScale, where acture lineWidth should
  252. // be calculated.
  253. var valueLineWidth = itemModel.get(BAR_BORDER_WIDTH_QUERY) || 0;
  254. if (valueLineWidth) {
  255. pathForLineWidth.attr({
  256. scaleX: symbolScale[0],
  257. scaleY: symbolScale[1],
  258. rotation: rotation
  259. });
  260. pathForLineWidth.updateTransform();
  261. valueLineWidth /= pathForLineWidth.getLineScale();
  262. valueLineWidth *= symbolScale[opt.valueDim.index];
  263. }
  264. outputSymbolMeta.valueLineWidth = valueLineWidth || 0;
  265. }
  266. function prepareLayoutInfo(itemModel, symbolSize, layout, symbolRepeat, symbolClip, symbolOffset, symbolPosition, valueLineWidth, boundingLength, repeatCutLength, opt, outputSymbolMeta) {
  267. var categoryDim = opt.categoryDim;
  268. var valueDim = opt.valueDim;
  269. var pxSign = outputSymbolMeta.pxSign;
  270. var unitLength = Math.max(symbolSize[valueDim.index] + valueLineWidth, 0);
  271. var pathLen = unitLength;
  272. // Note: rotation will not effect the layout of symbols, because user may
  273. // want symbols to rotate on its center, which should not be translated
  274. // when rotating.
  275. if (symbolRepeat) {
  276. var absBoundingLength = Math.abs(boundingLength);
  277. var symbolMargin = zrUtil.retrieve(itemModel.get('symbolMargin'), '15%') + '';
  278. var hasEndGap = false;
  279. if (symbolMargin.lastIndexOf('!') === symbolMargin.length - 1) {
  280. hasEndGap = true;
  281. symbolMargin = symbolMargin.slice(0, symbolMargin.length - 1);
  282. }
  283. var symbolMarginNumeric = parsePercent(symbolMargin, symbolSize[valueDim.index]);
  284. var uLenWithMargin = Math.max(unitLength + symbolMarginNumeric * 2, 0);
  285. // When symbol margin is less than 0, margin at both ends will be subtracted
  286. // to ensure that all of the symbols will not be overflow the given area.
  287. var endFix = hasEndGap ? 0 : symbolMarginNumeric * 2;
  288. // Both final repeatTimes and final symbolMarginNumeric area calculated based on
  289. // boundingLength.
  290. var repeatSpecified = isNumeric(symbolRepeat);
  291. var repeatTimes = repeatSpecified ? symbolRepeat : toIntTimes((absBoundingLength + endFix) / uLenWithMargin);
  292. // Adjust calculate margin, to ensure each symbol is displayed
  293. // entirely in the given layout area.
  294. var mDiff = absBoundingLength - repeatTimes * unitLength;
  295. symbolMarginNumeric = mDiff / 2 / (hasEndGap ? repeatTimes : Math.max(repeatTimes - 1, 1));
  296. uLenWithMargin = unitLength + symbolMarginNumeric * 2;
  297. endFix = hasEndGap ? 0 : symbolMarginNumeric * 2;
  298. // Update repeatTimes when not all symbol will be shown.
  299. if (!repeatSpecified && symbolRepeat !== 'fixed') {
  300. repeatTimes = repeatCutLength ? toIntTimes((Math.abs(repeatCutLength) + endFix) / uLenWithMargin) : 0;
  301. }
  302. pathLen = repeatTimes * uLenWithMargin - endFix;
  303. outputSymbolMeta.repeatTimes = repeatTimes;
  304. outputSymbolMeta.symbolMargin = symbolMarginNumeric;
  305. }
  306. var sizeFix = pxSign * (pathLen / 2);
  307. var pathPosition = outputSymbolMeta.pathPosition = [];
  308. pathPosition[categoryDim.index] = layout[categoryDim.wh] / 2;
  309. pathPosition[valueDim.index] = symbolPosition === 'start' ? sizeFix : symbolPosition === 'end' ? boundingLength - sizeFix : boundingLength / 2; // 'center'
  310. if (symbolOffset) {
  311. pathPosition[0] += symbolOffset[0];
  312. pathPosition[1] += symbolOffset[1];
  313. }
  314. var bundlePosition = outputSymbolMeta.bundlePosition = [];
  315. bundlePosition[categoryDim.index] = layout[categoryDim.xy];
  316. bundlePosition[valueDim.index] = layout[valueDim.xy];
  317. var barRectShape = outputSymbolMeta.barRectShape = zrUtil.extend({}, layout);
  318. barRectShape[valueDim.wh] = pxSign * Math.max(Math.abs(layout[valueDim.wh]), Math.abs(pathPosition[valueDim.index] + sizeFix));
  319. barRectShape[categoryDim.wh] = layout[categoryDim.wh];
  320. var clipShape = outputSymbolMeta.clipShape = {};
  321. // Consider that symbol may be overflow layout rect.
  322. clipShape[categoryDim.xy] = -layout[categoryDim.xy];
  323. clipShape[categoryDim.wh] = opt.ecSize[categoryDim.wh];
  324. clipShape[valueDim.xy] = 0;
  325. clipShape[valueDim.wh] = layout[valueDim.wh];
  326. }
  327. function createPath(symbolMeta) {
  328. var symbolPatternSize = symbolMeta.symbolPatternSize;
  329. var path = createSymbol(
  330. // Consider texture img, make a big size.
  331. symbolMeta.symbolType, -symbolPatternSize / 2, -symbolPatternSize / 2, symbolPatternSize, symbolPatternSize);
  332. path.attr({
  333. culling: true
  334. });
  335. path.type !== 'image' && path.setStyle({
  336. strokeNoScale: true
  337. });
  338. return path;
  339. }
  340. function createOrUpdateRepeatSymbols(bar, opt, symbolMeta, isUpdate) {
  341. var bundle = bar.__pictorialBundle;
  342. var symbolSize = symbolMeta.symbolSize;
  343. var valueLineWidth = symbolMeta.valueLineWidth;
  344. var pathPosition = symbolMeta.pathPosition;
  345. var valueDim = opt.valueDim;
  346. var repeatTimes = symbolMeta.repeatTimes || 0;
  347. var index = 0;
  348. var unit = symbolSize[opt.valueDim.index] + valueLineWidth + symbolMeta.symbolMargin * 2;
  349. eachPath(bar, function (path) {
  350. path.__pictorialAnimationIndex = index;
  351. path.__pictorialRepeatTimes = repeatTimes;
  352. if (index < repeatTimes) {
  353. updateAttr(path, null, makeTarget(index), symbolMeta, isUpdate);
  354. } else {
  355. updateAttr(path, null, {
  356. scaleX: 0,
  357. scaleY: 0
  358. }, symbolMeta, isUpdate, function () {
  359. bundle.remove(path);
  360. });
  361. }
  362. // updateHoverAnimation(path, symbolMeta);
  363. index++;
  364. });
  365. for (; index < repeatTimes; index++) {
  366. var path = createPath(symbolMeta);
  367. path.__pictorialAnimationIndex = index;
  368. path.__pictorialRepeatTimes = repeatTimes;
  369. bundle.add(path);
  370. var target = makeTarget(index);
  371. updateAttr(path, {
  372. x: target.x,
  373. y: target.y,
  374. scaleX: 0,
  375. scaleY: 0
  376. }, {
  377. scaleX: target.scaleX,
  378. scaleY: target.scaleY,
  379. rotation: target.rotation
  380. }, symbolMeta, isUpdate);
  381. }
  382. function makeTarget(index) {
  383. var position = pathPosition.slice();
  384. // (start && pxSign > 0) || (end && pxSign < 0): i = repeatTimes - index
  385. // Otherwise: i = index;
  386. var pxSign = symbolMeta.pxSign;
  387. var i = index;
  388. if (symbolMeta.symbolRepeatDirection === 'start' ? pxSign > 0 : pxSign < 0) {
  389. i = repeatTimes - 1 - index;
  390. }
  391. position[valueDim.index] = unit * (i - repeatTimes / 2 + 0.5) + pathPosition[valueDim.index];
  392. return {
  393. x: position[0],
  394. y: position[1],
  395. scaleX: symbolMeta.symbolScale[0],
  396. scaleY: symbolMeta.symbolScale[1],
  397. rotation: symbolMeta.rotation
  398. };
  399. }
  400. }
  401. function createOrUpdateSingleSymbol(bar, opt, symbolMeta, isUpdate) {
  402. var bundle = bar.__pictorialBundle;
  403. var mainPath = bar.__pictorialMainPath;
  404. if (!mainPath) {
  405. mainPath = bar.__pictorialMainPath = createPath(symbolMeta);
  406. bundle.add(mainPath);
  407. updateAttr(mainPath, {
  408. x: symbolMeta.pathPosition[0],
  409. y: symbolMeta.pathPosition[1],
  410. scaleX: 0,
  411. scaleY: 0,
  412. rotation: symbolMeta.rotation
  413. }, {
  414. scaleX: symbolMeta.symbolScale[0],
  415. scaleY: symbolMeta.symbolScale[1]
  416. }, symbolMeta, isUpdate);
  417. } else {
  418. updateAttr(mainPath, null, {
  419. x: symbolMeta.pathPosition[0],
  420. y: symbolMeta.pathPosition[1],
  421. scaleX: symbolMeta.symbolScale[0],
  422. scaleY: symbolMeta.symbolScale[1],
  423. rotation: symbolMeta.rotation
  424. }, symbolMeta, isUpdate);
  425. }
  426. }
  427. // bar rect is used for label.
  428. function createOrUpdateBarRect(bar, symbolMeta, isUpdate) {
  429. var rectShape = zrUtil.extend({}, symbolMeta.barRectShape);
  430. var barRect = bar.__pictorialBarRect;
  431. if (!barRect) {
  432. barRect = bar.__pictorialBarRect = new graphic.Rect({
  433. z2: 2,
  434. shape: rectShape,
  435. silent: true,
  436. style: {
  437. stroke: 'transparent',
  438. fill: 'transparent',
  439. lineWidth: 0
  440. }
  441. });
  442. barRect.disableMorphing = true;
  443. bar.add(barRect);
  444. } else {
  445. updateAttr(barRect, null, {
  446. shape: rectShape
  447. }, symbolMeta, isUpdate);
  448. }
  449. }
  450. function createOrUpdateClip(bar, opt, symbolMeta, isUpdate) {
  451. // If not clip, symbol will be remove and rebuilt.
  452. if (symbolMeta.symbolClip) {
  453. var clipPath = bar.__pictorialClipPath;
  454. var clipShape = zrUtil.extend({}, symbolMeta.clipShape);
  455. var valueDim = opt.valueDim;
  456. var animationModel = symbolMeta.animationModel;
  457. var dataIndex = symbolMeta.dataIndex;
  458. if (clipPath) {
  459. graphic.updateProps(clipPath, {
  460. shape: clipShape
  461. }, animationModel, dataIndex);
  462. } else {
  463. clipShape[valueDim.wh] = 0;
  464. clipPath = new graphic.Rect({
  465. shape: clipShape
  466. });
  467. bar.__pictorialBundle.setClipPath(clipPath);
  468. bar.__pictorialClipPath = clipPath;
  469. var target = {};
  470. target[valueDim.wh] = symbolMeta.clipShape[valueDim.wh];
  471. graphic[isUpdate ? 'updateProps' : 'initProps'](clipPath, {
  472. shape: target
  473. }, animationModel, dataIndex);
  474. }
  475. }
  476. }
  477. function getItemModel(data, dataIndex) {
  478. var itemModel = data.getItemModel(dataIndex);
  479. itemModel.getAnimationDelayParams = getAnimationDelayParams;
  480. itemModel.isAnimationEnabled = isAnimationEnabled;
  481. return itemModel;
  482. }
  483. function getAnimationDelayParams(path) {
  484. // The order is the same as the z-order, see `symbolRepeatDiretion`.
  485. return {
  486. index: path.__pictorialAnimationIndex,
  487. count: path.__pictorialRepeatTimes
  488. };
  489. }
  490. function isAnimationEnabled() {
  491. // `animation` prop can be set on itemModel in pictorial bar chart.
  492. return this.parentModel.isAnimationEnabled() && !!this.getShallow('animation');
  493. }
  494. function createBar(data, opt, symbolMeta, isUpdate) {
  495. // bar is the main element for each data.
  496. var bar = new graphic.Group();
  497. // bundle is used for location and clip.
  498. var bundle = new graphic.Group();
  499. bar.add(bundle);
  500. bar.__pictorialBundle = bundle;
  501. bundle.x = symbolMeta.bundlePosition[0];
  502. bundle.y = symbolMeta.bundlePosition[1];
  503. if (symbolMeta.symbolRepeat) {
  504. createOrUpdateRepeatSymbols(bar, opt, symbolMeta);
  505. } else {
  506. createOrUpdateSingleSymbol(bar, opt, symbolMeta);
  507. }
  508. createOrUpdateBarRect(bar, symbolMeta, isUpdate);
  509. createOrUpdateClip(bar, opt, symbolMeta, isUpdate);
  510. bar.__pictorialShapeStr = getShapeStr(data, symbolMeta);
  511. bar.__pictorialSymbolMeta = symbolMeta;
  512. return bar;
  513. }
  514. function updateBar(bar, opt, symbolMeta) {
  515. var animationModel = symbolMeta.animationModel;
  516. var dataIndex = symbolMeta.dataIndex;
  517. var bundle = bar.__pictorialBundle;
  518. graphic.updateProps(bundle, {
  519. x: symbolMeta.bundlePosition[0],
  520. y: symbolMeta.bundlePosition[1]
  521. }, animationModel, dataIndex);
  522. if (symbolMeta.symbolRepeat) {
  523. createOrUpdateRepeatSymbols(bar, opt, symbolMeta, true);
  524. } else {
  525. createOrUpdateSingleSymbol(bar, opt, symbolMeta, true);
  526. }
  527. createOrUpdateBarRect(bar, symbolMeta, true);
  528. createOrUpdateClip(bar, opt, symbolMeta, true);
  529. }
  530. function removeBar(data, dataIndex, animationModel, bar) {
  531. // Not show text when animating
  532. var labelRect = bar.__pictorialBarRect;
  533. labelRect && labelRect.removeTextContent();
  534. var paths = [];
  535. eachPath(bar, function (path) {
  536. paths.push(path);
  537. });
  538. bar.__pictorialMainPath && paths.push(bar.__pictorialMainPath);
  539. // I do not find proper remove animation for clip yet.
  540. bar.__pictorialClipPath && (animationModel = null);
  541. zrUtil.each(paths, function (path) {
  542. graphic.removeElement(path, {
  543. scaleX: 0,
  544. scaleY: 0
  545. }, animationModel, dataIndex, function () {
  546. bar.parent && bar.parent.remove(bar);
  547. });
  548. });
  549. data.setItemGraphicEl(dataIndex, null);
  550. }
  551. function getShapeStr(data, symbolMeta) {
  552. return [data.getItemVisual(symbolMeta.dataIndex, 'symbol') || 'none', !!symbolMeta.symbolRepeat, !!symbolMeta.symbolClip].join(':');
  553. }
  554. function eachPath(bar, cb, context) {
  555. // Do not use Group#eachChild, because it do not support remove.
  556. zrUtil.each(bar.__pictorialBundle.children(), function (el) {
  557. el !== bar.__pictorialBarRect && cb.call(context, el);
  558. });
  559. }
  560. function updateAttr(el, immediateAttrs, animationAttrs, symbolMeta, isUpdate, cb) {
  561. immediateAttrs && el.attr(immediateAttrs);
  562. // when symbolCip used, only clip path has init animation, otherwise it would be weird effect.
  563. if (symbolMeta.symbolClip && !isUpdate) {
  564. animationAttrs && el.attr(animationAttrs);
  565. } else {
  566. animationAttrs && graphic[isUpdate ? 'updateProps' : 'initProps'](el, animationAttrs, symbolMeta.animationModel, symbolMeta.dataIndex, cb);
  567. }
  568. }
  569. function updateCommon(bar, opt, symbolMeta) {
  570. var dataIndex = symbolMeta.dataIndex;
  571. var itemModel = symbolMeta.itemModel;
  572. // Color must be excluded.
  573. // Because symbol provide setColor individually to set fill and stroke
  574. var emphasisModel = itemModel.getModel('emphasis');
  575. var emphasisStyle = emphasisModel.getModel('itemStyle').getItemStyle();
  576. var blurStyle = itemModel.getModel(['blur', 'itemStyle']).getItemStyle();
  577. var selectStyle = itemModel.getModel(['select', 'itemStyle']).getItemStyle();
  578. var cursorStyle = itemModel.getShallow('cursor');
  579. var focus = emphasisModel.get('focus');
  580. var blurScope = emphasisModel.get('blurScope');
  581. var hoverScale = emphasisModel.get('scale');
  582. eachPath(bar, function (path) {
  583. if (path instanceof ZRImage) {
  584. var pathStyle = path.style;
  585. path.useStyle(zrUtil.extend({
  586. // TODO other properties like dx, dy ?
  587. image: pathStyle.image,
  588. x: pathStyle.x,
  589. y: pathStyle.y,
  590. width: pathStyle.width,
  591. height: pathStyle.height
  592. }, symbolMeta.style));
  593. } else {
  594. path.useStyle(symbolMeta.style);
  595. }
  596. var emphasisState = path.ensureState('emphasis');
  597. emphasisState.style = emphasisStyle;
  598. if (hoverScale) {
  599. // NOTE: Must after scale is set after updateAttr
  600. emphasisState.scaleX = path.scaleX * 1.1;
  601. emphasisState.scaleY = path.scaleY * 1.1;
  602. }
  603. path.ensureState('blur').style = blurStyle;
  604. path.ensureState('select').style = selectStyle;
  605. cursorStyle && (path.cursor = cursorStyle);
  606. path.z2 = symbolMeta.z2;
  607. });
  608. var barPositionOutside = opt.valueDim.posDesc[+(symbolMeta.boundingLength > 0)];
  609. var barRect = bar.__pictorialBarRect;
  610. barRect.ignoreClip = true;
  611. setLabelStyle(barRect, getLabelStatesModels(itemModel), {
  612. labelFetcher: opt.seriesModel,
  613. labelDataIndex: dataIndex,
  614. defaultText: getDefaultLabel(opt.seriesModel.getData(), dataIndex),
  615. inheritColor: symbolMeta.style.fill,
  616. defaultOpacity: symbolMeta.style.opacity,
  617. defaultOutsidePosition: barPositionOutside
  618. });
  619. toggleHoverEmphasis(bar, focus, blurScope, emphasisModel.get('disabled'));
  620. }
  621. function toIntTimes(times) {
  622. var roundedTimes = Math.round(times);
  623. // Escapse accurate error
  624. return Math.abs(times - roundedTimes) < 1e-4 ? roundedTimes : Math.ceil(times);
  625. }
  626. export default PictorialBarView;