sunburstLayout.js 7.1 KB

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