SunburstView.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 ChartView = require("../../view/Chart");
  21. var SunburstPiece = require("./SunburstPiece");
  22. var DataDiffer = require("../../data/DataDiffer");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. var ROOT_TO_NODE_ACTION = 'sunburstRootToNode';
  42. var SunburstView = ChartView.extend({
  43. type: 'sunburst',
  44. init: function () {},
  45. render: function (seriesModel, ecModel, api, payload) {
  46. var that = this;
  47. this.seriesModel = seriesModel;
  48. this.api = api;
  49. this.ecModel = ecModel;
  50. var data = seriesModel.getData();
  51. var virtualRoot = data.tree.root;
  52. var newRoot = seriesModel.getViewRoot();
  53. var group = this.group;
  54. var renderLabelForZeroData = seriesModel.get('renderLabelForZeroData');
  55. var newChildren = [];
  56. newRoot.eachNode(function (node) {
  57. newChildren.push(node);
  58. });
  59. var oldChildren = this._oldChildren || [];
  60. dualTravel(newChildren, oldChildren);
  61. renderRollUp(virtualRoot, newRoot);
  62. if (payload && payload.highlight && payload.highlight.piece) {
  63. var highlightPolicy = seriesModel.getShallow('highlightPolicy');
  64. payload.highlight.piece.onEmphasis(highlightPolicy);
  65. } else if (payload && payload.unhighlight) {
  66. var piece = this.virtualPiece;
  67. if (!piece && virtualRoot.children.length) {
  68. piece = virtualRoot.children[0].piece;
  69. }
  70. if (piece) {
  71. piece.onNormal();
  72. }
  73. }
  74. this._initEvents();
  75. this._oldChildren = newChildren;
  76. function dualTravel(newChildren, oldChildren) {
  77. if (newChildren.length === 0 && oldChildren.length === 0) {
  78. return;
  79. }
  80. new DataDiffer(oldChildren, newChildren, getKey, getKey).add(processNode).update(processNode).remove(zrUtil.curry(processNode, null)).execute();
  81. function getKey(node) {
  82. return node.getId();
  83. }
  84. function processNode(newId, oldId) {
  85. var newNode = newId == null ? null : newChildren[newId];
  86. var oldNode = oldId == null ? null : oldChildren[oldId];
  87. doRenderNode(newNode, oldNode);
  88. }
  89. }
  90. function doRenderNode(newNode, oldNode) {
  91. if (!renderLabelForZeroData && newNode && !newNode.getValue()) {
  92. // Not render data with value 0
  93. newNode = null;
  94. }
  95. if (newNode !== virtualRoot && oldNode !== virtualRoot) {
  96. if (oldNode && oldNode.piece) {
  97. if (newNode) {
  98. // Update
  99. oldNode.piece.updateData(false, newNode, 'normal', seriesModel, ecModel); // For tooltip
  100. data.setItemGraphicEl(newNode.dataIndex, oldNode.piece);
  101. } else {
  102. // Remove
  103. removeNode(oldNode);
  104. }
  105. } else if (newNode) {
  106. // Add
  107. var piece = new SunburstPiece(newNode, seriesModel, ecModel);
  108. group.add(piece); // For tooltip
  109. data.setItemGraphicEl(newNode.dataIndex, piece);
  110. }
  111. }
  112. }
  113. function removeNode(node) {
  114. if (!node) {
  115. return;
  116. }
  117. if (node.piece) {
  118. group.remove(node.piece);
  119. node.piece = null;
  120. }
  121. }
  122. function renderRollUp(virtualRoot, viewRoot) {
  123. if (viewRoot.depth > 0) {
  124. // Render
  125. if (that.virtualPiece) {
  126. // Update
  127. that.virtualPiece.updateData(false, virtualRoot, 'normal', seriesModel, ecModel);
  128. } else {
  129. // Add
  130. that.virtualPiece = new SunburstPiece(virtualRoot, seriesModel, ecModel);
  131. group.add(that.virtualPiece);
  132. }
  133. if (viewRoot.piece._onclickEvent) {
  134. viewRoot.piece.off('click', viewRoot.piece._onclickEvent);
  135. }
  136. var event = function (e) {
  137. that._rootToNode(viewRoot.parentNode);
  138. };
  139. viewRoot.piece._onclickEvent = event;
  140. that.virtualPiece.on('click', event);
  141. } else if (that.virtualPiece) {
  142. // Remove
  143. group.remove(that.virtualPiece);
  144. that.virtualPiece = null;
  145. }
  146. }
  147. },
  148. dispose: function () {},
  149. /**
  150. * @private
  151. */
  152. _initEvents: function () {
  153. var that = this;
  154. var event = function (e) {
  155. var targetFound = false;
  156. var viewRoot = that.seriesModel.getViewRoot();
  157. viewRoot.eachNode(function (node) {
  158. if (!targetFound && node.piece && node.piece.childAt(0) === e.target) {
  159. var nodeClick = node.getModel().get('nodeClick');
  160. if (nodeClick === 'rootToNode') {
  161. that._rootToNode(node);
  162. } else if (nodeClick === 'link') {
  163. var itemModel = node.getModel();
  164. var link = itemModel.get('link');
  165. if (link) {
  166. var linkTarget = itemModel.get('target', true) || '_blank';
  167. window.open(link, linkTarget);
  168. }
  169. }
  170. targetFound = true;
  171. }
  172. });
  173. };
  174. if (this.group._onclickEvent) {
  175. this.group.off('click', this.group._onclickEvent);
  176. }
  177. this.group.on('click', event);
  178. this.group._onclickEvent = event;
  179. },
  180. /**
  181. * @private
  182. */
  183. _rootToNode: function (node) {
  184. if (node !== this.seriesModel.getViewRoot()) {
  185. this.api.dispatchAction({
  186. type: ROOT_TO_NODE_ACTION,
  187. from: this.uid,
  188. seriesId: this.seriesModel.id,
  189. targetNode: node
  190. });
  191. }
  192. },
  193. /**
  194. * @implement
  195. */
  196. containPoint: function (point, seriesModel) {
  197. var treeRoot = seriesModel.getData();
  198. var itemLayout = treeRoot.getItemLayout(0);
  199. if (itemLayout) {
  200. var dx = point[0] - itemLayout.cx;
  201. var dy = point[1] - itemLayout.cy;
  202. var radius = Math.sqrt(dx * dx + dy * dy);
  203. return radius <= itemLayout.r && radius >= itemLayout.r0;
  204. }
  205. }
  206. });
  207. var _default = SunburstView;
  208. module.exports = _default;