BarView.js 33 KB

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