BarView.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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 Path from 'zrender/lib/graphic/Path.js';
  42. import Group from 'zrender/lib/graphic/Group.js';
  43. import { extend, each, map } from 'zrender/lib/core/util.js';
  44. import { Rect, Sector, updateProps, initProps, removeElementWithFadeOut, traverseElements } from '../../util/graphic.js';
  45. import { getECData } from '../../util/innerStore.js';
  46. import { setStatesStylesFromModel, toggleHoverEmphasis } from '../../util/states.js';
  47. import { setLabelStyle, getLabelStatesModels, setLabelValueAnimation, labelInner } from '../../label/labelStyle.js';
  48. import { throttle } from '../../util/throttle.js';
  49. import { createClipPath } from '../helper/createClipPathFromCoordSys.js';
  50. import Sausage from '../../util/shape/sausage.js';
  51. import ChartView from '../../view/Chart.js';
  52. import { isCoordinateSystemType } from '../../coord/CoordinateSystem.js';
  53. import { getDefaultLabel, getDefaultInterpolatedLabel } from '../helper/labelHelper.js';
  54. import { warn } from '../../util/log.js';
  55. import { createSectorCalculateTextPosition, setSectorTextRotation } from '../../label/sectorLabel.js';
  56. import { saveOldStyle } from '../../animation/basicTransition.js';
  57. import { getSectorCornerRadius } from '../helper/sectorHelper.js';
  58. var mathMax = Math.max;
  59. var mathMin = Math.min;
  60. function getClipArea(coord, data) {
  61. var coordSysClipArea = coord.getArea && coord.getArea();
  62. if (isCoordinateSystemType(coord, 'cartesian2d')) {
  63. var baseAxis = coord.getBaseAxis();
  64. // When boundaryGap is false or using time axis. bar may exceed the grid.
  65. // We should not clip this part.
  66. // See test/bar2.html
  67. if (baseAxis.type !== 'category' || !baseAxis.onBand) {
  68. var expandWidth = data.getLayout('bandWidth');
  69. if (baseAxis.isHorizontal()) {
  70. coordSysClipArea.x -= expandWidth;
  71. coordSysClipArea.width += expandWidth * 2;
  72. } else {
  73. coordSysClipArea.y -= expandWidth;
  74. coordSysClipArea.height += expandWidth * 2;
  75. }
  76. }
  77. }
  78. return coordSysClipArea;
  79. }
  80. var BarView = /** @class */function (_super) {
  81. __extends(BarView, _super);
  82. function BarView() {
  83. var _this = _super.call(this) || this;
  84. _this.type = BarView.type;
  85. _this._isFirstFrame = true;
  86. return _this;
  87. }
  88. BarView.prototype.render = function (seriesModel, ecModel, api, payload) {
  89. this._model = seriesModel;
  90. this._removeOnRenderedListener(api);
  91. this._updateDrawMode(seriesModel);
  92. var coordinateSystemType = seriesModel.get('coordinateSystem');
  93. if (coordinateSystemType === 'cartesian2d' || coordinateSystemType === 'polar') {
  94. // Clear previously rendered progressive elements.
  95. this._progressiveEls = null;
  96. this._isLargeDraw ? this._renderLarge(seriesModel, ecModel, api) : this._renderNormal(seriesModel, ecModel, api, payload);
  97. } else if (process.env.NODE_ENV !== 'production') {
  98. warn('Only cartesian2d and polar supported for bar.');
  99. }
  100. };
  101. BarView.prototype.incrementalPrepareRender = function (seriesModel) {
  102. this._clear();
  103. this._updateDrawMode(seriesModel);
  104. // incremental also need to clip, otherwise might be overlow.
  105. // But must not set clip in each frame, otherwise all of the children will be marked redraw.
  106. this._updateLargeClip(seriesModel);
  107. };
  108. BarView.prototype.incrementalRender = function (params, seriesModel) {
  109. // Reset
  110. this._progressiveEls = [];
  111. // Do not support progressive in normal mode.
  112. this._incrementalRenderLarge(params, seriesModel);
  113. };
  114. BarView.prototype.eachRendered = function (cb) {
  115. traverseElements(this._progressiveEls || this.group, cb);
  116. };
  117. BarView.prototype._updateDrawMode = function (seriesModel) {
  118. var isLargeDraw = seriesModel.pipelineContext.large;
  119. if (this._isLargeDraw == null || isLargeDraw !== this._isLargeDraw) {
  120. this._isLargeDraw = isLargeDraw;
  121. this._clear();
  122. }
  123. };
  124. BarView.prototype._renderNormal = function (seriesModel, ecModel, api, payload) {
  125. var group = this.group;
  126. var data = seriesModel.getData();
  127. var oldData = this._data;
  128. var coord = seriesModel.coordinateSystem;
  129. var baseAxis = coord.getBaseAxis();
  130. var isHorizontalOrRadial;
  131. if (coord.type === 'cartesian2d') {
  132. isHorizontalOrRadial = baseAxis.isHorizontal();
  133. } else if (coord.type === 'polar') {
  134. isHorizontalOrRadial = baseAxis.dim === 'angle';
  135. }
  136. var animationModel = seriesModel.isAnimationEnabled() ? seriesModel : null;
  137. var realtimeSortCfg = shouldRealtimeSort(seriesModel, coord);
  138. if (realtimeSortCfg) {
  139. this._enableRealtimeSort(realtimeSortCfg, data, api);
  140. }
  141. var needsClip = seriesModel.get('clip', true) || realtimeSortCfg;
  142. var coordSysClipArea = getClipArea(coord, data);
  143. // If there is clipPath created in large mode. Remove it.
  144. group.removeClipPath();
  145. // We don't use clipPath in normal mode because we needs a perfect animation
  146. // And don't want the label are clipped.
  147. var roundCap = seriesModel.get('roundCap', true);
  148. var drawBackground = seriesModel.get('showBackground', true);
  149. var backgroundModel = seriesModel.getModel('backgroundStyle');
  150. var barBorderRadius = backgroundModel.get('borderRadius') || 0;
  151. var bgEls = [];
  152. var oldBgEls = this._backgroundEls;
  153. var isInitSort = payload && payload.isInitSort;
  154. var isChangeOrder = payload && payload.type === 'changeAxisOrder';
  155. function createBackground(dataIndex) {
  156. var bgLayout = getLayout[coord.type](data, dataIndex);
  157. if (!bgLayout) {
  158. return null;
  159. }
  160. var bgEl = createBackgroundEl(coord, isHorizontalOrRadial, bgLayout);
  161. bgEl.useStyle(backgroundModel.getItemStyle());
  162. // Only cartesian2d support borderRadius.
  163. if (coord.type === 'cartesian2d') {
  164. bgEl.setShape('r', barBorderRadius);
  165. } else {
  166. bgEl.setShape('cornerRadius', barBorderRadius);
  167. }
  168. bgEls[dataIndex] = bgEl;
  169. return bgEl;
  170. }
  171. ;
  172. data.diff(oldData).add(function (dataIndex) {
  173. var itemModel = data.getItemModel(dataIndex);
  174. var layout = getLayout[coord.type](data, dataIndex, itemModel);
  175. if (!layout) {
  176. return;
  177. }
  178. if (drawBackground) {
  179. createBackground(dataIndex);
  180. }
  181. // If dataZoom in filteMode: 'empty', the baseValue can be set as NaN in "axisProxy".
  182. if (!data.hasValue(dataIndex) || !isValidLayout[coord.type](layout)) {
  183. return;
  184. }
  185. var isClipped = false;
  186. if (needsClip) {
  187. // Clip will modify the layout params.
  188. // And return a boolean to determine if the shape are fully clipped.
  189. isClipped = clip[coord.type](coordSysClipArea, layout);
  190. }
  191. var el = elementCreator[coord.type](seriesModel, data, dataIndex, layout, isHorizontalOrRadial, animationModel, baseAxis.model, false, roundCap);
  192. if (realtimeSortCfg) {
  193. /**
  194. * Force label animation because even if the element is
  195. * ignored because it's clipped, it may not be clipped after
  196. * changing order. Then, if not using forceLabelAnimation,
  197. * the label animation was never started, in which case,
  198. * the label will be the final value and doesn't have label
  199. * animation.
  200. */
  201. el.forceLabelAnimation = true;
  202. }
  203. updateStyle(el, data, dataIndex, itemModel, layout, seriesModel, isHorizontalOrRadial, coord.type === 'polar');
  204. if (isInitSort) {
  205. el.attr({
  206. shape: layout
  207. });
  208. } else if (realtimeSortCfg) {
  209. updateRealtimeAnimation(realtimeSortCfg, animationModel, el, layout, dataIndex, isHorizontalOrRadial, false, false);
  210. } else {
  211. initProps(el, {
  212. shape: layout
  213. }, seriesModel, dataIndex);
  214. }
  215. data.setItemGraphicEl(dataIndex, el);
  216. group.add(el);
  217. el.ignore = isClipped;
  218. }).update(function (newIndex, oldIndex) {
  219. var itemModel = data.getItemModel(newIndex);
  220. var layout = getLayout[coord.type](data, newIndex, itemModel);
  221. if (!layout) {
  222. return;
  223. }
  224. if (drawBackground) {
  225. var bgEl = void 0;
  226. if (oldBgEls.length === 0) {
  227. bgEl = createBackground(oldIndex);
  228. } else {
  229. bgEl = oldBgEls[oldIndex];
  230. bgEl.useStyle(backgroundModel.getItemStyle());
  231. // Only cartesian2d support borderRadius.
  232. if (coord.type === 'cartesian2d') {
  233. bgEl.setShape('r', barBorderRadius);
  234. } else {
  235. bgEl.setShape('cornerRadius', barBorderRadius);
  236. }
  237. bgEls[newIndex] = bgEl;
  238. }
  239. var bgLayout = getLayout[coord.type](data, newIndex);
  240. var shape = createBackgroundShape(isHorizontalOrRadial, bgLayout, coord);
  241. updateProps(bgEl, {
  242. shape: shape
  243. }, animationModel, newIndex);
  244. }
  245. var el = oldData.getItemGraphicEl(oldIndex);
  246. if (!data.hasValue(newIndex) || !isValidLayout[coord.type](layout)) {
  247. group.remove(el);
  248. return;
  249. }
  250. var isClipped = false;
  251. if (needsClip) {
  252. isClipped = clip[coord.type](coordSysClipArea, layout);
  253. if (isClipped) {
  254. group.remove(el);
  255. }
  256. }
  257. var roundCapChanged = el && (el.type === 'sector' && roundCap || el.type === 'sausage' && !roundCap);
  258. if (roundCapChanged) {
  259. // roundCap changed, there is no way to use animation from a `sector` to a `sausage` shape,
  260. // so remove the old one and create a new shape
  261. el && removeElementWithFadeOut(el, seriesModel, oldIndex);
  262. el = null;
  263. }
  264. if (!el) {
  265. el = elementCreator[coord.type](seriesModel, data, newIndex, layout, isHorizontalOrRadial, animationModel, baseAxis.model, true, roundCap);
  266. } else {
  267. saveOldStyle(el);
  268. }
  269. if (realtimeSortCfg) {
  270. el.forceLabelAnimation = true;
  271. }
  272. if (isChangeOrder) {
  273. var textEl = el.getTextContent();
  274. if (textEl) {
  275. var labelInnerStore = labelInner(textEl);
  276. if (labelInnerStore.prevValue != null) {
  277. /**
  278. * Set preValue to be value so that no new label
  279. * should be started, otherwise, it will take a full
  280. * `animationDurationUpdate` time to finish the
  281. * animation, which is not expected.
  282. */
  283. labelInnerStore.prevValue = labelInnerStore.value;
  284. }
  285. }
  286. }
  287. // Not change anything if only order changed.
  288. // Especially not change label.
  289. else {
  290. updateStyle(el, data, newIndex, itemModel, layout, seriesModel, isHorizontalOrRadial, coord.type === 'polar');
  291. }
  292. if (isInitSort) {
  293. el.attr({
  294. shape: layout
  295. });
  296. } else if (realtimeSortCfg) {
  297. updateRealtimeAnimation(realtimeSortCfg, animationModel, el, layout, newIndex, isHorizontalOrRadial, true, isChangeOrder);
  298. } else {
  299. updateProps(el, {
  300. shape: layout
  301. }, seriesModel, newIndex, null);
  302. }
  303. data.setItemGraphicEl(newIndex, el);
  304. el.ignore = isClipped;
  305. group.add(el);
  306. }).remove(function (dataIndex) {
  307. var el = oldData.getItemGraphicEl(dataIndex);
  308. el && removeElementWithFadeOut(el, seriesModel, dataIndex);
  309. }).execute();
  310. var bgGroup = this._backgroundGroup || (this._backgroundGroup = new Group());
  311. bgGroup.removeAll();
  312. for (var i = 0; i < bgEls.length; ++i) {
  313. bgGroup.add(bgEls[i]);
  314. }
  315. group.add(bgGroup);
  316. this._backgroundEls = bgEls;
  317. this._data = data;
  318. };
  319. BarView.prototype._renderLarge = function (seriesModel, ecModel, api) {
  320. this._clear();
  321. createLarge(seriesModel, this.group);
  322. this._updateLargeClip(seriesModel);
  323. };
  324. BarView.prototype._incrementalRenderLarge = function (params, seriesModel) {
  325. this._removeBackground();
  326. createLarge(seriesModel, this.group, this._progressiveEls, true);
  327. };
  328. BarView.prototype._updateLargeClip = function (seriesModel) {
  329. // Use clipPath in large mode.
  330. var clipPath = seriesModel.get('clip', true) && createClipPath(seriesModel.coordinateSystem, false, seriesModel);
  331. var group = this.group;
  332. if (clipPath) {
  333. group.setClipPath(clipPath);
  334. } else {
  335. group.removeClipPath();
  336. }
  337. };
  338. BarView.prototype._enableRealtimeSort = function (realtimeSortCfg, data, api) {
  339. var _this = this;
  340. // If no data in the first frame, wait for data to initSort
  341. if (!data.count()) {
  342. return;
  343. }
  344. var baseAxis = realtimeSortCfg.baseAxis;
  345. if (this._isFirstFrame) {
  346. this._dispatchInitSort(data, realtimeSortCfg, api);
  347. this._isFirstFrame = false;
  348. } else {
  349. var orderMapping_1 = function (idx) {
  350. var el = data.getItemGraphicEl(idx);
  351. var shape = el && el.shape;
  352. return shape &&
  353. // The result should be consistent with the initial sort by data value.
  354. // Do not support the case that both positive and negative exist.
  355. Math.abs(baseAxis.isHorizontal() ? shape.height : shape.width)
  356. // If data is NaN, shape.xxx may be NaN, so use || 0 here in case
  357. || 0;
  358. };
  359. this._onRendered = function () {
  360. _this._updateSortWithinSameData(data, orderMapping_1, baseAxis, api);
  361. };
  362. api.getZr().on('rendered', this._onRendered);
  363. }
  364. };
  365. BarView.prototype._dataSort = function (data, baseAxis, orderMapping) {
  366. var info = [];
  367. data.each(data.mapDimension(baseAxis.dim), function (ordinalNumber, dataIdx) {
  368. var mappedValue = orderMapping(dataIdx);
  369. mappedValue = mappedValue == null ? NaN : mappedValue;
  370. info.push({
  371. dataIndex: dataIdx,
  372. mappedValue: mappedValue,
  373. ordinalNumber: ordinalNumber
  374. });
  375. });
  376. info.sort(function (a, b) {
  377. // If NaN, it will be treated as min val.
  378. return b.mappedValue - a.mappedValue;
  379. });
  380. return {
  381. ordinalNumbers: map(info, function (item) {
  382. return item.ordinalNumber;
  383. })
  384. };
  385. };
  386. BarView.prototype._isOrderChangedWithinSameData = function (data, orderMapping, baseAxis) {
  387. var scale = baseAxis.scale;
  388. var ordinalDataDim = data.mapDimension(baseAxis.dim);
  389. var lastValue = Number.MAX_VALUE;
  390. for (var tickNum = 0, len = scale.getOrdinalMeta().categories.length; tickNum < len; ++tickNum) {
  391. var rawIdx = data.rawIndexOf(ordinalDataDim, scale.getRawOrdinalNumber(tickNum));
  392. var value = rawIdx < 0
  393. // If some tick have no bar, the tick will be treated as min.
  394. ? Number.MIN_VALUE
  395. // PENDING: if dataZoom on baseAxis exits, is it a performance issue?
  396. : orderMapping(data.indexOfRawIndex(rawIdx));
  397. if (value > lastValue) {
  398. return true;
  399. }
  400. lastValue = value;
  401. }
  402. return false;
  403. };
  404. /*
  405. * Consider the case when A and B changed order, whose representing
  406. * bars are both out of sight, we don't wish to trigger reorder action
  407. * as long as the order in the view doesn't change.
  408. */
  409. BarView.prototype._isOrderDifferentInView = function (orderInfo, baseAxis) {
  410. var scale = baseAxis.scale;
  411. var extent = scale.getExtent();
  412. var tickNum = Math.max(0, extent[0]);
  413. var tickMax = Math.min(extent[1], scale.getOrdinalMeta().categories.length - 1);
  414. for (; tickNum <= tickMax; ++tickNum) {
  415. if (orderInfo.ordinalNumbers[tickNum] !== scale.getRawOrdinalNumber(tickNum)) {
  416. return true;
  417. }
  418. }
  419. };
  420. BarView.prototype._updateSortWithinSameData = function (data, orderMapping, baseAxis, api) {
  421. if (!this._isOrderChangedWithinSameData(data, orderMapping, baseAxis)) {
  422. return;
  423. }
  424. var sortInfo = this._dataSort(data, baseAxis, orderMapping);
  425. if (this._isOrderDifferentInView(sortInfo, baseAxis)) {
  426. this._removeOnRenderedListener(api);
  427. api.dispatchAction({
  428. type: 'changeAxisOrder',
  429. componentType: baseAxis.dim + 'Axis',
  430. axisId: baseAxis.index,
  431. sortInfo: sortInfo
  432. });
  433. }
  434. };
  435. BarView.prototype._dispatchInitSort = function (data, realtimeSortCfg, api) {
  436. var baseAxis = realtimeSortCfg.baseAxis;
  437. var sortResult = this._dataSort(data, baseAxis, function (dataIdx) {
  438. return data.get(data.mapDimension(realtimeSortCfg.otherAxis.dim), dataIdx);
  439. });
  440. api.dispatchAction({
  441. type: 'changeAxisOrder',
  442. componentType: baseAxis.dim + 'Axis',
  443. isInitSort: true,
  444. axisId: baseAxis.index,
  445. sortInfo: sortResult
  446. });
  447. };
  448. BarView.prototype.remove = function (ecModel, api) {
  449. this._clear(this._model);
  450. this._removeOnRenderedListener(api);
  451. };
  452. BarView.prototype.dispose = function (ecModel, api) {
  453. this._removeOnRenderedListener(api);
  454. };
  455. BarView.prototype._removeOnRenderedListener = function (api) {
  456. if (this._onRendered) {
  457. api.getZr().off('rendered', this._onRendered);
  458. this._onRendered = null;
  459. }
  460. };
  461. BarView.prototype._clear = function (model) {
  462. var group = this.group;
  463. var data = this._data;
  464. if (model && model.isAnimationEnabled() && data && !this._isLargeDraw) {
  465. this._removeBackground();
  466. this._backgroundEls = [];
  467. data.eachItemGraphicEl(function (el) {
  468. removeElementWithFadeOut(el, model, getECData(el).dataIndex);
  469. });
  470. } else {
  471. group.removeAll();
  472. }
  473. this._data = null;
  474. this._isFirstFrame = true;
  475. };
  476. BarView.prototype._removeBackground = function () {
  477. this.group.remove(this._backgroundGroup);
  478. this._backgroundGroup = null;
  479. };
  480. BarView.type = 'bar';
  481. return BarView;
  482. }(ChartView);
  483. var clip = {
  484. cartesian2d: function (coordSysBoundingRect, layout) {
  485. var signWidth = layout.width < 0 ? -1 : 1;
  486. var signHeight = layout.height < 0 ? -1 : 1;
  487. // Needs positive width and height
  488. if (signWidth < 0) {
  489. layout.x += layout.width;
  490. layout.width = -layout.width;
  491. }
  492. if (signHeight < 0) {
  493. layout.y += layout.height;
  494. layout.height = -layout.height;
  495. }
  496. var coordSysX2 = coordSysBoundingRect.x + coordSysBoundingRect.width;
  497. var coordSysY2 = coordSysBoundingRect.y + coordSysBoundingRect.height;
  498. var x = mathMax(layout.x, coordSysBoundingRect.x);
  499. var x2 = mathMin(layout.x + layout.width, coordSysX2);
  500. var y = mathMax(layout.y, coordSysBoundingRect.y);
  501. var y2 = mathMin(layout.y + layout.height, coordSysY2);
  502. var xClipped = x2 < x;
  503. var yClipped = y2 < y;
  504. // When xClipped or yClipped, the element will be marked as `ignore`.
  505. // But we should also place the element at the edge of the coord sys bounding rect.
  506. // Because if data changed and the bar shows again, its transition animation
  507. // will begin at this place.
  508. layout.x = xClipped && x > coordSysX2 ? x2 : x;
  509. layout.y = yClipped && y > coordSysY2 ? y2 : y;
  510. layout.width = xClipped ? 0 : x2 - x;
  511. layout.height = yClipped ? 0 : y2 - y;
  512. // Reverse back
  513. if (signWidth < 0) {
  514. layout.x += layout.width;
  515. layout.width = -layout.width;
  516. }
  517. if (signHeight < 0) {
  518. layout.y += layout.height;
  519. layout.height = -layout.height;
  520. }
  521. return xClipped || yClipped;
  522. },
  523. polar: function (coordSysClipArea, layout) {
  524. var signR = layout.r0 <= layout.r ? 1 : -1;
  525. // Make sure r is larger than r0
  526. if (signR < 0) {
  527. var tmp = layout.r;
  528. layout.r = layout.r0;
  529. layout.r0 = tmp;
  530. }
  531. var r = mathMin(layout.r, coordSysClipArea.r);
  532. var r0 = mathMax(layout.r0, coordSysClipArea.r0);
  533. layout.r = r;
  534. layout.r0 = r0;
  535. var clipped = r - r0 < 0;
  536. // Reverse back
  537. if (signR < 0) {
  538. var tmp = layout.r;
  539. layout.r = layout.r0;
  540. layout.r0 = tmp;
  541. }
  542. return clipped;
  543. }
  544. };
  545. var elementCreator = {
  546. cartesian2d: function (seriesModel, data, newIndex, layout, isHorizontal, animationModel, axisModel, isUpdate, roundCap) {
  547. var rect = new Rect({
  548. shape: extend({}, layout),
  549. z2: 1
  550. });
  551. rect.__dataIndex = newIndex;
  552. rect.name = 'item';
  553. if (animationModel) {
  554. var rectShape = rect.shape;
  555. var animateProperty = isHorizontal ? 'height' : 'width';
  556. rectShape[animateProperty] = 0;
  557. }
  558. return rect;
  559. },
  560. polar: function (seriesModel, data, newIndex, layout, isRadial, animationModel, axisModel, isUpdate, roundCap) {
  561. var ShapeClass = !isRadial && roundCap ? Sausage : Sector;
  562. var sector = new ShapeClass({
  563. shape: layout,
  564. z2: 1
  565. });
  566. sector.name = 'item';
  567. var positionMap = createPolarPositionMapping(isRadial);
  568. sector.calculateTextPosition = createSectorCalculateTextPosition(positionMap, {
  569. isRoundCap: ShapeClass === Sausage
  570. });
  571. // Animation
  572. if (animationModel) {
  573. var sectorShape = sector.shape;
  574. var animateProperty = isRadial ? 'r' : 'endAngle';
  575. var animateTarget = {};
  576. sectorShape[animateProperty] = isRadial ? layout.r0 : layout.startAngle;
  577. animateTarget[animateProperty] = layout[animateProperty];
  578. (isUpdate ? updateProps : initProps)(sector, {
  579. shape: animateTarget
  580. // __value: typeof dataValue === 'string' ? parseInt(dataValue, 10) : dataValue
  581. }, animationModel);
  582. }
  583. return sector;
  584. }
  585. };
  586. function shouldRealtimeSort(seriesModel, coordSys) {
  587. var realtimeSortOption = seriesModel.get('realtimeSort', true);
  588. var baseAxis = coordSys.getBaseAxis();
  589. if (process.env.NODE_ENV !== 'production') {
  590. if (realtimeSortOption) {
  591. if (baseAxis.type !== 'category') {
  592. warn('`realtimeSort` will not work because this bar series is not based on a category axis.');
  593. }
  594. if (coordSys.type !== 'cartesian2d') {
  595. warn('`realtimeSort` will not work because this bar series is not on cartesian2d.');
  596. }
  597. }
  598. }
  599. if (realtimeSortOption && baseAxis.type === 'category' && coordSys.type === 'cartesian2d') {
  600. return {
  601. baseAxis: baseAxis,
  602. otherAxis: coordSys.getOtherAxis(baseAxis)
  603. };
  604. }
  605. }
  606. function updateRealtimeAnimation(realtimeSortCfg, seriesAnimationModel, el, layout, newIndex, isHorizontal, isUpdate, isChangeOrder) {
  607. var seriesTarget;
  608. var axisTarget;
  609. if (isHorizontal) {
  610. axisTarget = {
  611. x: layout.x,
  612. width: layout.width
  613. };
  614. seriesTarget = {
  615. y: layout.y,
  616. height: layout.height
  617. };
  618. } else {
  619. axisTarget = {
  620. y: layout.y,
  621. height: layout.height
  622. };
  623. seriesTarget = {
  624. x: layout.x,
  625. width: layout.width
  626. };
  627. }
  628. if (!isChangeOrder) {
  629. // Keep the original growth animation if only axis order changed.
  630. // Not start a new animation.
  631. (isUpdate ? updateProps : initProps)(el, {
  632. shape: seriesTarget
  633. }, seriesAnimationModel, newIndex, null);
  634. }
  635. var axisAnimationModel = seriesAnimationModel ? realtimeSortCfg.baseAxis.model : null;
  636. (isUpdate ? updateProps : initProps)(el, {
  637. shape: axisTarget
  638. }, axisAnimationModel, newIndex);
  639. }
  640. function checkPropertiesNotValid(obj, props) {
  641. for (var i = 0; i < props.length; i++) {
  642. if (!isFinite(obj[props[i]])) {
  643. return true;
  644. }
  645. }
  646. return false;
  647. }
  648. var rectPropties = ['x', 'y', 'width', 'height'];
  649. var polarPropties = ['cx', 'cy', 'r', 'startAngle', 'endAngle'];
  650. var isValidLayout = {
  651. cartesian2d: function (layout) {
  652. return !checkPropertiesNotValid(layout, rectPropties);
  653. },
  654. polar: function (layout) {
  655. return !checkPropertiesNotValid(layout, polarPropties);
  656. }
  657. };
  658. var getLayout = {
  659. // itemModel is only used to get borderWidth, which is not needed
  660. // when calculating bar background layout.
  661. cartesian2d: function (data, dataIndex, itemModel) {
  662. var layout = data.getItemLayout(dataIndex);
  663. if (!layout) {
  664. return null;
  665. }
  666. var fixedLineWidth = itemModel ? getLineWidth(itemModel, layout) : 0;
  667. // fix layout with lineWidth
  668. var signX = layout.width > 0 ? 1 : -1;
  669. var signY = layout.height > 0 ? 1 : -1;
  670. return {
  671. x: layout.x + signX * fixedLineWidth / 2,
  672. y: layout.y + signY * fixedLineWidth / 2,
  673. width: layout.width - signX * fixedLineWidth,
  674. height: layout.height - signY * fixedLineWidth
  675. };
  676. },
  677. polar: function (data, dataIndex, itemModel) {
  678. var layout = data.getItemLayout(dataIndex);
  679. return {
  680. cx: layout.cx,
  681. cy: layout.cy,
  682. r0: layout.r0,
  683. r: layout.r,
  684. startAngle: layout.startAngle,
  685. endAngle: layout.endAngle,
  686. clockwise: layout.clockwise
  687. };
  688. }
  689. };
  690. function isZeroOnPolar(layout) {
  691. return layout.startAngle != null && layout.endAngle != null && layout.startAngle === layout.endAngle;
  692. }
  693. function createPolarPositionMapping(isRadial) {
  694. return function (isRadial) {
  695. var arcOrAngle = isRadial ? 'Arc' : 'Angle';
  696. return function (position) {
  697. switch (position) {
  698. case 'start':
  699. case 'insideStart':
  700. case 'end':
  701. case 'insideEnd':
  702. return position + arcOrAngle;
  703. default:
  704. return position;
  705. }
  706. };
  707. }(isRadial);
  708. }
  709. function updateStyle(el, data, dataIndex, itemModel, layout, seriesModel, isHorizontalOrRadial, isPolar) {
  710. var style = data.getItemVisual(dataIndex, 'style');
  711. if (!isPolar) {
  712. var borderRadius = itemModel.get(['itemStyle', 'borderRadius']) || 0;
  713. el.setShape('r', borderRadius);
  714. } else if (!seriesModel.get('roundCap')) {
  715. var sectorShape = el.shape;
  716. var cornerRadius = getSectorCornerRadius(itemModel.getModel('itemStyle'), sectorShape, true);
  717. extend(sectorShape, cornerRadius);
  718. el.setShape(sectorShape);
  719. }
  720. el.useStyle(style);
  721. var cursorStyle = itemModel.getShallow('cursor');
  722. cursorStyle && el.attr('cursor', cursorStyle);
  723. var labelPositionOutside = isPolar ? isHorizontalOrRadial ? layout.r >= layout.r0 ? 'endArc' : 'startArc' : layout.endAngle >= layout.startAngle ? 'endAngle' : 'startAngle' : isHorizontalOrRadial ? layout.height >= 0 ? 'bottom' : 'top' : layout.width >= 0 ? 'right' : 'left';
  724. var labelStatesModels = getLabelStatesModels(itemModel);
  725. setLabelStyle(el, labelStatesModels, {
  726. labelFetcher: seriesModel,
  727. labelDataIndex: dataIndex,
  728. defaultText: getDefaultLabel(seriesModel.getData(), dataIndex),
  729. inheritColor: style.fill,
  730. defaultOpacity: style.opacity,
  731. defaultOutsidePosition: labelPositionOutside
  732. });
  733. var label = el.getTextContent();
  734. if (isPolar && label) {
  735. var position = itemModel.get(['label', 'position']);
  736. el.textConfig.inside = position === 'middle' ? true : null;
  737. setSectorTextRotation(el, position === 'outside' ? labelPositionOutside : position, createPolarPositionMapping(isHorizontalOrRadial), itemModel.get(['label', 'rotate']));
  738. }
  739. setLabelValueAnimation(label, labelStatesModels, seriesModel.getRawValue(dataIndex), function (value) {
  740. return getDefaultInterpolatedLabel(data, value);
  741. });
  742. var emphasisModel = itemModel.getModel(['emphasis']);
  743. toggleHoverEmphasis(el, emphasisModel.get('focus'), emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  744. setStatesStylesFromModel(el, itemModel);
  745. if (isZeroOnPolar(layout)) {
  746. el.style.fill = 'none';
  747. el.style.stroke = 'none';
  748. each(el.states, function (state) {
  749. if (state.style) {
  750. state.style.fill = state.style.stroke = 'none';
  751. }
  752. });
  753. }
  754. }
  755. // In case width or height are too small.
  756. function getLineWidth(itemModel, rawLayout) {
  757. // Has no border.
  758. var borderColor = itemModel.get(['itemStyle', 'borderColor']);
  759. if (!borderColor || borderColor === 'none') {
  760. return 0;
  761. }
  762. var lineWidth = itemModel.get(['itemStyle', 'borderWidth']) || 0;
  763. // width or height may be NaN for empty data
  764. var width = isNaN(rawLayout.width) ? Number.MAX_VALUE : Math.abs(rawLayout.width);
  765. var height = isNaN(rawLayout.height) ? Number.MAX_VALUE : Math.abs(rawLayout.height);
  766. return Math.min(lineWidth, width, height);
  767. }
  768. var LagePathShape = /** @class */function () {
  769. function LagePathShape() {}
  770. return LagePathShape;
  771. }();
  772. var LargePath = /** @class */function (_super) {
  773. __extends(LargePath, _super);
  774. function LargePath(opts) {
  775. var _this = _super.call(this, opts) || this;
  776. _this.type = 'largeBar';
  777. return _this;
  778. }
  779. LargePath.prototype.getDefaultShape = function () {
  780. return new LagePathShape();
  781. };
  782. LargePath.prototype.buildPath = function (ctx, shape) {
  783. // Drawing lines is more efficient than drawing
  784. // a whole line or drawing rects.
  785. var points = shape.points;
  786. var baseDimIdx = this.baseDimIdx;
  787. var valueDimIdx = 1 - this.baseDimIdx;
  788. var startPoint = [];
  789. var size = [];
  790. var barWidth = this.barWidth;
  791. for (var i = 0; i < points.length; i += 3) {
  792. size[baseDimIdx] = barWidth;
  793. size[valueDimIdx] = points[i + 2];
  794. startPoint[baseDimIdx] = points[i + baseDimIdx];
  795. startPoint[valueDimIdx] = points[i + valueDimIdx];
  796. ctx.rect(startPoint[0], startPoint[1], size[0], size[1]);
  797. }
  798. };
  799. return LargePath;
  800. }(Path);
  801. function createLarge(seriesModel, group, progressiveEls, incremental) {
  802. // TODO support polar
  803. var data = seriesModel.getData();
  804. var baseDimIdx = data.getLayout('valueAxisHorizontal') ? 1 : 0;
  805. var largeDataIndices = data.getLayout('largeDataIndices');
  806. var barWidth = data.getLayout('size');
  807. var backgroundModel = seriesModel.getModel('backgroundStyle');
  808. var bgPoints = data.getLayout('largeBackgroundPoints');
  809. if (bgPoints) {
  810. var bgEl = new LargePath({
  811. shape: {
  812. points: bgPoints
  813. },
  814. incremental: !!incremental,
  815. silent: true,
  816. z2: 0
  817. });
  818. bgEl.baseDimIdx = baseDimIdx;
  819. bgEl.largeDataIndices = largeDataIndices;
  820. bgEl.barWidth = barWidth;
  821. bgEl.useStyle(backgroundModel.getItemStyle());
  822. group.add(bgEl);
  823. progressiveEls && progressiveEls.push(bgEl);
  824. }
  825. var el = new LargePath({
  826. shape: {
  827. points: data.getLayout('largePoints')
  828. },
  829. incremental: !!incremental,
  830. ignoreCoarsePointer: true,
  831. z2: 1
  832. });
  833. el.baseDimIdx = baseDimIdx;
  834. el.largeDataIndices = largeDataIndices;
  835. el.barWidth = barWidth;
  836. group.add(el);
  837. el.useStyle(data.getVisual('style'));
  838. // Stroke is rendered first to avoid overlapping with fill
  839. el.style.stroke = null;
  840. // Enable tooltip and user mouse/touch event handlers.
  841. getECData(el).seriesIndex = seriesModel.seriesIndex;
  842. if (!seriesModel.get('silent')) {
  843. el.on('mousedown', largePathUpdateDataIndex);
  844. el.on('mousemove', largePathUpdateDataIndex);
  845. }
  846. progressiveEls && progressiveEls.push(el);
  847. }
  848. // Use throttle to avoid frequently traverse to find dataIndex.
  849. var largePathUpdateDataIndex = throttle(function (event) {
  850. var largePath = this;
  851. var dataIndex = largePathFindDataIndex(largePath, event.offsetX, event.offsetY);
  852. getECData(largePath).dataIndex = dataIndex >= 0 ? dataIndex : null;
  853. }, 30, false);
  854. function largePathFindDataIndex(largePath, x, y) {
  855. var baseDimIdx = largePath.baseDimIdx;
  856. var valueDimIdx = 1 - baseDimIdx;
  857. var points = largePath.shape.points;
  858. var largeDataIndices = largePath.largeDataIndices;
  859. var startPoint = [];
  860. var size = [];
  861. var barWidth = largePath.barWidth;
  862. for (var i = 0, len = points.length / 3; i < len; i++) {
  863. var ii = i * 3;
  864. size[baseDimIdx] = barWidth;
  865. size[valueDimIdx] = points[ii + 2];
  866. startPoint[baseDimIdx] = points[ii + baseDimIdx];
  867. startPoint[valueDimIdx] = points[ii + valueDimIdx];
  868. if (size[valueDimIdx] < 0) {
  869. startPoint[valueDimIdx] += size[valueDimIdx];
  870. size[valueDimIdx] = -size[valueDimIdx];
  871. }
  872. if (x >= startPoint[0] && x <= startPoint[0] + size[0] && y >= startPoint[1] && y <= startPoint[1] + size[1]) {
  873. return largeDataIndices[i];
  874. }
  875. }
  876. return -1;
  877. }
  878. function createBackgroundShape(isHorizontalOrRadial, layout, coord) {
  879. if (isCoordinateSystemType(coord, 'cartesian2d')) {
  880. var rectShape = layout;
  881. var coordLayout = coord.getArea();
  882. return {
  883. x: isHorizontalOrRadial ? rectShape.x : coordLayout.x,
  884. y: isHorizontalOrRadial ? coordLayout.y : rectShape.y,
  885. width: isHorizontalOrRadial ? rectShape.width : coordLayout.width,
  886. height: isHorizontalOrRadial ? coordLayout.height : rectShape.height
  887. };
  888. } else {
  889. var coordLayout = coord.getArea();
  890. var sectorShape = layout;
  891. return {
  892. cx: coordLayout.cx,
  893. cy: coordLayout.cy,
  894. r0: isHorizontalOrRadial ? coordLayout.r0 : sectorShape.r0,
  895. r: isHorizontalOrRadial ? coordLayout.r : sectorShape.r,
  896. startAngle: isHorizontalOrRadial ? sectorShape.startAngle : 0,
  897. endAngle: isHorizontalOrRadial ? sectorShape.endAngle : Math.PI * 2
  898. };
  899. }
  900. }
  901. function createBackgroundEl(coord, isHorizontalOrRadial, layout) {
  902. var ElementClz = coord.type === 'polar' ? Sector : Rect;
  903. return new ElementClz({
  904. shape: createBackgroundShape(isHorizontalOrRadial, layout, coord),
  905. silent: true,
  906. z2: 0
  907. });
  908. }
  909. export default BarView;