BarView.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. var _config = require("../../config");
  20. var __DEV__ = _config.__DEV__;
  21. var echarts = require("../../echarts");
  22. var zrUtil = require("zrender/lib/core/util");
  23. var graphic = require("../../util/graphic");
  24. var _helper = require("./helper");
  25. var setLabel = _helper.setLabel;
  26. var Model = require("../../model/Model");
  27. var barItemStyle = require("./barItemStyle");
  28. var Path = require("zrender/lib/graphic/Path");
  29. var _throttle = require("../../util/throttle");
  30. var throttle = _throttle.throttle;
  31. var _createClipPathFromCoordSys = require("../helper/createClipPathFromCoordSys");
  32. var createClipPath = _createClipPathFromCoordSys.createClipPath;
  33. var Sausage = require("../../util/shape/sausage");
  34. /*
  35. * Licensed to the Apache Software Foundation (ASF) under one
  36. * or more contributor license agreements. See the NOTICE file
  37. * distributed with this work for additional information
  38. * regarding copyright ownership. The ASF licenses this file
  39. * to you under the Apache License, Version 2.0 (the
  40. * "License"); you may not use this file except in compliance
  41. * with the License. You may obtain a copy of the License at
  42. *
  43. * http://www.apache.org/licenses/LICENSE-2.0
  44. *
  45. * Unless required by applicable law or agreed to in writing,
  46. * software distributed under the License is distributed on an
  47. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  48. * KIND, either express or implied. See the License for the
  49. * specific language governing permissions and limitations
  50. * under the License.
  51. */
  52. var BAR_BORDER_WIDTH_QUERY = ['itemStyle', 'barBorderWidth'];
  53. var _eventPos = [0, 0]; // FIXME
  54. // Just for compatible with ec2.
  55. zrUtil.extend(Model.prototype, barItemStyle);
  56. function getClipArea(coord, data) {
  57. var coordSysClipArea = coord.getArea && coord.getArea();
  58. if (coord.type === 'cartesian2d') {
  59. var baseAxis = coord.getBaseAxis(); // When boundaryGap is false or using time axis. bar may exceed the grid.
  60. // We should not clip this part.
  61. // See test/bar2.html
  62. if (baseAxis.type !== 'category' || !baseAxis.onBand) {
  63. var expandWidth = data.getLayout('bandWidth');
  64. if (baseAxis.isHorizontal()) {
  65. coordSysClipArea.x -= expandWidth;
  66. coordSysClipArea.width += expandWidth * 2;
  67. } else {
  68. coordSysClipArea.y -= expandWidth;
  69. coordSysClipArea.height += expandWidth * 2;
  70. }
  71. }
  72. }
  73. return coordSysClipArea;
  74. }
  75. var _default = echarts.extendChartView({
  76. type: 'bar',
  77. render: function (seriesModel, ecModel, api) {
  78. this._updateDrawMode(seriesModel);
  79. var coordinateSystemType = seriesModel.get('coordinateSystem');
  80. if (coordinateSystemType === 'cartesian2d' || coordinateSystemType === 'polar') {
  81. this._isLargeDraw ? this._renderLarge(seriesModel, ecModel, api) : this._renderNormal(seriesModel, ecModel, api);
  82. } else {}
  83. return this.group;
  84. },
  85. incrementalPrepareRender: function (seriesModel, ecModel, api) {
  86. this._clear();
  87. this._updateDrawMode(seriesModel);
  88. },
  89. incrementalRender: function (params, seriesModel, ecModel, api) {
  90. // Do not support progressive in normal mode.
  91. this._incrementalRenderLarge(params, seriesModel);
  92. },
  93. _updateDrawMode: function (seriesModel) {
  94. var isLargeDraw = seriesModel.pipelineContext.large;
  95. if (this._isLargeDraw == null || isLargeDraw ^ this._isLargeDraw) {
  96. this._isLargeDraw = isLargeDraw;
  97. this._clear();
  98. }
  99. },
  100. _renderNormal: function (seriesModel, ecModel, api) {
  101. var group = this.group;
  102. var data = seriesModel.getData();
  103. var oldData = this._data;
  104. var coord = seriesModel.coordinateSystem;
  105. var baseAxis = coord.getBaseAxis();
  106. var isHorizontalOrRadial;
  107. if (coord.type === 'cartesian2d') {
  108. isHorizontalOrRadial = baseAxis.isHorizontal();
  109. } else if (coord.type === 'polar') {
  110. isHorizontalOrRadial = baseAxis.dim === 'angle';
  111. }
  112. var animationModel = seriesModel.isAnimationEnabled() ? seriesModel : null;
  113. var needsClip = seriesModel.get('clip', true);
  114. var coordSysClipArea = getClipArea(coord, data); // If there is clipPath created in large mode. Remove it.
  115. group.removeClipPath(); // We don't use clipPath in normal mode because we needs a perfect animation
  116. // And don't want the label are clipped.
  117. var roundCap = seriesModel.get('roundCap', true);
  118. data.diff(oldData).add(function (dataIndex) {
  119. if (!data.hasValue(dataIndex)) {
  120. return;
  121. }
  122. var itemModel = data.getItemModel(dataIndex);
  123. var layout = getLayout[coord.type](data, dataIndex, itemModel);
  124. if (needsClip) {
  125. // Clip will modify the layout params.
  126. // And return a boolean to determine if the shape are fully clipped.
  127. var isClipped = clip[coord.type](coordSysClipArea, layout);
  128. if (isClipped) {
  129. group.remove(el);
  130. return;
  131. }
  132. }
  133. var el = elementCreator[coord.type](dataIndex, layout, isHorizontalOrRadial, animationModel, false, roundCap);
  134. data.setItemGraphicEl(dataIndex, el);
  135. group.add(el);
  136. updateStyle(el, data, dataIndex, itemModel, layout, seriesModel, isHorizontalOrRadial, coord.type === 'polar');
  137. }).update(function (newIndex, oldIndex) {
  138. var el = oldData.getItemGraphicEl(oldIndex);
  139. if (!data.hasValue(newIndex)) {
  140. group.remove(el);
  141. return;
  142. }
  143. var itemModel = data.getItemModel(newIndex);
  144. var layout = getLayout[coord.type](data, newIndex, itemModel);
  145. if (needsClip) {
  146. var isClipped = clip[coord.type](coordSysClipArea, layout);
  147. if (isClipped) {
  148. group.remove(el);
  149. return;
  150. }
  151. }
  152. if (el) {
  153. graphic.updateProps(el, {
  154. shape: layout
  155. }, animationModel, newIndex);
  156. } else {
  157. el = elementCreator[coord.type](newIndex, layout, isHorizontalOrRadial, animationModel, true, roundCap);
  158. }
  159. data.setItemGraphicEl(newIndex, el); // Add back
  160. group.add(el);
  161. updateStyle(el, data, newIndex, itemModel, layout, seriesModel, isHorizontalOrRadial, coord.type === 'polar');
  162. }).remove(function (dataIndex) {
  163. var el = oldData.getItemGraphicEl(dataIndex);
  164. if (coord.type === 'cartesian2d') {
  165. el && removeRect(dataIndex, animationModel, el);
  166. } else {
  167. el && removeSector(dataIndex, animationModel, el);
  168. }
  169. }).execute();
  170. this._data = data;
  171. },
  172. _renderLarge: function (seriesModel, ecModel, api) {
  173. this._clear();
  174. createLarge(seriesModel, this.group); // Use clipPath in large mode.
  175. var clipPath = seriesModel.get('clip', true) ? createClipPath(seriesModel.coordinateSystem, false, seriesModel) : null;
  176. if (clipPath) {
  177. this.group.setClipPath(clipPath);
  178. } else {
  179. this.group.removeClipPath();
  180. }
  181. },
  182. _incrementalRenderLarge: function (params, seriesModel) {
  183. createLarge(seriesModel, this.group, true);
  184. },
  185. dispose: zrUtil.noop,
  186. remove: function (ecModel) {
  187. this._clear(ecModel);
  188. },
  189. _clear: function (ecModel) {
  190. var group = this.group;
  191. var data = this._data;
  192. if (ecModel && ecModel.get('animation') && data && !this._isLargeDraw) {
  193. data.eachItemGraphicEl(function (el) {
  194. if (el.type === 'sector') {
  195. removeSector(el.dataIndex, ecModel, el);
  196. } else {
  197. removeRect(el.dataIndex, ecModel, el);
  198. }
  199. });
  200. } else {
  201. group.removeAll();
  202. }
  203. this._data = null;
  204. }
  205. });
  206. var mathMax = Math.max;
  207. var mathMin = Math.min;
  208. var clip = {
  209. cartesian2d: function (coordSysBoundingRect, layout) {
  210. var signWidth = layout.width < 0 ? -1 : 1;
  211. var signHeight = layout.height < 0 ? -1 : 1; // Needs positive width and height
  212. if (signWidth < 0) {
  213. layout.x += layout.width;
  214. layout.width = -layout.width;
  215. }
  216. if (signHeight < 0) {
  217. layout.y += layout.height;
  218. layout.height = -layout.height;
  219. }
  220. var x = mathMax(layout.x, coordSysBoundingRect.x);
  221. var x2 = mathMin(layout.x + layout.width, coordSysBoundingRect.x + coordSysBoundingRect.width);
  222. var y = mathMax(layout.y, coordSysBoundingRect.y);
  223. var y2 = mathMin(layout.y + layout.height, coordSysBoundingRect.y + coordSysBoundingRect.height);
  224. layout.x = x;
  225. layout.y = y;
  226. layout.width = x2 - x;
  227. layout.height = y2 - y;
  228. var clipped = layout.width < 0 || layout.height < 0; // Reverse back
  229. if (signWidth < 0) {
  230. layout.x += layout.width;
  231. layout.width = -layout.width;
  232. }
  233. if (signHeight < 0) {
  234. layout.y += layout.height;
  235. layout.height = -layout.height;
  236. }
  237. return clipped;
  238. },
  239. polar: function (coordSysClipArea) {
  240. return false;
  241. }
  242. };
  243. var elementCreator = {
  244. cartesian2d: function (dataIndex, layout, isHorizontal, animationModel, isUpdate) {
  245. var rect = new graphic.Rect({
  246. shape: zrUtil.extend({}, layout)
  247. }); // Animation
  248. if (animationModel) {
  249. var rectShape = rect.shape;
  250. var animateProperty = isHorizontal ? 'height' : 'width';
  251. var animateTarget = {};
  252. rectShape[animateProperty] = 0;
  253. animateTarget[animateProperty] = layout[animateProperty];
  254. graphic[isUpdate ? 'updateProps' : 'initProps'](rect, {
  255. shape: animateTarget
  256. }, animationModel, dataIndex);
  257. }
  258. return rect;
  259. },
  260. polar: function (dataIndex, layout, isRadial, animationModel, isUpdate, roundCap) {
  261. // Keep the same logic with bar in catesion: use end value to control
  262. // direction. Notice that if clockwise is true (by default), the sector
  263. // will always draw clockwisely, no matter whether endAngle is greater
  264. // or less than startAngle.
  265. var clockwise = layout.startAngle < layout.endAngle;
  266. var ShapeClass = !isRadial && roundCap ? Sausage : graphic.Sector;
  267. var sector = new ShapeClass({
  268. shape: zrUtil.defaults({
  269. clockwise: clockwise
  270. }, layout)
  271. }); // Animation
  272. if (animationModel) {
  273. var sectorShape = sector.shape;
  274. var animateProperty = isRadial ? 'r' : 'endAngle';
  275. var animateTarget = {};
  276. sectorShape[animateProperty] = isRadial ? 0 : layout.startAngle;
  277. animateTarget[animateProperty] = layout[animateProperty];
  278. graphic[isUpdate ? 'updateProps' : 'initProps'](sector, {
  279. shape: animateTarget
  280. }, animationModel, dataIndex);
  281. }
  282. return sector;
  283. }
  284. };
  285. function removeRect(dataIndex, animationModel, el) {
  286. // Not show text when animating
  287. el.style.text = null;
  288. graphic.updateProps(el, {
  289. shape: {
  290. width: 0
  291. }
  292. }, animationModel, dataIndex, function () {
  293. el.parent && el.parent.remove(el);
  294. });
  295. }
  296. function removeSector(dataIndex, animationModel, el) {
  297. // Not show text when animating
  298. el.style.text = null;
  299. graphic.updateProps(el, {
  300. shape: {
  301. r: el.shape.r0
  302. }
  303. }, animationModel, dataIndex, function () {
  304. el.parent && el.parent.remove(el);
  305. });
  306. }
  307. var getLayout = {
  308. cartesian2d: function (data, dataIndex, itemModel) {
  309. var layout = data.getItemLayout(dataIndex);
  310. var fixedLineWidth = getLineWidth(itemModel, layout); // fix layout with lineWidth
  311. var signX = layout.width > 0 ? 1 : -1;
  312. var signY = layout.height > 0 ? 1 : -1;
  313. return {
  314. x: layout.x + signX * fixedLineWidth / 2,
  315. y: layout.y + signY * fixedLineWidth / 2,
  316. width: layout.width - signX * fixedLineWidth,
  317. height: layout.height - signY * fixedLineWidth
  318. };
  319. },
  320. polar: function (data, dataIndex, itemModel) {
  321. var layout = data.getItemLayout(dataIndex);
  322. return {
  323. cx: layout.cx,
  324. cy: layout.cy,
  325. r0: layout.r0,
  326. r: layout.r,
  327. startAngle: layout.startAngle,
  328. endAngle: layout.endAngle
  329. };
  330. }
  331. };
  332. function isZeroOnPolar(layout) {
  333. return layout.startAngle != null && layout.endAngle != null && layout.startAngle === layout.endAngle;
  334. }
  335. function updateStyle(el, data, dataIndex, itemModel, layout, seriesModel, isHorizontal, isPolar) {
  336. var color = data.getItemVisual(dataIndex, 'color');
  337. var opacity = data.getItemVisual(dataIndex, 'opacity');
  338. var stroke = data.getVisual('borderColor');
  339. var itemStyleModel = itemModel.getModel('itemStyle');
  340. var hoverStyle = itemModel.getModel('emphasis.itemStyle').getBarItemStyle();
  341. if (!isPolar) {
  342. el.setShape('r', itemStyleModel.get('barBorderRadius') || 0);
  343. }
  344. el.useStyle(zrUtil.defaults({
  345. stroke: isZeroOnPolar(layout) ? 'none' : stroke,
  346. fill: isZeroOnPolar(layout) ? 'none' : color,
  347. opacity: opacity
  348. }, itemStyleModel.getBarItemStyle()));
  349. var cursorStyle = itemModel.getShallow('cursor');
  350. cursorStyle && el.attr('cursor', cursorStyle);
  351. var labelPositionOutside = isHorizontal ? layout.height > 0 ? 'bottom' : 'top' : layout.width > 0 ? 'left' : 'right';
  352. if (!isPolar) {
  353. setLabel(el.style, hoverStyle, itemModel, color, seriesModel, dataIndex, labelPositionOutside);
  354. }
  355. if (isZeroOnPolar(layout)) {
  356. hoverStyle.fill = hoverStyle.stroke = 'none';
  357. }
  358. graphic.setHoverStyle(el, hoverStyle);
  359. } // In case width or height are too small.
  360. function getLineWidth(itemModel, rawLayout) {
  361. var lineWidth = itemModel.get(BAR_BORDER_WIDTH_QUERY) || 0;
  362. return Math.min(lineWidth, Math.abs(rawLayout.width), Math.abs(rawLayout.height));
  363. }
  364. var LargePath = Path.extend({
  365. type: 'largeBar',
  366. shape: {
  367. points: []
  368. },
  369. buildPath: function (ctx, shape) {
  370. // Drawing lines is more efficient than drawing
  371. // a whole line or drawing rects.
  372. var points = shape.points;
  373. var startPoint = this.__startPoint;
  374. var baseDimIdx = this.__baseDimIdx;
  375. for (var i = 0; i < points.length; i += 2) {
  376. startPoint[baseDimIdx] = points[i + baseDimIdx];
  377. ctx.moveTo(startPoint[0], startPoint[1]);
  378. ctx.lineTo(points[i], points[i + 1]);
  379. }
  380. }
  381. });
  382. function createLarge(seriesModel, group, incremental) {
  383. // TODO support polar
  384. var data = seriesModel.getData();
  385. var startPoint = [];
  386. var baseDimIdx = data.getLayout('valueAxisHorizontal') ? 1 : 0;
  387. startPoint[1 - baseDimIdx] = data.getLayout('valueAxisStart');
  388. var el = new LargePath({
  389. shape: {
  390. points: data.getLayout('largePoints')
  391. },
  392. incremental: !!incremental,
  393. __startPoint: startPoint,
  394. __baseDimIdx: baseDimIdx,
  395. __largeDataIndices: data.getLayout('largeDataIndices'),
  396. __barWidth: data.getLayout('barWidth')
  397. });
  398. group.add(el);
  399. setLargeStyle(el, seriesModel, data); // Enable tooltip and user mouse/touch event handlers.
  400. el.seriesIndex = seriesModel.seriesIndex;
  401. if (!seriesModel.get('silent')) {
  402. el.on('mousedown', largePathUpdateDataIndex);
  403. el.on('mousemove', largePathUpdateDataIndex);
  404. }
  405. } // Use throttle to avoid frequently traverse to find dataIndex.
  406. var largePathUpdateDataIndex = throttle(function (event) {
  407. var largePath = this;
  408. var dataIndex = largePathFindDataIndex(largePath, event.offsetX, event.offsetY);
  409. largePath.dataIndex = dataIndex >= 0 ? dataIndex : null;
  410. }, 30, false);
  411. function largePathFindDataIndex(largePath, x, y) {
  412. var baseDimIdx = largePath.__baseDimIdx;
  413. var valueDimIdx = 1 - baseDimIdx;
  414. var points = largePath.shape.points;
  415. var largeDataIndices = largePath.__largeDataIndices;
  416. var barWidthHalf = Math.abs(largePath.__barWidth / 2);
  417. var startValueVal = largePath.__startPoint[valueDimIdx];
  418. _eventPos[0] = x;
  419. _eventPos[1] = y;
  420. var pointerBaseVal = _eventPos[baseDimIdx];
  421. var pointerValueVal = _eventPos[1 - baseDimIdx];
  422. var baseLowerBound = pointerBaseVal - barWidthHalf;
  423. var baseUpperBound = pointerBaseVal + barWidthHalf;
  424. for (var i = 0, len = points.length / 2; i < len; i++) {
  425. var ii = i * 2;
  426. var barBaseVal = points[ii + baseDimIdx];
  427. var barValueVal = points[ii + valueDimIdx];
  428. if (barBaseVal >= baseLowerBound && barBaseVal <= baseUpperBound && (startValueVal <= barValueVal ? pointerValueVal >= startValueVal && pointerValueVal <= barValueVal : pointerValueVal >= barValueVal && pointerValueVal <= startValueVal)) {
  429. return largeDataIndices[i];
  430. }
  431. }
  432. return -1;
  433. }
  434. function setLargeStyle(el, seriesModel, data) {
  435. var borderColor = data.getVisual('borderColor') || data.getVisual('color');
  436. var itemStyle = seriesModel.getModel('itemStyle').getItemStyle(['color', 'borderColor']);
  437. el.useStyle(itemStyle);
  438. el.style.fill = null;
  439. el.style.stroke = borderColor;
  440. el.style.lineWidth = data.getLayout('barWidth');
  441. }
  442. module.exports = _default;