pieLayout.js 6.1 KB

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