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