SunburstPiece.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. /*
  22. * Licensed to the Apache Software Foundation (ASF) under one
  23. * or more contributor license agreements. See the NOTICE file
  24. * distributed with this work for additional information
  25. * regarding copyright ownership. The ASF licenses this file
  26. * to you under the Apache License, Version 2.0 (the
  27. * "License"); you may not use this file except in compliance
  28. * with the License. You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing,
  33. * software distributed under the License is distributed on an
  34. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  35. * KIND, either express or implied. See the License for the
  36. * specific language governing permissions and limitations
  37. * under the License.
  38. */
  39. var NodeHighlightPolicy = {
  40. NONE: 'none',
  41. // not downplay others
  42. DESCENDANT: 'descendant',
  43. ANCESTOR: 'ancestor',
  44. SELF: 'self'
  45. };
  46. var DEFAULT_SECTOR_Z = 2;
  47. var DEFAULT_TEXT_Z = 4;
  48. /**
  49. * Sunburstce of Sunburst including Sector, Label, LabelLine
  50. * @constructor
  51. * @extends {module:zrender/graphic/Group}
  52. */
  53. function SunburstPiece(node, seriesModel, ecModel) {
  54. graphic.Group.call(this);
  55. var sector = new graphic.Sector({
  56. z2: DEFAULT_SECTOR_Z
  57. });
  58. sector.seriesIndex = seriesModel.seriesIndex;
  59. var text = new graphic.Text({
  60. z2: DEFAULT_TEXT_Z,
  61. silent: node.getModel('label').get('silent')
  62. });
  63. this.add(sector);
  64. this.add(text);
  65. this.updateData(true, node, 'normal', seriesModel, ecModel); // Hover to change label and labelLine
  66. function onEmphasis() {
  67. text.ignore = text.hoverIgnore;
  68. }
  69. function onNormal() {
  70. text.ignore = text.normalIgnore;
  71. }
  72. this.on('emphasis', onEmphasis).on('normal', onNormal).on('mouseover', onEmphasis).on('mouseout', onNormal);
  73. }
  74. var SunburstPieceProto = SunburstPiece.prototype;
  75. SunburstPieceProto.updateData = function (firstCreate, node, state, seriesModel, ecModel) {
  76. this.node = node;
  77. node.piece = this;
  78. seriesModel = seriesModel || this._seriesModel;
  79. ecModel = ecModel || this._ecModel;
  80. var sector = this.childAt(0);
  81. sector.dataIndex = node.dataIndex;
  82. var itemModel = node.getModel();
  83. var layout = node.getLayout(); // if (!layout) {
  84. // console.log(node.getLayout());
  85. // }
  86. var sectorShape = zrUtil.extend({}, layout);
  87. sectorShape.label = null;
  88. var visualColor = getNodeColor(node, seriesModel, ecModel);
  89. fillDefaultColor(node, seriesModel, visualColor);
  90. var normalStyle = itemModel.getModel('itemStyle').getItemStyle();
  91. var style;
  92. if (state === 'normal') {
  93. style = normalStyle;
  94. } else {
  95. var stateStyle = itemModel.getModel(state + '.itemStyle').getItemStyle();
  96. style = zrUtil.merge(stateStyle, normalStyle);
  97. }
  98. style = zrUtil.defaults({
  99. lineJoin: 'bevel',
  100. fill: style.fill || visualColor
  101. }, style);
  102. if (firstCreate) {
  103. sector.setShape(sectorShape);
  104. sector.shape.r = layout.r0;
  105. graphic.updateProps(sector, {
  106. shape: {
  107. r: layout.r
  108. }
  109. }, seriesModel, node.dataIndex);
  110. sector.useStyle(style);
  111. } else if (typeof style.fill === 'object' && style.fill.type || typeof sector.style.fill === 'object' && sector.style.fill.type) {
  112. // Disable animation for gradient since no interpolation method
  113. // is supported for gradient
  114. graphic.updateProps(sector, {
  115. shape: sectorShape
  116. }, seriesModel);
  117. sector.useStyle(style);
  118. } else {
  119. graphic.updateProps(sector, {
  120. shape: sectorShape,
  121. style: style
  122. }, seriesModel);
  123. }
  124. this._updateLabel(seriesModel, visualColor, state);
  125. var cursorStyle = itemModel.getShallow('cursor');
  126. cursorStyle && sector.attr('cursor', cursorStyle);
  127. if (firstCreate) {
  128. var highlightPolicy = seriesModel.getShallow('highlightPolicy');
  129. this._initEvents(sector, node, seriesModel, highlightPolicy);
  130. }
  131. this._seriesModel = seriesModel || this._seriesModel;
  132. this._ecModel = ecModel || this._ecModel;
  133. graphic.setHoverStyle(this);
  134. };
  135. SunburstPieceProto.onEmphasis = function (highlightPolicy) {
  136. var that = this;
  137. this.node.hostTree.root.eachNode(function (n) {
  138. if (n.piece) {
  139. if (that.node === n) {
  140. n.piece.updateData(false, n, 'emphasis');
  141. } else if (isNodeHighlighted(n, that.node, highlightPolicy)) {
  142. n.piece.childAt(0).trigger('highlight');
  143. } else if (highlightPolicy !== NodeHighlightPolicy.NONE) {
  144. n.piece.childAt(0).trigger('downplay');
  145. }
  146. }
  147. });
  148. };
  149. SunburstPieceProto.onNormal = function () {
  150. this.node.hostTree.root.eachNode(function (n) {
  151. if (n.piece) {
  152. n.piece.updateData(false, n, 'normal');
  153. }
  154. });
  155. };
  156. SunburstPieceProto.onHighlight = function () {
  157. this.updateData(false, this.node, 'highlight');
  158. };
  159. SunburstPieceProto.onDownplay = function () {
  160. this.updateData(false, this.node, 'downplay');
  161. };
  162. SunburstPieceProto._updateLabel = function (seriesModel, visualColor, state) {
  163. var itemModel = this.node.getModel();
  164. var normalModel = itemModel.getModel('label');
  165. var labelModel = state === 'normal' || state === 'emphasis' ? normalModel : itemModel.getModel(state + '.label');
  166. var labelHoverModel = itemModel.getModel('emphasis.label');
  167. var text = zrUtil.retrieve(seriesModel.getFormattedLabel(this.node.dataIndex, state, null, null, 'label'), this.node.name);
  168. if (getLabelAttr('show') === false) {
  169. text = '';
  170. }
  171. var layout = this.node.getLayout();
  172. var labelMinAngle = labelModel.get('minAngle');
  173. if (labelMinAngle == null) {
  174. labelMinAngle = normalModel.get('minAngle');
  175. }
  176. labelMinAngle = labelMinAngle / 180 * Math.PI;
  177. var angle = layout.endAngle - layout.startAngle;
  178. if (labelMinAngle != null && Math.abs(angle) < labelMinAngle) {
  179. // Not displaying text when angle is too small
  180. text = '';
  181. }
  182. var label = this.childAt(1);
  183. graphic.setLabelStyle(label.style, label.hoverStyle || {}, normalModel, labelHoverModel, {
  184. defaultText: labelModel.getShallow('show') ? text : null,
  185. autoColor: visualColor,
  186. useInsideStyle: true
  187. });
  188. var midAngle = (layout.startAngle + layout.endAngle) / 2;
  189. var dx = Math.cos(midAngle);
  190. var dy = Math.sin(midAngle);
  191. var r;
  192. var labelPosition = getLabelAttr('position');
  193. var labelPadding = getLabelAttr('distance') || 0;
  194. var textAlign = getLabelAttr('align');
  195. if (labelPosition === 'outside') {
  196. r = layout.r + labelPadding;
  197. textAlign = midAngle > Math.PI / 2 ? 'right' : 'left';
  198. } else {
  199. if (!textAlign || textAlign === 'center') {
  200. r = (layout.r + layout.r0) / 2;
  201. textAlign = 'center';
  202. } else if (textAlign === 'left') {
  203. r = layout.r0 + labelPadding;
  204. if (midAngle > Math.PI / 2) {
  205. textAlign = 'right';
  206. }
  207. } else if (textAlign === 'right') {
  208. r = layout.r - labelPadding;
  209. if (midAngle > Math.PI / 2) {
  210. textAlign = 'left';
  211. }
  212. }
  213. }
  214. label.attr('style', {
  215. text: text,
  216. textAlign: textAlign,
  217. textVerticalAlign: getLabelAttr('verticalAlign') || 'middle',
  218. opacity: getLabelAttr('opacity')
  219. });
  220. var textX = r * dx + layout.cx;
  221. var textY = r * dy + layout.cy;
  222. label.attr('position', [textX, textY]);
  223. var rotateType = getLabelAttr('rotate');
  224. var rotate = 0;
  225. if (rotateType === 'radial') {
  226. rotate = -midAngle;
  227. if (rotate < -Math.PI / 2) {
  228. rotate += Math.PI;
  229. }
  230. } else if (rotateType === 'tangential') {
  231. rotate = Math.PI / 2 - midAngle;
  232. if (rotate > Math.PI / 2) {
  233. rotate -= Math.PI;
  234. } else if (rotate < -Math.PI / 2) {
  235. rotate += Math.PI;
  236. }
  237. } else if (typeof rotateType === 'number') {
  238. rotate = rotateType * Math.PI / 180;
  239. }
  240. label.attr('rotation', rotate);
  241. function getLabelAttr(name) {
  242. var stateAttr = labelModel.get(name);
  243. if (stateAttr == null) {
  244. return normalModel.get(name);
  245. } else {
  246. return stateAttr;
  247. }
  248. }
  249. };
  250. SunburstPieceProto._initEvents = function (sector, node, seriesModel, highlightPolicy) {
  251. sector.off('mouseover').off('mouseout').off('emphasis').off('normal');
  252. var that = this;
  253. var onEmphasis = function () {
  254. that.onEmphasis(highlightPolicy);
  255. };
  256. var onNormal = function () {
  257. that.onNormal();
  258. };
  259. var onDownplay = function () {
  260. that.onDownplay();
  261. };
  262. var onHighlight = function () {
  263. that.onHighlight();
  264. };
  265. if (seriesModel.isAnimationEnabled()) {
  266. sector.on('mouseover', onEmphasis).on('mouseout', onNormal).on('emphasis', onEmphasis).on('normal', onNormal).on('downplay', onDownplay).on('highlight', onHighlight);
  267. }
  268. };
  269. zrUtil.inherits(SunburstPiece, graphic.Group);
  270. var _default = SunburstPiece;
  271. /**
  272. * Get node color
  273. *
  274. * @param {TreeNode} node the node to get color
  275. * @param {module:echarts/model/Series} seriesModel series
  276. * @param {module:echarts/model/Global} ecModel echarts defaults
  277. */
  278. function getNodeColor(node, seriesModel, ecModel) {
  279. // Color from visualMap
  280. var visualColor = node.getVisual('color');
  281. var visualMetaList = node.getVisual('visualMeta');
  282. if (!visualMetaList || visualMetaList.length === 0) {
  283. // Use first-generation color if has no visualMap
  284. visualColor = null;
  285. } // Self color or level color
  286. var color = node.getModel('itemStyle').get('color');
  287. if (color) {
  288. return color;
  289. } else if (visualColor) {
  290. // Color mapping
  291. return visualColor;
  292. } else if (node.depth === 0) {
  293. // Virtual root node
  294. return ecModel.option.color[0];
  295. } else {
  296. // First-generation color
  297. var length = ecModel.option.color.length;
  298. color = ecModel.option.color[getRootId(node) % length];
  299. }
  300. return color;
  301. }
  302. /**
  303. * Get index of root in sorted order
  304. *
  305. * @param {TreeNode} node current node
  306. * @return {number} index in root
  307. */
  308. function getRootId(node) {
  309. var ancestor = node;
  310. while (ancestor.depth > 1) {
  311. ancestor = ancestor.parentNode;
  312. }
  313. var virtualRoot = node.getAncestors()[0];
  314. return zrUtil.indexOf(virtualRoot.children, ancestor);
  315. }
  316. function isNodeHighlighted(node, activeNode, policy) {
  317. if (policy === NodeHighlightPolicy.NONE) {
  318. return false;
  319. } else if (policy === NodeHighlightPolicy.SELF) {
  320. return node === activeNode;
  321. } else if (policy === NodeHighlightPolicy.ANCESTOR) {
  322. return node === activeNode || node.isAncestorOf(activeNode);
  323. } else {
  324. return node === activeNode || node.isDescendantOf(activeNode);
  325. }
  326. } // Fix tooltip callback function params.color incorrect when pick a default color
  327. function fillDefaultColor(node, seriesModel, color) {
  328. var data = seriesModel.getData();
  329. data.setItemVisual(node.dataIndex, 'color', color);
  330. }
  331. module.exports = _default;