sunburstLayout.js 7.3 KB

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