PieSeries.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 echarts = require("../../echarts");
  20. var createListSimply = require("../helper/createListSimply");
  21. var zrUtil = require("zrender/lib/core/util");
  22. var modelUtil = require("../../util/model");
  23. var _number = require("../../util/number");
  24. var getPercentWithPrecision = _number.getPercentWithPrecision;
  25. var dataSelectableMixin = require("../../component/helper/selectableMixin");
  26. var _dataProvider = require("../../data/helper/dataProvider");
  27. var retrieveRawAttr = _dataProvider.retrieveRawAttr;
  28. var _sourceHelper = require("../../data/helper/sourceHelper");
  29. var makeSeriesEncodeForNameBased = _sourceHelper.makeSeriesEncodeForNameBased;
  30. var LegendVisualProvider = require("../../visual/LegendVisualProvider");
  31. /*
  32. * Licensed to the Apache Software Foundation (ASF) under one
  33. * or more contributor license agreements. See the NOTICE file
  34. * distributed with this work for additional information
  35. * regarding copyright ownership. The ASF licenses this file
  36. * to you under the Apache License, Version 2.0 (the
  37. * "License"); you may not use this file except in compliance
  38. * with the License. You may obtain a copy of the License at
  39. *
  40. * http://www.apache.org/licenses/LICENSE-2.0
  41. *
  42. * Unless required by applicable law or agreed to in writing,
  43. * software distributed under the License is distributed on an
  44. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  45. * KIND, either express or implied. See the License for the
  46. * specific language governing permissions and limitations
  47. * under the License.
  48. */
  49. var PieSeries = echarts.extendSeriesModel({
  50. type: 'series.pie',
  51. // Overwrite
  52. init: function (option) {
  53. PieSeries.superApply(this, 'init', arguments); // Enable legend selection for each data item
  54. // Use a function instead of direct access because data reference may changed
  55. this.legendVisualProvider = new LegendVisualProvider(zrUtil.bind(this.getData, this), zrUtil.bind(this.getRawData, this));
  56. this.updateSelectedMap(this._createSelectableList());
  57. this._defaultLabelLine(option);
  58. },
  59. // Overwrite
  60. mergeOption: function (newOption) {
  61. PieSeries.superCall(this, 'mergeOption', newOption);
  62. this.updateSelectedMap(this._createSelectableList());
  63. },
  64. getInitialData: function (option, ecModel) {
  65. return createListSimply(this, {
  66. coordDimensions: ['value'],
  67. encodeDefaulter: zrUtil.curry(makeSeriesEncodeForNameBased, this)
  68. });
  69. },
  70. _createSelectableList: function () {
  71. var data = this.getRawData();
  72. var valueDim = data.mapDimension('value');
  73. var targetList = [];
  74. for (var i = 0, len = data.count(); i < len; i++) {
  75. targetList.push({
  76. name: data.getName(i),
  77. value: data.get(valueDim, i),
  78. selected: retrieveRawAttr(data, i, 'selected')
  79. });
  80. }
  81. return targetList;
  82. },
  83. // Overwrite
  84. getDataParams: function (dataIndex) {
  85. var data = this.getData();
  86. var params = PieSeries.superCall(this, 'getDataParams', dataIndex); // FIXME toFixed?
  87. var valueList = [];
  88. data.each(data.mapDimension('value'), function (value) {
  89. valueList.push(value);
  90. });
  91. params.percent = getPercentWithPrecision(valueList, dataIndex, data.hostModel.get('percentPrecision'));
  92. params.$vars.push('percent');
  93. return params;
  94. },
  95. _defaultLabelLine: function (option) {
  96. // Extend labelLine emphasis
  97. modelUtil.defaultEmphasis(option, 'labelLine', ['show']);
  98. var labelLineNormalOpt = option.labelLine;
  99. var labelLineEmphasisOpt = option.emphasis.labelLine; // Not show label line if `label.normal.show = false`
  100. labelLineNormalOpt.show = labelLineNormalOpt.show && option.label.show;
  101. labelLineEmphasisOpt.show = labelLineEmphasisOpt.show && option.emphasis.label.show;
  102. },
  103. defaultOption: {
  104. zlevel: 0,
  105. z: 2,
  106. legendHoverLink: true,
  107. hoverAnimation: true,
  108. // 默认全局居中
  109. center: ['50%', '50%'],
  110. radius: [0, '75%'],
  111. // 默认顺时针
  112. clockwise: true,
  113. startAngle: 90,
  114. // 最小角度改为0
  115. minAngle: 0,
  116. // If the angle of a sector less than `minShowLabelAngle`,
  117. // the label will not be displayed.
  118. minShowLabelAngle: 0,
  119. // 选中时扇区偏移量
  120. selectedOffset: 10,
  121. // 高亮扇区偏移量
  122. hoverOffset: 10,
  123. // If use strategy to avoid label overlapping
  124. avoidLabelOverlap: true,
  125. // 选择模式,默认关闭,可选single,multiple
  126. // selectedMode: false,
  127. // 南丁格尔玫瑰图模式,'radius'(半径) | 'area'(面积)
  128. // roseType: null,
  129. percentPrecision: 2,
  130. // If still show when all data zero.
  131. stillShowZeroSum: true,
  132. // cursor: null,
  133. left: 0,
  134. top: 0,
  135. right: 0,
  136. bottom: 0,
  137. width: null,
  138. height: null,
  139. label: {
  140. // If rotate around circle
  141. rotate: false,
  142. show: true,
  143. // 'outer', 'inside', 'center'
  144. position: 'outer',
  145. // 'none', 'labelLine', 'edge'. Works only when position is 'outer'
  146. alignTo: 'none',
  147. // Closest distance between label and chart edge.
  148. // Works only position is 'outer' and alignTo is 'edge'.
  149. margin: '25%',
  150. // Works only position is 'outer' and alignTo is not 'edge'.
  151. bleedMargin: 10,
  152. // Distance between text and label line.
  153. distanceToLabelLine: 5 // formatter: 标签文本格式器,同Tooltip.formatter,不支持异步回调
  154. // 默认使用全局文本样式,详见TEXTSTYLE
  155. // distance: 当position为inner时有效,为label位置到圆心的距离与圆半径(环状图为内外半径和)的比例系数
  156. },
  157. // Enabled when label.normal.position is 'outer'
  158. labelLine: {
  159. show: true,
  160. // 引导线两段中的第一段长度
  161. length: 15,
  162. // 引导线两段中的第二段长度
  163. length2: 15,
  164. smooth: false,
  165. lineStyle: {
  166. // color: 各异,
  167. width: 1,
  168. type: 'solid'
  169. }
  170. },
  171. itemStyle: {
  172. borderWidth: 1
  173. },
  174. // Animation type. Valid values: expansion, scale
  175. animationType: 'expansion',
  176. // Animation type when update. Valid values: transition, expansion
  177. animationTypeUpdate: 'transition',
  178. animationEasing: 'cubicOut'
  179. }
  180. });
  181. zrUtil.mixin(PieSeries, dataSelectableMixin);
  182. var _default = PieSeries;
  183. module.exports = _default;