SunburstSeries.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 zrUtil = require("zrender/lib/core/util");
  20. var SeriesModel = require("../../model/Series");
  21. var Tree = require("../../data/Tree");
  22. var _treeHelper = require("../helper/treeHelper");
  23. var wrapTreePathInfo = _treeHelper.wrapTreePathInfo;
  24. /*
  25. * Licensed to the Apache Software Foundation (ASF) under one
  26. * or more contributor license agreements. See the NOTICE file
  27. * distributed with this work for additional information
  28. * regarding copyright ownership. The ASF licenses this file
  29. * to you under the Apache License, Version 2.0 (the
  30. * "License"); you may not use this file except in compliance
  31. * with the License. You may obtain a copy of the License at
  32. *
  33. * http://www.apache.org/licenses/LICENSE-2.0
  34. *
  35. * Unless required by applicable law or agreed to in writing,
  36. * software distributed under the License is distributed on an
  37. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  38. * KIND, either express or implied. See the License for the
  39. * specific language governing permissions and limitations
  40. * under the License.
  41. */
  42. var _default = SeriesModel.extend({
  43. type: 'series.sunburst',
  44. /**
  45. * @type {module:echarts/data/Tree~Node}
  46. */
  47. _viewRoot: null,
  48. getInitialData: function (option, ecModel) {
  49. // Create a virtual root.
  50. var root = {
  51. name: option.name,
  52. children: option.data
  53. };
  54. completeTreeValue(root);
  55. var levels = option.levels || []; // levels = option.levels = setDefault(levels, ecModel);
  56. var treeOption = {};
  57. treeOption.levels = levels; // Make sure always a new tree is created when setOption,
  58. // in TreemapView, we check whether oldTree === newTree
  59. // to choose mappings approach among old shapes and new shapes.
  60. return Tree.createTree(root, this, treeOption).data;
  61. },
  62. optionUpdated: function () {
  63. this.resetViewRoot();
  64. },
  65. /*
  66. * @override
  67. */
  68. getDataParams: function (dataIndex) {
  69. var params = SeriesModel.prototype.getDataParams.apply(this, arguments);
  70. var node = this.getData().tree.getNodeByDataIndex(dataIndex);
  71. params.treePathInfo = wrapTreePathInfo(node, this);
  72. return params;
  73. },
  74. defaultOption: {
  75. zlevel: 0,
  76. z: 2,
  77. // 默认全局居中
  78. center: ['50%', '50%'],
  79. radius: [0, '75%'],
  80. // 默认顺时针
  81. clockwise: true,
  82. startAngle: 90,
  83. // 最小角度改为0
  84. minAngle: 0,
  85. percentPrecision: 2,
  86. // If still show when all data zero.
  87. stillShowZeroSum: true,
  88. // Policy of highlighting pieces when hover on one
  89. // Valid values: 'none' (for not downplay others), 'descendant',
  90. // 'ancestor', 'self'
  91. highlightPolicy: 'descendant',
  92. // 'rootToNode', 'link', or false
  93. nodeClick: 'rootToNode',
  94. renderLabelForZeroData: false,
  95. label: {
  96. // could be: 'radial', 'tangential', or 'none'
  97. rotate: 'radial',
  98. show: true,
  99. opacity: 1,
  100. // 'left' is for inner side of inside, and 'right' is for outter
  101. // side for inside
  102. align: 'center',
  103. position: 'inside',
  104. distance: 5,
  105. silent: true
  106. },
  107. itemStyle: {
  108. borderWidth: 1,
  109. borderColor: 'white',
  110. borderType: 'solid',
  111. shadowBlur: 0,
  112. shadowColor: 'rgba(0, 0, 0, 0.2)',
  113. shadowOffsetX: 0,
  114. shadowOffsetY: 0,
  115. opacity: 1
  116. },
  117. highlight: {
  118. itemStyle: {
  119. opacity: 1
  120. }
  121. },
  122. downplay: {
  123. itemStyle: {
  124. opacity: 0.5
  125. },
  126. label: {
  127. opacity: 0.6
  128. }
  129. },
  130. // Animation type canbe expansion, scale
  131. animationType: 'expansion',
  132. animationDuration: 1000,
  133. animationDurationUpdate: 500,
  134. animationEasing: 'cubicOut',
  135. data: [],
  136. levels: [],
  137. /**
  138. * Sort order.
  139. *
  140. * Valid values: 'desc', 'asc', null, or callback function.
  141. * 'desc' and 'asc' for descend and ascendant order;
  142. * null for not sorting;
  143. * example of callback function:
  144. * function(nodeA, nodeB) {
  145. * return nodeA.getValue() - nodeB.getValue();
  146. * }
  147. */
  148. sort: 'desc'
  149. },
  150. getViewRoot: function () {
  151. return this._viewRoot;
  152. },
  153. /**
  154. * @param {module:echarts/data/Tree~Node} [viewRoot]
  155. */
  156. resetViewRoot: function (viewRoot) {
  157. viewRoot ? this._viewRoot = viewRoot : viewRoot = this._viewRoot;
  158. var root = this.getRawData().tree.root;
  159. if (!viewRoot || viewRoot !== root && !root.contains(viewRoot)) {
  160. this._viewRoot = root;
  161. }
  162. }
  163. });
  164. /**
  165. * @param {Object} dataNode
  166. */
  167. function completeTreeValue(dataNode) {
  168. // Postorder travel tree.
  169. // If value of none-leaf node is not set,
  170. // calculate it by suming up the value of all children.
  171. var sum = 0;
  172. zrUtil.each(dataNode.children, function (child) {
  173. completeTreeValue(child);
  174. var childValue = child.value;
  175. zrUtil.isArray(childValue) && (childValue = childValue[0]);
  176. sum += childValue;
  177. });
  178. var thisValue = dataNode.value;
  179. if (zrUtil.isArray(thisValue)) {
  180. thisValue = thisValue[0];
  181. }
  182. if (thisValue == null || isNaN(thisValue)) {
  183. thisValue = sum;
  184. } // Value should not less than 0.
  185. if (thisValue < 0) {
  186. thisValue = 0;
  187. }
  188. zrUtil.isArray(dataNode.value) ? dataNode.value[0] = thisValue : dataNode.value = thisValue;
  189. }
  190. module.exports = _default;