PictorialBarView.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. var echarts = require("../../echarts");
  2. var zrUtil = require("zrender/lib/core/util");
  3. var graphic = require("../../util/graphic");
  4. var _symbol = require("../../util/symbol");
  5. var createSymbol = _symbol.createSymbol;
  6. var _number = require("../../util/number");
  7. var parsePercent = _number.parsePercent;
  8. var isNumeric = _number.isNumeric;
  9. var _helper = require("./helper");
  10. var setLabel = _helper.setLabel;
  11. var BAR_BORDER_WIDTH_QUERY = ['itemStyle', 'normal', 'borderWidth']; // index: +isHorizontal
  12. var LAYOUT_ATTRS = [{
  13. xy: 'x',
  14. wh: 'width',
  15. index: 0,
  16. posDesc: ['left', 'right']
  17. }, {
  18. xy: 'y',
  19. wh: 'height',
  20. index: 1,
  21. posDesc: ['top', 'bottom']
  22. }];
  23. var pathForLineWidth = new graphic.Circle();
  24. var BarView = echarts.extendChartView({
  25. type: 'pictorialBar',
  26. render: function (seriesModel, ecModel, api) {
  27. var group = this.group;
  28. var data = seriesModel.getData();
  29. var oldData = this._data;
  30. var cartesian = seriesModel.coordinateSystem;
  31. var baseAxis = cartesian.getBaseAxis();
  32. var isHorizontal = !!baseAxis.isHorizontal();
  33. var coordSysRect = cartesian.grid.getRect();
  34. var opt = {
  35. ecSize: {
  36. width: api.getWidth(),
  37. height: api.getHeight()
  38. },
  39. seriesModel: seriesModel,
  40. coordSys: cartesian,
  41. coordSysExtent: [[coordSysRect.x, coordSysRect.x + coordSysRect.width], [coordSysRect.y, coordSysRect.y + coordSysRect.height]],
  42. isHorizontal: isHorizontal,
  43. valueDim: LAYOUT_ATTRS[+isHorizontal],
  44. categoryDim: LAYOUT_ATTRS[1 - isHorizontal]
  45. };
  46. data.diff(oldData).add(function (dataIndex) {
  47. if (!data.hasValue(dataIndex)) {
  48. return;
  49. }
  50. var itemModel = getItemModel(data, dataIndex);
  51. var symbolMeta = getSymbolMeta(data, dataIndex, itemModel, opt);
  52. var bar = createBar(data, opt, symbolMeta);
  53. data.setItemGraphicEl(dataIndex, bar);
  54. group.add(bar);
  55. updateCommon(bar, opt, symbolMeta);
  56. }).update(function (newIndex, oldIndex) {
  57. var bar = oldData.getItemGraphicEl(oldIndex);
  58. if (!data.hasValue(newIndex)) {
  59. group.remove(bar);
  60. return;
  61. }
  62. var itemModel = getItemModel(data, newIndex);
  63. var symbolMeta = getSymbolMeta(data, newIndex, itemModel, opt);
  64. var pictorialShapeStr = getShapeStr(data, symbolMeta);
  65. if (bar && pictorialShapeStr !== bar.__pictorialShapeStr) {
  66. group.remove(bar);
  67. data.setItemGraphicEl(newIndex, null);
  68. bar = null;
  69. }
  70. if (bar) {
  71. updateBar(bar, opt, symbolMeta);
  72. } else {
  73. bar = createBar(data, opt, symbolMeta, true);
  74. }
  75. data.setItemGraphicEl(newIndex, bar);
  76. bar.__pictorialSymbolMeta = symbolMeta; // Add back
  77. group.add(bar);
  78. updateCommon(bar, opt, symbolMeta);
  79. }).remove(function (dataIndex) {
  80. var bar = oldData.getItemGraphicEl(dataIndex);
  81. bar && removeBar(oldData, dataIndex, bar.__pictorialSymbolMeta.animationModel, bar);
  82. }).execute();
  83. this._data = data;
  84. return this.group;
  85. },
  86. dispose: zrUtil.noop,
  87. remove: function (ecModel, api) {
  88. var group = this.group;
  89. var data = this._data;
  90. if (ecModel.get('animation')) {
  91. if (data) {
  92. data.eachItemGraphicEl(function (bar) {
  93. removeBar(data, bar.dataIndex, ecModel, bar);
  94. });
  95. }
  96. } else {
  97. group.removeAll();
  98. }
  99. }
  100. }); // Set or calculate default value about symbol, and calculate layout info.
  101. function getSymbolMeta(data, dataIndex, itemModel, opt) {
  102. var layout = data.getItemLayout(dataIndex);
  103. var symbolRepeat = itemModel.get('symbolRepeat');
  104. var symbolClip = itemModel.get('symbolClip');
  105. var symbolPosition = itemModel.get('symbolPosition') || 'start';
  106. var symbolRotate = itemModel.get('symbolRotate');
  107. var rotation = (symbolRotate || 0) * Math.PI / 180 || 0;
  108. var symbolPatternSize = itemModel.get('symbolPatternSize') || 2;
  109. var isAnimationEnabled = itemModel.isAnimationEnabled();
  110. var symbolMeta = {
  111. dataIndex: dataIndex,
  112. layout: layout,
  113. itemModel: itemModel,
  114. symbolType: data.getItemVisual(dataIndex, 'symbol') || 'circle',
  115. color: data.getItemVisual(dataIndex, 'color'),
  116. symbolClip: symbolClip,
  117. symbolRepeat: symbolRepeat,
  118. symbolRepeatDirection: itemModel.get('symbolRepeatDirection'),
  119. symbolPatternSize: symbolPatternSize,
  120. rotation: rotation,
  121. animationModel: isAnimationEnabled ? itemModel : null,
  122. hoverAnimation: isAnimationEnabled && itemModel.get('hoverAnimation'),
  123. z2: itemModel.getShallow('z', true) || 0
  124. };
  125. prepareBarLength(itemModel, symbolRepeat, layout, opt, symbolMeta);
  126. prepareSymbolSize(data, dataIndex, layout, symbolRepeat, symbolClip, symbolMeta.boundingLength, symbolMeta.pxSign, symbolPatternSize, opt, symbolMeta);
  127. prepareLineWidth(itemModel, symbolMeta.symbolScale, rotation, opt, symbolMeta);
  128. var symbolSize = symbolMeta.symbolSize;
  129. var symbolOffset = itemModel.get('symbolOffset');
  130. if (zrUtil.isArray(symbolOffset)) {
  131. symbolOffset = [parsePercent(symbolOffset[0], symbolSize[0]), parsePercent(symbolOffset[1], symbolSize[1])];
  132. }
  133. prepareLayoutInfo(itemModel, symbolSize, layout, symbolRepeat, symbolClip, symbolOffset, symbolPosition, symbolMeta.valueLineWidth, symbolMeta.boundingLength, symbolMeta.repeatCutLength, opt, symbolMeta);
  134. return symbolMeta;
  135. } // bar length can be negative.
  136. function prepareBarLength(itemModel, symbolRepeat, layout, opt, output) {
  137. var valueDim = opt.valueDim;
  138. var symbolBoundingData = itemModel.get('symbolBoundingData');
  139. var valueAxis = opt.coordSys.getOtherAxis(opt.coordSys.getBaseAxis());
  140. var zeroPx = valueAxis.toGlobalCoord(valueAxis.dataToCoord(0));
  141. var pxSignIdx = 1 - +(layout[valueDim.wh] <= 0);
  142. var boundingLength;
  143. if (zrUtil.isArray(symbolBoundingData)) {
  144. var symbolBoundingExtent = [convertToCoordOnAxis(valueAxis, symbolBoundingData[0]) - zeroPx, convertToCoordOnAxis(valueAxis, symbolBoundingData[1]) - zeroPx];
  145. symbolBoundingExtent[1] < symbolBoundingExtent[0] && symbolBoundingExtent.reverse();
  146. boundingLength = symbolBoundingExtent[pxSignIdx];
  147. } else if (symbolBoundingData != null) {
  148. boundingLength = convertToCoordOnAxis(valueAxis, symbolBoundingData) - zeroPx;
  149. } else if (symbolRepeat) {
  150. boundingLength = opt.coordSysExtent[valueDim.index][pxSignIdx] - zeroPx;
  151. } else {
  152. boundingLength = layout[valueDim.wh];
  153. }
  154. output.boundingLength = boundingLength;
  155. if (symbolRepeat) {
  156. output.repeatCutLength = layout[valueDim.wh];
  157. }
  158. output.pxSign = boundingLength > 0 ? 1 : boundingLength < 0 ? -1 : 0;
  159. }
  160. function convertToCoordOnAxis(axis, value) {
  161. return axis.toGlobalCoord(axis.dataToCoord(axis.scale.parse(value)));
  162. } // Support ['100%', '100%']
  163. function prepareSymbolSize(data, dataIndex, layout, symbolRepeat, symbolClip, boundingLength, pxSign, symbolPatternSize, opt, output) {
  164. var valueDim = opt.valueDim;
  165. var categoryDim = opt.categoryDim;
  166. var categorySize = Math.abs(layout[categoryDim.wh]);
  167. var symbolSize = data.getItemVisual(dataIndex, 'symbolSize');
  168. if (zrUtil.isArray(symbolSize)) {
  169. symbolSize = symbolSize.slice();
  170. } else {
  171. if (symbolSize == null) {
  172. symbolSize = '100%';
  173. }
  174. symbolSize = [symbolSize, symbolSize];
  175. } // Note: percentage symbolSize (like '100%') do not consider lineWidth, because it is
  176. // to complicated to calculate real percent value if considering scaled lineWidth.
  177. // So the actual size will bigger than layout size if lineWidth is bigger than zero,
  178. // which can be tolerated in pictorial chart.
  179. symbolSize[categoryDim.index] = parsePercent(symbolSize[categoryDim.index], categorySize);
  180. symbolSize[valueDim.index] = parsePercent(symbolSize[valueDim.index], symbolRepeat ? categorySize : Math.abs(boundingLength));
  181. output.symbolSize = symbolSize; // If x or y is less than zero, show reversed shape.
  182. var symbolScale = output.symbolScale = [symbolSize[0] / symbolPatternSize, symbolSize[1] / symbolPatternSize]; // Follow convention, 'right' and 'top' is the normal scale.
  183. symbolScale[valueDim.index] *= (opt.isHorizontal ? -1 : 1) * pxSign;
  184. }
  185. function prepareLineWidth(itemModel, symbolScale, rotation, opt, output) {
  186. // In symbols are drawn with scale, so do not need to care about the case that width
  187. // or height are too small. But symbol use strokeNoScale, where acture lineWidth should
  188. // be calculated.
  189. var valueLineWidth = itemModel.get(BAR_BORDER_WIDTH_QUERY) || 0;
  190. if (valueLineWidth) {
  191. pathForLineWidth.attr({
  192. scale: symbolScale.slice(),
  193. rotation: rotation
  194. });
  195. pathForLineWidth.updateTransform();
  196. valueLineWidth /= pathForLineWidth.getLineScale();
  197. valueLineWidth *= symbolScale[opt.valueDim.index];
  198. }
  199. output.valueLineWidth = valueLineWidth;
  200. }
  201. function prepareLayoutInfo(itemModel, symbolSize, layout, symbolRepeat, symbolClip, symbolOffset, symbolPosition, valueLineWidth, boundingLength, repeatCutLength, opt, output) {
  202. var categoryDim = opt.categoryDim;
  203. var valueDim = opt.valueDim;
  204. var pxSign = output.pxSign;
  205. var unitLength = Math.max(symbolSize[valueDim.index] + valueLineWidth, 0);
  206. var pathLen = unitLength; // Note: rotation will not effect the layout of symbols, because user may
  207. // want symbols to rotate on its center, which should not be translated
  208. // when rotating.
  209. if (symbolRepeat) {
  210. var absBoundingLength = Math.abs(boundingLength);
  211. var symbolMargin = zrUtil.retrieve(itemModel.get('symbolMargin'), '15%') + '';
  212. var hasEndGap = false;
  213. if (symbolMargin.lastIndexOf('!') === symbolMargin.length - 1) {
  214. hasEndGap = true;
  215. symbolMargin = symbolMargin.slice(0, symbolMargin.length - 1);
  216. }
  217. symbolMargin = parsePercent(symbolMargin, symbolSize[valueDim.index]);
  218. var uLenWithMargin = Math.max(unitLength + symbolMargin * 2, 0); // When symbol margin is less than 0, margin at both ends will be subtracted
  219. // to ensure that all of the symbols will not be overflow the given area.
  220. var endFix = hasEndGap ? 0 : symbolMargin * 2; // Both final repeatTimes and final symbolMargin area calculated based on
  221. // boundingLength.
  222. var repeatSpecified = isNumeric(symbolRepeat);
  223. var repeatTimes = repeatSpecified ? symbolRepeat : toIntTimes((absBoundingLength + endFix) / uLenWithMargin); // Adjust calculate margin, to ensure each symbol is displayed
  224. // entirely in the given layout area.
  225. var mDiff = absBoundingLength - repeatTimes * unitLength;
  226. symbolMargin = mDiff / 2 / (hasEndGap ? repeatTimes : repeatTimes - 1);
  227. uLenWithMargin = unitLength + symbolMargin * 2;
  228. endFix = hasEndGap ? 0 : symbolMargin * 2; // Update repeatTimes when not all symbol will be shown.
  229. if (!repeatSpecified && symbolRepeat !== 'fixed') {
  230. repeatTimes = repeatCutLength ? toIntTimes((Math.abs(repeatCutLength) + endFix) / uLenWithMargin) : 0;
  231. }
  232. pathLen = repeatTimes * uLenWithMargin - endFix;
  233. output.repeatTimes = repeatTimes;
  234. output.symbolMargin = symbolMargin;
  235. }
  236. var sizeFix = pxSign * (pathLen / 2);
  237. var pathPosition = output.pathPosition = [];
  238. pathPosition[categoryDim.index] = layout[categoryDim.wh] / 2;
  239. pathPosition[valueDim.index] = symbolPosition === 'start' ? sizeFix : symbolPosition === 'end' ? boundingLength - sizeFix : boundingLength / 2; // 'center'
  240. if (symbolOffset) {
  241. pathPosition[0] += symbolOffset[0];
  242. pathPosition[1] += symbolOffset[1];
  243. }
  244. var bundlePosition = output.bundlePosition = [];
  245. bundlePosition[categoryDim.index] = layout[categoryDim.xy];
  246. bundlePosition[valueDim.index] = layout[valueDim.xy];
  247. var barRectShape = output.barRectShape = zrUtil.extend({}, layout);
  248. barRectShape[valueDim.wh] = pxSign * Math.max(Math.abs(layout[valueDim.wh]), Math.abs(pathPosition[valueDim.index] + sizeFix));
  249. barRectShape[categoryDim.wh] = layout[categoryDim.wh];
  250. var clipShape = output.clipShape = {}; // Consider that symbol may be overflow layout rect.
  251. clipShape[categoryDim.xy] = -layout[categoryDim.xy];
  252. clipShape[categoryDim.wh] = opt.ecSize[categoryDim.wh];
  253. clipShape[valueDim.xy] = 0;
  254. clipShape[valueDim.wh] = layout[valueDim.wh];
  255. }
  256. function createPath(symbolMeta) {
  257. var symbolPatternSize = symbolMeta.symbolPatternSize;
  258. var path = createSymbol( // Consider texture img, make a big size.
  259. symbolMeta.symbolType, -symbolPatternSize / 2, -symbolPatternSize / 2, symbolPatternSize, symbolPatternSize, symbolMeta.color);
  260. path.attr({
  261. culling: true
  262. });
  263. path.type !== 'image' && path.setStyle({
  264. strokeNoScale: true
  265. });
  266. return path;
  267. }
  268. function createOrUpdateRepeatSymbols(bar, opt, symbolMeta, isUpdate) {
  269. var bundle = bar.__pictorialBundle;
  270. var symbolSize = symbolMeta.symbolSize;
  271. var valueLineWidth = symbolMeta.valueLineWidth;
  272. var pathPosition = symbolMeta.pathPosition;
  273. var valueDim = opt.valueDim;
  274. var repeatTimes = symbolMeta.repeatTimes || 0;
  275. var index = 0;
  276. var unit = symbolSize[opt.valueDim.index] + valueLineWidth + symbolMeta.symbolMargin * 2;
  277. eachPath(bar, function (path) {
  278. path.__pictorialAnimationIndex = index;
  279. path.__pictorialRepeatTimes = repeatTimes;
  280. if (index < repeatTimes) {
  281. updateAttr(path, null, makeTarget(index), symbolMeta, isUpdate);
  282. } else {
  283. updateAttr(path, null, {
  284. scale: [0, 0]
  285. }, symbolMeta, isUpdate, function () {
  286. bundle.remove(path);
  287. });
  288. }
  289. updateHoverAnimation(path, symbolMeta);
  290. index++;
  291. });
  292. for (; index < repeatTimes; index++) {
  293. var path = createPath(symbolMeta);
  294. path.__pictorialAnimationIndex = index;
  295. path.__pictorialRepeatTimes = repeatTimes;
  296. bundle.add(path);
  297. var target = makeTarget(index);
  298. updateAttr(path, {
  299. position: target.position,
  300. scale: [0, 0]
  301. }, {
  302. scale: target.scale,
  303. rotation: target.rotation
  304. }, symbolMeta, isUpdate); // FIXME
  305. // If all emphasis/normal through action.
  306. path.on('mouseover', onMouseOver).on('mouseout', onMouseOut);
  307. updateHoverAnimation(path, symbolMeta);
  308. }
  309. function makeTarget(index) {
  310. var position = pathPosition.slice(); // (start && pxSign > 0) || (end && pxSign < 0): i = repeatTimes - index
  311. // Otherwise: i = index;
  312. var pxSign = symbolMeta.pxSign;
  313. var i = index;
  314. if (symbolMeta.symbolRepeatDirection === 'start' ? pxSign > 0 : pxSign < 0) {
  315. i = repeatTimes - 1 - index;
  316. }
  317. position[valueDim.index] = unit * (i - repeatTimes / 2 + 0.5) + pathPosition[valueDim.index];
  318. return {
  319. position: position,
  320. scale: symbolMeta.symbolScale.slice(),
  321. rotation: symbolMeta.rotation
  322. };
  323. }
  324. function onMouseOver() {
  325. eachPath(bar, function (path) {
  326. path.trigger('emphasis');
  327. });
  328. }
  329. function onMouseOut() {
  330. eachPath(bar, function (path) {
  331. path.trigger('normal');
  332. });
  333. }
  334. }
  335. function createOrUpdateSingleSymbol(bar, opt, symbolMeta, isUpdate) {
  336. var bundle = bar.__pictorialBundle;
  337. var mainPath = bar.__pictorialMainPath;
  338. if (!mainPath) {
  339. mainPath = bar.__pictorialMainPath = createPath(symbolMeta);
  340. bundle.add(mainPath);
  341. updateAttr(mainPath, {
  342. position: symbolMeta.pathPosition.slice(),
  343. scale: [0, 0],
  344. rotation: symbolMeta.rotation
  345. }, {
  346. scale: symbolMeta.symbolScale.slice()
  347. }, symbolMeta, isUpdate);
  348. mainPath.on('mouseover', onMouseOver).on('mouseout', onMouseOut);
  349. } else {
  350. updateAttr(mainPath, null, {
  351. position: symbolMeta.pathPosition.slice(),
  352. scale: symbolMeta.symbolScale.slice(),
  353. rotation: symbolMeta.rotation
  354. }, symbolMeta, isUpdate);
  355. }
  356. updateHoverAnimation(mainPath, symbolMeta);
  357. function onMouseOver() {
  358. this.trigger('emphasis');
  359. }
  360. function onMouseOut() {
  361. this.trigger('normal');
  362. }
  363. } // bar rect is used for label.
  364. function createOrUpdateBarRect(bar, symbolMeta, isUpdate) {
  365. var rectShape = zrUtil.extend({}, symbolMeta.barRectShape);
  366. var barRect = bar.__pictorialBarRect;
  367. if (!barRect) {
  368. barRect = bar.__pictorialBarRect = new graphic.Rect({
  369. z2: 2,
  370. shape: rectShape,
  371. silent: true,
  372. style: {
  373. stroke: 'transparent',
  374. fill: 'transparent',
  375. lineWidth: 0
  376. }
  377. });
  378. bar.add(barRect);
  379. } else {
  380. updateAttr(barRect, null, {
  381. shape: rectShape
  382. }, symbolMeta, isUpdate);
  383. }
  384. }
  385. function createOrUpdateClip(bar, opt, symbolMeta, isUpdate) {
  386. // If not clip, symbol will be remove and rebuilt.
  387. if (symbolMeta.symbolClip) {
  388. var clipPath = bar.__pictorialClipPath;
  389. var clipShape = zrUtil.extend({}, symbolMeta.clipShape);
  390. var valueDim = opt.valueDim;
  391. var animationModel = symbolMeta.animationModel;
  392. var dataIndex = symbolMeta.dataIndex;
  393. if (clipPath) {
  394. graphic.updateProps(clipPath, {
  395. shape: clipShape
  396. }, animationModel, dataIndex);
  397. } else {
  398. clipShape[valueDim.wh] = 0;
  399. clipPath = new graphic.Rect({
  400. shape: clipShape
  401. });
  402. bar.__pictorialBundle.setClipPath(clipPath);
  403. bar.__pictorialClipPath = clipPath;
  404. var target = {};
  405. target[valueDim.wh] = symbolMeta.clipShape[valueDim.wh];
  406. graphic[isUpdate ? 'updateProps' : 'initProps'](clipPath, {
  407. shape: target
  408. }, animationModel, dataIndex);
  409. }
  410. }
  411. }
  412. function getItemModel(data, dataIndex) {
  413. var itemModel = data.getItemModel(dataIndex);
  414. itemModel.getAnimationDelayParams = getAnimationDelayParams;
  415. itemModel.isAnimationEnabled = isAnimationEnabled;
  416. return itemModel;
  417. }
  418. function getAnimationDelayParams(path) {
  419. // The order is the same as the z-order, see `symbolRepeatDiretion`.
  420. return {
  421. index: path.__pictorialAnimationIndex,
  422. count: path.__pictorialRepeatTimes
  423. };
  424. }
  425. function isAnimationEnabled() {
  426. // `animation` prop can be set on itemModel in pictorial bar chart.
  427. return this.parentModel.isAnimationEnabled() && !!this.getShallow('animation');
  428. }
  429. function updateHoverAnimation(path, symbolMeta) {
  430. path.off('emphasis').off('normal');
  431. var scale = symbolMeta.symbolScale.slice();
  432. symbolMeta.hoverAnimation && path.on('emphasis', function () {
  433. this.animateTo({
  434. scale: [scale[0] * 1.1, scale[1] * 1.1]
  435. }, 400, 'elasticOut');
  436. }).on('normal', function () {
  437. this.animateTo({
  438. scale: scale.slice()
  439. }, 400, 'elasticOut');
  440. });
  441. }
  442. function createBar(data, opt, symbolMeta, isUpdate) {
  443. // bar is the main element for each data.
  444. var bar = new graphic.Group(); // bundle is used for location and clip.
  445. var bundle = new graphic.Group();
  446. bar.add(bundle);
  447. bar.__pictorialBundle = bundle;
  448. bundle.attr('position', symbolMeta.bundlePosition.slice());
  449. if (symbolMeta.symbolRepeat) {
  450. createOrUpdateRepeatSymbols(bar, opt, symbolMeta);
  451. } else {
  452. createOrUpdateSingleSymbol(bar, opt, symbolMeta);
  453. }
  454. createOrUpdateBarRect(bar, symbolMeta, isUpdate);
  455. createOrUpdateClip(bar, opt, symbolMeta, isUpdate);
  456. bar.__pictorialShapeStr = getShapeStr(data, symbolMeta);
  457. bar.__pictorialSymbolMeta = symbolMeta;
  458. return bar;
  459. }
  460. function updateBar(bar, opt, symbolMeta) {
  461. var animationModel = symbolMeta.animationModel;
  462. var dataIndex = symbolMeta.dataIndex;
  463. var bundle = bar.__pictorialBundle;
  464. graphic.updateProps(bundle, {
  465. position: symbolMeta.bundlePosition.slice()
  466. }, animationModel, dataIndex);
  467. if (symbolMeta.symbolRepeat) {
  468. createOrUpdateRepeatSymbols(bar, opt, symbolMeta, true);
  469. } else {
  470. createOrUpdateSingleSymbol(bar, opt, symbolMeta, true);
  471. }
  472. createOrUpdateBarRect(bar, symbolMeta, true);
  473. createOrUpdateClip(bar, opt, symbolMeta, true);
  474. }
  475. function removeBar(data, dataIndex, animationModel, bar) {
  476. // Not show text when animating
  477. var labelRect = bar.__pictorialBarRect;
  478. labelRect && (labelRect.style.text = null);
  479. var pathes = [];
  480. eachPath(bar, function (path) {
  481. pathes.push(path);
  482. });
  483. bar.__pictorialMainPath && pathes.push(bar.__pictorialMainPath); // I do not find proper remove animation for clip yet.
  484. bar.__pictorialClipPath && (animationModel = null);
  485. zrUtil.each(pathes, function (path) {
  486. graphic.updateProps(path, {
  487. scale: [0, 0]
  488. }, animationModel, dataIndex, function () {
  489. bar.parent && bar.parent.remove(bar);
  490. });
  491. });
  492. data.setItemGraphicEl(dataIndex, null);
  493. }
  494. function getShapeStr(data, symbolMeta) {
  495. return [data.getItemVisual(symbolMeta.dataIndex, 'symbol') || 'none', !!symbolMeta.symbolRepeat, !!symbolMeta.symbolClip].join(':');
  496. }
  497. function eachPath(bar, cb, context) {
  498. // Do not use Group#eachChild, because it do not support remove.
  499. zrUtil.each(bar.__pictorialBundle.children(), function (el) {
  500. el !== bar.__pictorialBarRect && cb.call(context, el);
  501. });
  502. }
  503. function updateAttr(el, immediateAttrs, animationAttrs, symbolMeta, isUpdate, cb) {
  504. immediateAttrs && el.attr(immediateAttrs); // when symbolCip used, only clip path has init animation, otherwise it would be weird effect.
  505. if (symbolMeta.symbolClip && !isUpdate) {
  506. animationAttrs && el.attr(animationAttrs);
  507. } else {
  508. animationAttrs && graphic[isUpdate ? 'updateProps' : 'initProps'](el, animationAttrs, symbolMeta.animationModel, symbolMeta.dataIndex, cb);
  509. }
  510. }
  511. function updateCommon(bar, opt, symbolMeta) {
  512. var color = symbolMeta.color;
  513. var dataIndex = symbolMeta.dataIndex;
  514. var itemModel = symbolMeta.itemModel; // Color must be excluded.
  515. // Because symbol provide setColor individually to set fill and stroke
  516. var normalStyle = itemModel.getModel('itemStyle.normal').getItemStyle(['color']);
  517. var hoverStyle = itemModel.getModel('itemStyle.emphasis').getItemStyle();
  518. var cursorStyle = itemModel.getShallow('cursor');
  519. eachPath(bar, function (path) {
  520. // PENDING setColor should be before setStyle!!!
  521. path.setColor(color);
  522. path.setStyle(zrUtil.defaults({
  523. fill: color,
  524. opacity: symbolMeta.opacity
  525. }, normalStyle));
  526. graphic.setHoverStyle(path, hoverStyle);
  527. cursorStyle && (path.cursor = cursorStyle);
  528. path.z2 = symbolMeta.z2;
  529. });
  530. var barRectHoverStyle = {};
  531. var barPositionOutside = opt.valueDim.posDesc[+(symbolMeta.boundingLength > 0)];
  532. var barRect = bar.__pictorialBarRect;
  533. setLabel(barRect.style, barRectHoverStyle, itemModel, color, opt.seriesModel, dataIndex, barPositionOutside);
  534. graphic.setHoverStyle(barRect, barRectHoverStyle);
  535. }
  536. function toIntTimes(times) {
  537. var roundedTimes = Math.round(times); // Escapse accurate error
  538. return Math.abs(times - roundedTimes) < 1e-4 ? roundedTimes : Math.ceil(times);
  539. }
  540. var _default = BarView;
  541. module.exports = _default;