pieLayout.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 { linearMap } from '../../util/number.js';
  41. import { normalizeArcAngles } from 'zrender/lib/core/PathProxy.js';
  42. import { makeInner } from '../../util/model.js';
  43. import { getCircleLayout } from '../../util/layout.js';
  44. var PI2 = Math.PI * 2;
  45. var RADIAN = Math.PI / 180;
  46. export default function pieLayout(seriesType, ecModel, api) {
  47. ecModel.eachSeriesByType(seriesType, function (seriesModel) {
  48. var data = seriesModel.getData();
  49. var valueDim = data.mapDimension('value');
  50. var _a = getCircleLayout(seriesModel, api),
  51. cx = _a.cx,
  52. cy = _a.cy,
  53. r = _a.r,
  54. r0 = _a.r0,
  55. viewRect = _a.viewRect;
  56. var startAngle = -seriesModel.get('startAngle') * RADIAN;
  57. var endAngle = seriesModel.get('endAngle');
  58. var padAngle = seriesModel.get('padAngle') * RADIAN;
  59. endAngle = endAngle === 'auto' ? startAngle - PI2 : -endAngle * RADIAN;
  60. var minAngle = seriesModel.get('minAngle') * RADIAN;
  61. var minAndPadAngle = minAngle + padAngle;
  62. var validDataCount = 0;
  63. data.each(valueDim, function (value) {
  64. !isNaN(value) && validDataCount++;
  65. });
  66. var sum = data.getSum(valueDim);
  67. // Sum may be 0
  68. var unitRadian = Math.PI / (sum || validDataCount) * 2;
  69. var clockwise = seriesModel.get('clockwise');
  70. var roseType = seriesModel.get('roseType');
  71. var stillShowZeroSum = seriesModel.get('stillShowZeroSum');
  72. // [0...max]
  73. var extent = data.getDataExtent(valueDim);
  74. extent[0] = 0;
  75. var dir = clockwise ? 1 : -1;
  76. var angles = [startAngle, endAngle];
  77. var halfPadAngle = dir * padAngle / 2;
  78. normalizeArcAngles(angles, !clockwise);
  79. startAngle = angles[0], endAngle = angles[1];
  80. var layoutData = getSeriesLayoutData(seriesModel);
  81. layoutData.startAngle = startAngle;
  82. layoutData.endAngle = endAngle;
  83. layoutData.clockwise = clockwise;
  84. layoutData.cx = cx;
  85. layoutData.cy = cy;
  86. layoutData.r = r;
  87. layoutData.r0 = r0;
  88. var angleRange = Math.abs(endAngle - startAngle);
  89. // In the case some sector angle is smaller than minAngle
  90. var restAngle = angleRange;
  91. var valueSumLargerThanMinAngle = 0;
  92. var currentAngle = startAngle;
  93. // Requird by `pieLabelLayout`.
  94. data.setLayout({
  95. viewRect: viewRect,
  96. r: r
  97. });
  98. data.each(valueDim, function (value, idx) {
  99. var angle;
  100. if (isNaN(value)) {
  101. data.setItemLayout(idx, {
  102. angle: NaN,
  103. startAngle: NaN,
  104. endAngle: NaN,
  105. clockwise: clockwise,
  106. cx: cx,
  107. cy: cy,
  108. r0: r0,
  109. r: roseType ? NaN : r
  110. });
  111. return;
  112. }
  113. // FIXME 兼容 2.0 但是 roseType 是 area 的时候才是这样?
  114. if (roseType !== 'area') {
  115. angle = sum === 0 && stillShowZeroSum ? unitRadian : value * unitRadian;
  116. } else {
  117. angle = angleRange / validDataCount;
  118. }
  119. if (angle < minAndPadAngle) {
  120. angle = minAndPadAngle;
  121. restAngle -= minAndPadAngle;
  122. } else {
  123. valueSumLargerThanMinAngle += value;
  124. }
  125. var endAngle = currentAngle + dir * angle;
  126. // calculate display angle
  127. var actualStartAngle = 0;
  128. var actualEndAngle = 0;
  129. if (padAngle > angle) {
  130. actualStartAngle = currentAngle + dir * angle / 2;
  131. actualEndAngle = actualStartAngle;
  132. } else {
  133. actualStartAngle = currentAngle + halfPadAngle;
  134. actualEndAngle = endAngle - halfPadAngle;
  135. }
  136. data.setItemLayout(idx, {
  137. angle: angle,
  138. startAngle: actualStartAngle,
  139. endAngle: actualEndAngle,
  140. clockwise: clockwise,
  141. cx: cx,
  142. cy: cy,
  143. r0: r0,
  144. r: roseType ? linearMap(value, extent, [r0, r]) : r
  145. });
  146. currentAngle = endAngle;
  147. });
  148. // Some sector is constrained by minAngle and padAngle
  149. // Rest sectors needs recalculate angle
  150. if (restAngle < PI2 && validDataCount) {
  151. // Average the angle if rest angle is not enough after all angles is
  152. // Constrained by minAngle and padAngle
  153. if (restAngle <= 1e-3) {
  154. var angle_1 = angleRange / validDataCount;
  155. data.each(valueDim, function (value, idx) {
  156. if (!isNaN(value)) {
  157. var layout = data.getItemLayout(idx);
  158. layout.angle = angle_1;
  159. var actualStartAngle = 0;
  160. var actualEndAngle = 0;
  161. if (angle_1 < padAngle) {
  162. actualStartAngle = startAngle + dir * (idx + 1 / 2) * angle_1;
  163. actualEndAngle = actualStartAngle;
  164. } else {
  165. actualStartAngle = startAngle + dir * idx * angle_1 + halfPadAngle;
  166. actualEndAngle = startAngle + dir * (idx + 1) * angle_1 - halfPadAngle;
  167. }
  168. layout.startAngle = actualStartAngle;
  169. layout.endAngle = actualEndAngle;
  170. }
  171. });
  172. } else {
  173. unitRadian = restAngle / valueSumLargerThanMinAngle;
  174. currentAngle = startAngle;
  175. data.each(valueDim, function (value, idx) {
  176. if (!isNaN(value)) {
  177. var layout = data.getItemLayout(idx);
  178. var angle = layout.angle === minAndPadAngle ? minAndPadAngle : value * unitRadian;
  179. var actualStartAngle = 0;
  180. var actualEndAngle = 0;
  181. if (angle < padAngle) {
  182. actualStartAngle = currentAngle + dir * angle / 2;
  183. actualEndAngle = actualStartAngle;
  184. } else {
  185. actualStartAngle = currentAngle + halfPadAngle;
  186. actualEndAngle = currentAngle + dir * angle - halfPadAngle;
  187. }
  188. layout.startAngle = actualStartAngle;
  189. layout.endAngle = actualEndAngle;
  190. currentAngle += dir * angle;
  191. }
  192. });
  193. }
  194. }
  195. });
  196. }
  197. export var getSeriesLayoutData = makeInner();