PictorialBarView.js 25 KB

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