SunburstView.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 * as zrUtil from 'zrender/lib/core/util.js';
  42. import ChartView from '../../view/Chart.js';
  43. import SunburstPiece from './SunburstPiece.js';
  44. import DataDiffer from '../../data/DataDiffer.js';
  45. import { ROOT_TO_NODE_ACTION } from './sunburstAction.js';
  46. import { windowOpen } from '../../util/format.js';
  47. var SunburstView =
  48. /** @class */
  49. function (_super) {
  50. __extends(SunburstView, _super);
  51. function SunburstView() {
  52. var _this = _super !== null && _super.apply(this, arguments) || this;
  53. _this.type = SunburstView.type;
  54. return _this;
  55. }
  56. SunburstView.prototype.render = function (seriesModel, ecModel, api, // @ts-ignore
  57. payload) {
  58. var self = this;
  59. this.seriesModel = seriesModel;
  60. this.api = api;
  61. this.ecModel = ecModel;
  62. var data = seriesModel.getData();
  63. var virtualRoot = data.tree.root;
  64. var newRoot = seriesModel.getViewRoot();
  65. var group = this.group;
  66. var renderLabelForZeroData = seriesModel.get('renderLabelForZeroData');
  67. var newChildren = [];
  68. newRoot.eachNode(function (node) {
  69. newChildren.push(node);
  70. });
  71. var oldChildren = this._oldChildren || [];
  72. dualTravel(newChildren, oldChildren);
  73. renderRollUp(virtualRoot, newRoot);
  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(newIdx, oldIdx) {
  85. var newNode = newIdx == null ? null : newChildren[newIdx];
  86. var oldNode = oldIdx == null ? null : oldChildren[oldIdx];
  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, seriesModel, ecModel, api); // 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, api);
  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 (self.virtualPiece) {
  126. // Update
  127. self.virtualPiece.updateData(false, virtualRoot, seriesModel, ecModel, api);
  128. } else {
  129. // Add
  130. self.virtualPiece = new SunburstPiece(virtualRoot, seriesModel, ecModel, api);
  131. group.add(self.virtualPiece);
  132. } // TODO event scope
  133. viewRoot.piece.off('click');
  134. self.virtualPiece.on('click', function (e) {
  135. self._rootToNode(viewRoot.parentNode);
  136. });
  137. } else if (self.virtualPiece) {
  138. // Remove
  139. group.remove(self.virtualPiece);
  140. self.virtualPiece = null;
  141. }
  142. }
  143. };
  144. /**
  145. * @private
  146. */
  147. SunburstView.prototype._initEvents = function () {
  148. var _this = this;
  149. this.group.off('click');
  150. this.group.on('click', function (e) {
  151. var targetFound = false;
  152. var viewRoot = _this.seriesModel.getViewRoot();
  153. viewRoot.eachNode(function (node) {
  154. if (!targetFound && node.piece && node.piece === e.target) {
  155. var nodeClick = node.getModel().get('nodeClick');
  156. if (nodeClick === 'rootToNode') {
  157. _this._rootToNode(node);
  158. } else if (nodeClick === 'link') {
  159. var itemModel = node.getModel();
  160. var link = itemModel.get('link');
  161. if (link) {
  162. var linkTarget = itemModel.get('target', true) || '_blank';
  163. windowOpen(link, linkTarget);
  164. }
  165. }
  166. targetFound = true;
  167. }
  168. });
  169. });
  170. };
  171. /**
  172. * @private
  173. */
  174. SunburstView.prototype._rootToNode = function (node) {
  175. if (node !== this.seriesModel.getViewRoot()) {
  176. this.api.dispatchAction({
  177. type: ROOT_TO_NODE_ACTION,
  178. from: this.uid,
  179. seriesId: this.seriesModel.id,
  180. targetNode: node
  181. });
  182. }
  183. };
  184. /**
  185. * @implement
  186. */
  187. SunburstView.prototype.containPoint = function (point, seriesModel) {
  188. var treeRoot = seriesModel.getData();
  189. var itemLayout = treeRoot.getItemLayout(0);
  190. if (itemLayout) {
  191. var dx = point[0] - itemLayout.cx;
  192. var dy = point[1] - itemLayout.cy;
  193. var radius = Math.sqrt(dx * dx + dy * dy);
  194. return radius <= itemLayout.r && radius >= itemLayout.r0;
  195. }
  196. };
  197. SunburstView.type = 'sunburst';
  198. return SunburstView;
  199. }(ChartView);
  200. export default SunburstView;