SunburstSeries.js 6.2 KB

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