PieView.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 zrUtil = require("zrender/lib/core/util");
  20. var graphic = require("../../util/graphic");
  21. var ChartView = require("../../view/Chart");
  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. /**
  41. * @param {module:echarts/model/Series} seriesModel
  42. * @param {boolean} hasAnimation
  43. * @inner
  44. */
  45. function updateDataSelected(uid, seriesModel, hasAnimation, api) {
  46. var data = seriesModel.getData();
  47. var dataIndex = this.dataIndex;
  48. var name = data.getName(dataIndex);
  49. var selectedOffset = seriesModel.get('selectedOffset');
  50. api.dispatchAction({
  51. type: 'pieToggleSelect',
  52. from: uid,
  53. name: name,
  54. seriesId: seriesModel.id
  55. });
  56. data.each(function (idx) {
  57. toggleItemSelected(data.getItemGraphicEl(idx), data.getItemLayout(idx), seriesModel.isSelected(data.getName(idx)), selectedOffset, hasAnimation);
  58. });
  59. }
  60. /**
  61. * @param {module:zrender/graphic/Sector} el
  62. * @param {Object} layout
  63. * @param {boolean} isSelected
  64. * @param {number} selectedOffset
  65. * @param {boolean} hasAnimation
  66. * @inner
  67. */
  68. function toggleItemSelected(el, layout, isSelected, selectedOffset, hasAnimation) {
  69. var midAngle = (layout.startAngle + layout.endAngle) / 2;
  70. var dx = Math.cos(midAngle);
  71. var dy = Math.sin(midAngle);
  72. var offset = isSelected ? selectedOffset : 0;
  73. var position = [dx * offset, dy * offset];
  74. hasAnimation // animateTo will stop revious animation like update transition
  75. ? el.animate().when(200, {
  76. position: position
  77. }).start('bounceOut') : el.attr('position', position);
  78. }
  79. /**
  80. * Piece of pie including Sector, Label, LabelLine
  81. * @constructor
  82. * @extends {module:zrender/graphic/Group}
  83. */
  84. function PiePiece(data, idx) {
  85. graphic.Group.call(this);
  86. var sector = new graphic.Sector({
  87. z2: 2
  88. });
  89. var polyline = new graphic.Polyline();
  90. var text = new graphic.Text();
  91. this.add(sector);
  92. this.add(polyline);
  93. this.add(text);
  94. this.updateData(data, idx, true);
  95. }
  96. var piePieceProto = PiePiece.prototype;
  97. piePieceProto.updateData = function (data, idx, firstCreate) {
  98. var sector = this.childAt(0);
  99. var labelLine = this.childAt(1);
  100. var labelText = this.childAt(2);
  101. var seriesModel = data.hostModel;
  102. var itemModel = data.getItemModel(idx);
  103. var layout = data.getItemLayout(idx);
  104. var sectorShape = zrUtil.extend({}, layout);
  105. sectorShape.label = null;
  106. var animationTypeUpdate = seriesModel.getShallow('animationTypeUpdate');
  107. if (firstCreate) {
  108. sector.setShape(sectorShape);
  109. var animationType = seriesModel.getShallow('animationType');
  110. if (animationType === 'scale') {
  111. sector.shape.r = layout.r0;
  112. graphic.initProps(sector, {
  113. shape: {
  114. r: layout.r
  115. }
  116. }, seriesModel, idx);
  117. } // Expansion
  118. else {
  119. sector.shape.endAngle = layout.startAngle;
  120. graphic.updateProps(sector, {
  121. shape: {
  122. endAngle: layout.endAngle
  123. }
  124. }, seriesModel, idx);
  125. }
  126. } else {
  127. if (animationTypeUpdate === 'expansion') {
  128. // Sectors are set to be target shape and an overlaying clipPath is used for animation
  129. sector.setShape(sectorShape);
  130. } else {
  131. // Transition animation from the old shape
  132. graphic.updateProps(sector, {
  133. shape: sectorShape
  134. }, seriesModel, idx);
  135. }
  136. } // Update common style
  137. var visualColor = data.getItemVisual(idx, 'color');
  138. sector.useStyle(zrUtil.defaults({
  139. lineJoin: 'bevel',
  140. fill: visualColor
  141. }, itemModel.getModel('itemStyle').getItemStyle()));
  142. sector.hoverStyle = itemModel.getModel('emphasis.itemStyle').getItemStyle();
  143. var cursorStyle = itemModel.getShallow('cursor');
  144. cursorStyle && sector.attr('cursor', cursorStyle); // Toggle selected
  145. toggleItemSelected(this, data.getItemLayout(idx), seriesModel.isSelected(data.getName(idx)), seriesModel.get('selectedOffset'), seriesModel.get('animation')); // Label and text animation should be applied only for transition type animation when update
  146. var withAnimation = !firstCreate && animationTypeUpdate === 'transition';
  147. this._updateLabel(data, idx, withAnimation);
  148. this.highDownOnUpdate = !seriesModel.get('silent') ? function (fromState, toState) {
  149. var hasAnimation = seriesModel.isAnimationEnabled() && itemModel.get('hoverAnimation');
  150. if (toState === 'emphasis') {
  151. labelLine.ignore = labelLine.hoverIgnore;
  152. labelText.ignore = labelText.hoverIgnore; // Sector may has animation of updating data. Force to move to the last frame
  153. // Or it may stopped on the wrong shape
  154. if (hasAnimation) {
  155. sector.stopAnimation(true);
  156. sector.animateTo({
  157. shape: {
  158. r: layout.r + seriesModel.get('hoverOffset')
  159. }
  160. }, 300, 'elasticOut');
  161. }
  162. } else {
  163. labelLine.ignore = labelLine.normalIgnore;
  164. labelText.ignore = labelText.normalIgnore;
  165. if (hasAnimation) {
  166. sector.stopAnimation(true);
  167. sector.animateTo({
  168. shape: {
  169. r: layout.r
  170. }
  171. }, 300, 'elasticOut');
  172. }
  173. }
  174. } : null;
  175. graphic.setHoverStyle(this);
  176. };
  177. piePieceProto._updateLabel = function (data, idx, withAnimation) {
  178. var labelLine = this.childAt(1);
  179. var labelText = this.childAt(2);
  180. var seriesModel = data.hostModel;
  181. var itemModel = data.getItemModel(idx);
  182. var layout = data.getItemLayout(idx);
  183. var labelLayout = layout.label;
  184. var visualColor = data.getItemVisual(idx, 'color');
  185. if (!labelLayout || isNaN(labelLayout.x) || isNaN(labelLayout.y)) {
  186. labelText.ignore = labelText.normalIgnore = labelText.hoverIgnore = labelLine.ignore = labelLine.normalIgnore = labelLine.hoverIgnore = true;
  187. return;
  188. }
  189. var targetLineShape = {
  190. points: labelLayout.linePoints || [[labelLayout.x, labelLayout.y], [labelLayout.x, labelLayout.y], [labelLayout.x, labelLayout.y]]
  191. };
  192. var targetTextStyle = {
  193. x: labelLayout.x,
  194. y: labelLayout.y
  195. };
  196. if (withAnimation) {
  197. graphic.updateProps(labelLine, {
  198. shape: targetLineShape
  199. }, seriesModel, idx);
  200. graphic.updateProps(labelText, {
  201. style: targetTextStyle
  202. }, seriesModel, idx);
  203. } else {
  204. labelLine.attr({
  205. shape: targetLineShape
  206. });
  207. labelText.attr({
  208. style: targetTextStyle
  209. });
  210. }
  211. labelText.attr({
  212. rotation: labelLayout.rotation,
  213. origin: [labelLayout.x, labelLayout.y],
  214. z2: 10
  215. });
  216. var labelModel = itemModel.getModel('label');
  217. var labelHoverModel = itemModel.getModel('emphasis.label');
  218. var labelLineModel = itemModel.getModel('labelLine');
  219. var labelLineHoverModel = itemModel.getModel('emphasis.labelLine');
  220. var visualColor = data.getItemVisual(idx, 'color');
  221. graphic.setLabelStyle(labelText.style, labelText.hoverStyle = {}, labelModel, labelHoverModel, {
  222. labelFetcher: data.hostModel,
  223. labelDataIndex: idx,
  224. defaultText: labelLayout.text,
  225. autoColor: visualColor,
  226. useInsideStyle: !!labelLayout.inside
  227. }, {
  228. textAlign: labelLayout.textAlign,
  229. textVerticalAlign: labelLayout.verticalAlign,
  230. opacity: data.getItemVisual(idx, 'opacity')
  231. });
  232. labelText.ignore = labelText.normalIgnore = !labelModel.get('show');
  233. labelText.hoverIgnore = !labelHoverModel.get('show');
  234. labelLine.ignore = labelLine.normalIgnore = !labelLineModel.get('show');
  235. labelLine.hoverIgnore = !labelLineHoverModel.get('show'); // Default use item visual color
  236. labelLine.setStyle({
  237. stroke: visualColor,
  238. opacity: data.getItemVisual(idx, 'opacity')
  239. });
  240. labelLine.setStyle(labelLineModel.getModel('lineStyle').getLineStyle());
  241. labelLine.hoverStyle = labelLineHoverModel.getModel('lineStyle').getLineStyle();
  242. var smooth = labelLineModel.get('smooth');
  243. if (smooth && smooth === true) {
  244. smooth = 0.4;
  245. }
  246. labelLine.setShape({
  247. smooth: smooth
  248. });
  249. };
  250. zrUtil.inherits(PiePiece, graphic.Group); // Pie view
  251. var PieView = ChartView.extend({
  252. type: 'pie',
  253. init: function () {
  254. var sectorGroup = new graphic.Group();
  255. this._sectorGroup = sectorGroup;
  256. },
  257. render: function (seriesModel, ecModel, api, payload) {
  258. if (payload && payload.from === this.uid) {
  259. return;
  260. }
  261. var data = seriesModel.getData();
  262. var oldData = this._data;
  263. var group = this.group;
  264. var hasAnimation = ecModel.get('animation');
  265. var isFirstRender = !oldData;
  266. var animationType = seriesModel.get('animationType');
  267. var animationTypeUpdate = seriesModel.get('animationTypeUpdate');
  268. var onSectorClick = zrUtil.curry(updateDataSelected, this.uid, seriesModel, hasAnimation, api);
  269. var selectedMode = seriesModel.get('selectedMode');
  270. data.diff(oldData).add(function (idx) {
  271. var piePiece = new PiePiece(data, idx); // Default expansion animation
  272. if (isFirstRender && animationType !== 'scale') {
  273. piePiece.eachChild(function (child) {
  274. child.stopAnimation(true);
  275. });
  276. }
  277. selectedMode && piePiece.on('click', onSectorClick);
  278. data.setItemGraphicEl(idx, piePiece);
  279. group.add(piePiece);
  280. }).update(function (newIdx, oldIdx) {
  281. var piePiece = oldData.getItemGraphicEl(oldIdx);
  282. if (!isFirstRender && animationTypeUpdate !== 'transition') {
  283. piePiece.eachChild(function (child) {
  284. child.stopAnimation(true);
  285. });
  286. }
  287. piePiece.updateData(data, newIdx);
  288. piePiece.off('click');
  289. selectedMode && piePiece.on('click', onSectorClick);
  290. group.add(piePiece);
  291. data.setItemGraphicEl(newIdx, piePiece);
  292. }).remove(function (idx) {
  293. var piePiece = oldData.getItemGraphicEl(idx);
  294. group.remove(piePiece);
  295. }).execute();
  296. if (hasAnimation && data.count() > 0 && (isFirstRender ? animationType !== 'scale' : animationTypeUpdate !== 'transition')) {
  297. var shape = data.getItemLayout(0);
  298. for (var s = 1; isNaN(shape.startAngle) && s < data.count(); ++s) {
  299. shape = data.getItemLayout(s);
  300. }
  301. var r = Math.max(api.getWidth(), api.getHeight()) / 2;
  302. var removeClipPath = zrUtil.bind(group.removeClipPath, group);
  303. group.setClipPath(this._createClipPath(shape.cx, shape.cy, r, shape.startAngle, shape.clockwise, removeClipPath, seriesModel, isFirstRender));
  304. } else {
  305. // clipPath is used in first-time animation, so remove it when otherwise. See: #8994
  306. group.removeClipPath();
  307. }
  308. this._data = data;
  309. },
  310. dispose: function () {},
  311. _createClipPath: function (cx, cy, r, startAngle, clockwise, cb, seriesModel, isFirstRender) {
  312. var clipPath = new graphic.Sector({
  313. shape: {
  314. cx: cx,
  315. cy: cy,
  316. r0: 0,
  317. r: r,
  318. startAngle: startAngle,
  319. endAngle: startAngle,
  320. clockwise: clockwise
  321. }
  322. });
  323. var initOrUpdate = isFirstRender ? graphic.initProps : graphic.updateProps;
  324. initOrUpdate(clipPath, {
  325. shape: {
  326. endAngle: startAngle + (clockwise ? 1 : -1) * Math.PI * 2
  327. }
  328. }, seriesModel, cb);
  329. return clipPath;
  330. },
  331. /**
  332. * @implement
  333. */
  334. containPoint: function (point, seriesModel) {
  335. var data = seriesModel.getData();
  336. var itemLayout = data.getItemLayout(0);
  337. if (itemLayout) {
  338. var dx = point[0] - itemLayout.cx;
  339. var dy = point[1] - itemLayout.cy;
  340. var radius = Math.sqrt(dx * dx + dy * dy);
  341. return radius <= itemLayout.r && radius >= itemLayout.r0;
  342. }
  343. }
  344. });
  345. var _default = PieView;
  346. module.exports = _default;