pieLayout.js 6.5 KB

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