PictorialBarView.js 25 KB

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