SunburstView.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 = /** @class */function (_super) {
  48. __extends(SunburstView, _super);
  49. function SunburstView() {
  50. var _this = _super !== null && _super.apply(this, arguments) || this;
  51. _this.type = SunburstView.type;
  52. return _this;
  53. }
  54. SunburstView.prototype.render = function (seriesModel, ecModel, api,
  55. // @ts-ignore
  56. payload) {
  57. var self = this;
  58. this.seriesModel = seriesModel;
  59. this.api = api;
  60. this.ecModel = ecModel;
  61. var data = seriesModel.getData();
  62. var virtualRoot = data.tree.root;
  63. var newRoot = seriesModel.getViewRoot();
  64. var group = this.group;
  65. var renderLabelForZeroData = seriesModel.get('renderLabelForZeroData');
  66. var newChildren = [];
  67. newRoot.eachNode(function (node) {
  68. newChildren.push(node);
  69. });
  70. var oldChildren = this._oldChildren || [];
  71. dualTravel(newChildren, oldChildren);
  72. renderRollUp(virtualRoot, newRoot);
  73. this._initEvents();
  74. this._oldChildren = newChildren;
  75. function dualTravel(newChildren, oldChildren) {
  76. if (newChildren.length === 0 && oldChildren.length === 0) {
  77. return;
  78. }
  79. new DataDiffer(oldChildren, newChildren, getKey, getKey).add(processNode).update(processNode).remove(zrUtil.curry(processNode, null)).execute();
  80. function getKey(node) {
  81. return node.getId();
  82. }
  83. function processNode(newIdx, oldIdx) {
  84. var newNode = newIdx == null ? null : newChildren[newIdx];
  85. var oldNode = oldIdx == null ? null : oldChildren[oldIdx];
  86. doRenderNode(newNode, oldNode);
  87. }
  88. }
  89. function doRenderNode(newNode, oldNode) {
  90. if (!renderLabelForZeroData && newNode && !newNode.getValue()) {
  91. // Not render data with value 0
  92. newNode = null;
  93. }
  94. if (newNode !== virtualRoot && oldNode !== virtualRoot) {
  95. if (oldNode && oldNode.piece) {
  96. if (newNode) {
  97. // Update
  98. oldNode.piece.updateData(false, newNode, seriesModel, ecModel, api);
  99. // 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);
  109. // For tooltip
  110. data.setItemGraphicEl(newNode.dataIndex, piece);
  111. }
  112. }
  113. }
  114. function removeNode(node) {
  115. if (!node) {
  116. return;
  117. }
  118. if (node.piece) {
  119. group.remove(node.piece);
  120. node.piece = null;
  121. }
  122. }
  123. function renderRollUp(virtualRoot, viewRoot) {
  124. if (viewRoot.depth > 0) {
  125. // Render
  126. if (self.virtualPiece) {
  127. // Update
  128. self.virtualPiece.updateData(false, virtualRoot, seriesModel, ecModel, api);
  129. } else {
  130. // Add
  131. self.virtualPiece = new SunburstPiece(virtualRoot, seriesModel, ecModel, api);
  132. group.add(self.virtualPiece);
  133. }
  134. // TODO event scope
  135. viewRoot.piece.off('click');
  136. self.virtualPiece.on('click', function (e) {
  137. self._rootToNode(viewRoot.parentNode);
  138. });
  139. } else if (self.virtualPiece) {
  140. // Remove
  141. group.remove(self.virtualPiece);
  142. self.virtualPiece = null;
  143. }
  144. }
  145. };
  146. /**
  147. * @private
  148. */
  149. SunburstView.prototype._initEvents = function () {
  150. var _this = this;
  151. this.group.off('click');
  152. this.group.on('click', function (e) {
  153. var targetFound = false;
  154. var viewRoot = _this.seriesModel.getViewRoot();
  155. viewRoot.eachNode(function (node) {
  156. if (!targetFound && node.piece && node.piece === e.target) {
  157. var nodeClick = node.getModel().get('nodeClick');
  158. if (nodeClick === 'rootToNode') {
  159. _this._rootToNode(node);
  160. } else if (nodeClick === 'link') {
  161. var itemModel = node.getModel();
  162. var link = itemModel.get('link');
  163. if (link) {
  164. var linkTarget = itemModel.get('target', true) || '_blank';
  165. windowOpen(link, linkTarget);
  166. }
  167. }
  168. targetFound = true;
  169. }
  170. });
  171. });
  172. };
  173. /**
  174. * @private
  175. */
  176. SunburstView.prototype._rootToNode = function (node) {
  177. if (node !== this.seriesModel.getViewRoot()) {
  178. this.api.dispatchAction({
  179. type: ROOT_TO_NODE_ACTION,
  180. from: this.uid,
  181. seriesId: this.seriesModel.id,
  182. targetNode: node
  183. });
  184. }
  185. };
  186. /**
  187. * @implement
  188. */
  189. SunburstView.prototype.containPoint = function (point, seriesModel) {
  190. var treeRoot = seriesModel.getData();
  191. var itemLayout = treeRoot.getItemLayout(0);
  192. if (itemLayout) {
  193. var dx = point[0] - itemLayout.cx;
  194. var dy = point[1] - itemLayout.cy;
  195. var radius = Math.sqrt(dx * dx + dy * dy);
  196. return radius <= itemLayout.r && radius >= itemLayout.r0;
  197. }
  198. };
  199. SunburstView.type = 'sunburst';
  200. return SunburstView;
  201. }(ChartView);
  202. export default SunburstView;