sunburstLayout.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 { parsePercent } from '../../util/number.js';
  41. import * as zrUtil from 'zrender/lib/core/util.js'; // let PI2 = Math.PI * 2;
  42. var RADIAN = Math.PI / 180;
  43. export default function sunburstLayout(seriesType, ecModel, api) {
  44. ecModel.eachSeriesByType(seriesType, function (seriesModel) {
  45. var center = seriesModel.get('center');
  46. var radius = seriesModel.get('radius');
  47. if (!zrUtil.isArray(radius)) {
  48. radius = [0, radius];
  49. }
  50. if (!zrUtil.isArray(center)) {
  51. center = [center, center];
  52. }
  53. var width = api.getWidth();
  54. var height = api.getHeight();
  55. var size = Math.min(width, height);
  56. var cx = parsePercent(center[0], width);
  57. var cy = parsePercent(center[1], height);
  58. var r0 = parsePercent(radius[0], size / 2);
  59. var r = parsePercent(radius[1], size / 2);
  60. var startAngle = -seriesModel.get('startAngle') * RADIAN;
  61. var minAngle = seriesModel.get('minAngle') * RADIAN;
  62. var virtualRoot = seriesModel.getData().tree.root;
  63. var treeRoot = seriesModel.getViewRoot();
  64. var rootDepth = treeRoot.depth;
  65. var sort = seriesModel.get('sort');
  66. if (sort != null) {
  67. initChildren(treeRoot, sort);
  68. }
  69. var validDataCount = 0;
  70. zrUtil.each(treeRoot.children, function (child) {
  71. !isNaN(child.getValue()) && validDataCount++;
  72. });
  73. var sum = treeRoot.getValue(); // Sum may be 0
  74. var unitRadian = Math.PI / (sum || validDataCount) * 2;
  75. var renderRollupNode = treeRoot.depth > 0;
  76. var levels = treeRoot.height - (renderRollupNode ? -1 : 1);
  77. var rPerLevel = (r - r0) / (levels || 1);
  78. var clockwise = seriesModel.get('clockwise');
  79. var stillShowZeroSum = seriesModel.get('stillShowZeroSum'); // In the case some sector angle is smaller than minAngle
  80. // let restAngle = PI2;
  81. // let valueSumLargerThanMinAngle = 0;
  82. var dir = clockwise ? 1 : -1;
  83. /**
  84. * Render a tree
  85. * @return increased angle
  86. */
  87. var renderNode = function (node, startAngle) {
  88. if (!node) {
  89. return;
  90. }
  91. var endAngle = startAngle; // Render self
  92. if (node !== virtualRoot) {
  93. // Tree node is virtual, so it doesn't need to be drawn
  94. var value = node.getValue();
  95. var angle = sum === 0 && stillShowZeroSum ? unitRadian : value * unitRadian;
  96. if (angle < minAngle) {
  97. angle = minAngle; // restAngle -= minAngle;
  98. } // else {
  99. // valueSumLargerThanMinAngle += value;
  100. // }
  101. endAngle = startAngle + dir * angle;
  102. var depth = node.depth - rootDepth - (renderRollupNode ? -1 : 1);
  103. var rStart = r0 + rPerLevel * depth;
  104. var rEnd = r0 + rPerLevel * (depth + 1);
  105. var levelModel = seriesModel.getLevelModel(node);
  106. if (levelModel) {
  107. var r0_1 = levelModel.get('r0', true);
  108. var r_1 = levelModel.get('r', true);
  109. var radius_1 = levelModel.get('radius', true);
  110. if (radius_1 != null) {
  111. r0_1 = radius_1[0];
  112. r_1 = radius_1[1];
  113. }
  114. r0_1 != null && (rStart = parsePercent(r0_1, size / 2));
  115. r_1 != null && (rEnd = parsePercent(r_1, size / 2));
  116. }
  117. node.setLayout({
  118. angle: angle,
  119. startAngle: startAngle,
  120. endAngle: endAngle,
  121. clockwise: clockwise,
  122. cx: cx,
  123. cy: cy,
  124. r0: rStart,
  125. r: rEnd
  126. });
  127. } // Render children
  128. if (node.children && node.children.length) {
  129. // currentAngle = startAngle;
  130. var siblingAngle_1 = 0;
  131. zrUtil.each(node.children, function (node) {
  132. siblingAngle_1 += renderNode(node, startAngle + siblingAngle_1);
  133. });
  134. }
  135. return endAngle - startAngle;
  136. }; // Virtual root node for roll up
  137. if (renderRollupNode) {
  138. var rStart = r0;
  139. var rEnd = r0 + rPerLevel;
  140. var angle = Math.PI * 2;
  141. virtualRoot.setLayout({
  142. angle: angle,
  143. startAngle: startAngle,
  144. endAngle: startAngle + angle,
  145. clockwise: clockwise,
  146. cx: cx,
  147. cy: cy,
  148. r0: rStart,
  149. r: rEnd
  150. });
  151. }
  152. renderNode(treeRoot, startAngle);
  153. });
  154. }
  155. /**
  156. * Init node children by order and update visual
  157. */
  158. function initChildren(node, sortOrder) {
  159. var children = node.children || [];
  160. node.children = sort(children, sortOrder); // Init children recursively
  161. if (children.length) {
  162. zrUtil.each(node.children, function (child) {
  163. initChildren(child, sortOrder);
  164. });
  165. }
  166. }
  167. /**
  168. * Sort children nodes
  169. *
  170. * @param {TreeNode[]} children children of node to be sorted
  171. * @param {string | function | null} sort sort method
  172. * See SunburstSeries.js for details.
  173. */
  174. function sort(children, sortOrder) {
  175. if (zrUtil.isFunction(sortOrder)) {
  176. var sortTargets = zrUtil.map(children, function (child, idx) {
  177. var value = child.getValue();
  178. return {
  179. params: {
  180. depth: child.depth,
  181. height: child.height,
  182. dataIndex: child.dataIndex,
  183. getValue: function () {
  184. return value;
  185. }
  186. },
  187. index: idx
  188. };
  189. });
  190. sortTargets.sort(function (a, b) {
  191. return sortOrder(a.params, b.params);
  192. });
  193. return zrUtil.map(sortTargets, function (target) {
  194. return children[target.index];
  195. });
  196. } else {
  197. var isAsc_1 = sortOrder === 'asc';
  198. return children.sort(function (a, b) {
  199. var diff = (a.getValue() - b.getValue()) * (isAsc_1 ? 1 : -1);
  200. return diff === 0 ? (a.dataIndex - b.dataIndex) * (isAsc_1 ? -1 : 1) : diff;
  201. });
  202. }
  203. }