pieLayout.js 8.2 KB

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