Sector.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Sector = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. var _clsx = _interopRequireDefault(require("clsx"));
  8. var _ReactUtils = require("../util/ReactUtils");
  9. var _PolarUtils = require("../util/PolarUtils");
  10. var _DataUtils = require("../util/DataUtils");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  12. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  13. function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  14. function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
  15. function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
  16. function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  17. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  18. function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /**
  19. * @fileOverview Sector
  20. */
  21. var getDeltaAngle = function getDeltaAngle(startAngle, endAngle) {
  22. var sign = (0, _DataUtils.mathSign)(endAngle - startAngle);
  23. var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 359.999);
  24. return sign * deltaAngle;
  25. };
  26. var getTangentCircle = function getTangentCircle(_ref) {
  27. var cx = _ref.cx,
  28. cy = _ref.cy,
  29. radius = _ref.radius,
  30. angle = _ref.angle,
  31. sign = _ref.sign,
  32. isExternal = _ref.isExternal,
  33. cornerRadius = _ref.cornerRadius,
  34. cornerIsExternal = _ref.cornerIsExternal;
  35. var centerRadius = cornerRadius * (isExternal ? 1 : -1) + radius;
  36. var theta = Math.asin(cornerRadius / centerRadius) / _PolarUtils.RADIAN;
  37. var centerAngle = cornerIsExternal ? angle : angle + sign * theta;
  38. var center = (0, _PolarUtils.polarToCartesian)(cx, cy, centerRadius, centerAngle);
  39. // The coordinate of point which is tangent to the circle
  40. var circleTangency = (0, _PolarUtils.polarToCartesian)(cx, cy, radius, centerAngle);
  41. // The coordinate of point which is tangent to the radius line
  42. var lineTangencyAngle = cornerIsExternal ? angle - sign * theta : angle;
  43. var lineTangency = (0, _PolarUtils.polarToCartesian)(cx, cy, centerRadius * Math.cos(theta * _PolarUtils.RADIAN), lineTangencyAngle);
  44. return {
  45. center: center,
  46. circleTangency: circleTangency,
  47. lineTangency: lineTangency,
  48. theta: theta
  49. };
  50. };
  51. var getSectorPath = function getSectorPath(_ref2) {
  52. var cx = _ref2.cx,
  53. cy = _ref2.cy,
  54. innerRadius = _ref2.innerRadius,
  55. outerRadius = _ref2.outerRadius,
  56. startAngle = _ref2.startAngle,
  57. endAngle = _ref2.endAngle;
  58. var angle = getDeltaAngle(startAngle, endAngle);
  59. // When the angle of sector equals to 360, star point and end point coincide
  60. var tempEndAngle = startAngle + angle;
  61. var outerStartPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, outerRadius, startAngle);
  62. var outerEndPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, outerRadius, tempEndAngle);
  63. var path = "M ".concat(outerStartPoint.x, ",").concat(outerStartPoint.y, "\n A ").concat(outerRadius, ",").concat(outerRadius, ",0,\n ").concat(+(Math.abs(angle) > 180), ",").concat(+(startAngle > tempEndAngle), ",\n ").concat(outerEndPoint.x, ",").concat(outerEndPoint.y, "\n ");
  64. if (innerRadius > 0) {
  65. var innerStartPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, innerRadius, startAngle);
  66. var innerEndPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, innerRadius, tempEndAngle);
  67. path += "L ".concat(innerEndPoint.x, ",").concat(innerEndPoint.y, "\n A ").concat(innerRadius, ",").concat(innerRadius, ",0,\n ").concat(+(Math.abs(angle) > 180), ",").concat(+(startAngle <= tempEndAngle), ",\n ").concat(innerStartPoint.x, ",").concat(innerStartPoint.y, " Z");
  68. } else {
  69. path += "L ".concat(cx, ",").concat(cy, " Z");
  70. }
  71. return path;
  72. };
  73. var getSectorWithCorner = function getSectorWithCorner(_ref3) {
  74. var cx = _ref3.cx,
  75. cy = _ref3.cy,
  76. innerRadius = _ref3.innerRadius,
  77. outerRadius = _ref3.outerRadius,
  78. cornerRadius = _ref3.cornerRadius,
  79. forceCornerRadius = _ref3.forceCornerRadius,
  80. cornerIsExternal = _ref3.cornerIsExternal,
  81. startAngle = _ref3.startAngle,
  82. endAngle = _ref3.endAngle;
  83. var sign = (0, _DataUtils.mathSign)(endAngle - startAngle);
  84. var _getTangentCircle = getTangentCircle({
  85. cx: cx,
  86. cy: cy,
  87. radius: outerRadius,
  88. angle: startAngle,
  89. sign: sign,
  90. cornerRadius: cornerRadius,
  91. cornerIsExternal: cornerIsExternal
  92. }),
  93. soct = _getTangentCircle.circleTangency,
  94. solt = _getTangentCircle.lineTangency,
  95. sot = _getTangentCircle.theta;
  96. var _getTangentCircle2 = getTangentCircle({
  97. cx: cx,
  98. cy: cy,
  99. radius: outerRadius,
  100. angle: endAngle,
  101. sign: -sign,
  102. cornerRadius: cornerRadius,
  103. cornerIsExternal: cornerIsExternal
  104. }),
  105. eoct = _getTangentCircle2.circleTangency,
  106. eolt = _getTangentCircle2.lineTangency,
  107. eot = _getTangentCircle2.theta;
  108. var outerArcAngle = cornerIsExternal ? Math.abs(startAngle - endAngle) : Math.abs(startAngle - endAngle) - sot - eot;
  109. if (outerArcAngle < 0) {
  110. if (forceCornerRadius) {
  111. return "M ".concat(solt.x, ",").concat(solt.y, "\n a").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,1,").concat(cornerRadius * 2, ",0\n a").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,1,").concat(-cornerRadius * 2, ",0\n ");
  112. }
  113. return getSectorPath({
  114. cx: cx,
  115. cy: cy,
  116. innerRadius: innerRadius,
  117. outerRadius: outerRadius,
  118. startAngle: startAngle,
  119. endAngle: endAngle
  120. });
  121. }
  122. var path = "M ".concat(solt.x, ",").concat(solt.y, "\n A").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,").concat(+(sign < 0), ",").concat(soct.x, ",").concat(soct.y, "\n A").concat(outerRadius, ",").concat(outerRadius, ",0,").concat(+(outerArcAngle > 180), ",").concat(+(sign < 0), ",").concat(eoct.x, ",").concat(eoct.y, "\n A").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,").concat(+(sign < 0), ",").concat(eolt.x, ",").concat(eolt.y, "\n ");
  123. if (innerRadius > 0) {
  124. var _getTangentCircle3 = getTangentCircle({
  125. cx: cx,
  126. cy: cy,
  127. radius: innerRadius,
  128. angle: startAngle,
  129. sign: sign,
  130. isExternal: true,
  131. cornerRadius: cornerRadius,
  132. cornerIsExternal: cornerIsExternal
  133. }),
  134. sict = _getTangentCircle3.circleTangency,
  135. silt = _getTangentCircle3.lineTangency,
  136. sit = _getTangentCircle3.theta;
  137. var _getTangentCircle4 = getTangentCircle({
  138. cx: cx,
  139. cy: cy,
  140. radius: innerRadius,
  141. angle: endAngle,
  142. sign: -sign,
  143. isExternal: true,
  144. cornerRadius: cornerRadius,
  145. cornerIsExternal: cornerIsExternal
  146. }),
  147. eict = _getTangentCircle4.circleTangency,
  148. eilt = _getTangentCircle4.lineTangency,
  149. eit = _getTangentCircle4.theta;
  150. var innerArcAngle = cornerIsExternal ? Math.abs(startAngle - endAngle) : Math.abs(startAngle - endAngle) - sit - eit;
  151. if (innerArcAngle < 0 && cornerRadius === 0) {
  152. return "".concat(path, "L").concat(cx, ",").concat(cy, "Z");
  153. }
  154. path += "L".concat(eilt.x, ",").concat(eilt.y, "\n A").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,").concat(+(sign < 0), ",").concat(eict.x, ",").concat(eict.y, "\n A").concat(innerRadius, ",").concat(innerRadius, ",0,").concat(+(innerArcAngle > 180), ",").concat(+(sign > 0), ",").concat(sict.x, ",").concat(sict.y, "\n A").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,").concat(+(sign < 0), ",").concat(silt.x, ",").concat(silt.y, "Z");
  155. } else {
  156. path += "L".concat(cx, ",").concat(cy, "Z");
  157. }
  158. return path;
  159. };
  160. var defaultProps = {
  161. cx: 0,
  162. cy: 0,
  163. innerRadius: 0,
  164. outerRadius: 0,
  165. startAngle: 0,
  166. endAngle: 0,
  167. cornerRadius: 0,
  168. forceCornerRadius: false,
  169. cornerIsExternal: false
  170. };
  171. var Sector = exports.Sector = function Sector(sectorProps) {
  172. var props = _objectSpread(_objectSpread({}, defaultProps), sectorProps);
  173. var cx = props.cx,
  174. cy = props.cy,
  175. innerRadius = props.innerRadius,
  176. outerRadius = props.outerRadius,
  177. cornerRadius = props.cornerRadius,
  178. forceCornerRadius = props.forceCornerRadius,
  179. cornerIsExternal = props.cornerIsExternal,
  180. startAngle = props.startAngle,
  181. endAngle = props.endAngle,
  182. className = props.className;
  183. if (outerRadius < innerRadius || startAngle === endAngle) {
  184. return null;
  185. }
  186. var layerClass = (0, _clsx["default"])('recharts-sector', className);
  187. var deltaRadius = outerRadius - innerRadius;
  188. var cr = (0, _DataUtils.getPercentValue)(cornerRadius, deltaRadius, 0, true);
  189. var path;
  190. if (cr > 0 && Math.abs(startAngle - endAngle) < 360) {
  191. path = getSectorWithCorner({
  192. cx: cx,
  193. cy: cy,
  194. innerRadius: innerRadius,
  195. outerRadius: outerRadius,
  196. cornerRadius: Math.min(cr, deltaRadius / 2),
  197. forceCornerRadius: forceCornerRadius,
  198. cornerIsExternal: cornerIsExternal,
  199. startAngle: startAngle,
  200. endAngle: endAngle
  201. });
  202. } else {
  203. path = getSectorPath({
  204. cx: cx,
  205. cy: cy,
  206. innerRadius: innerRadius,
  207. outerRadius: outerRadius,
  208. startAngle: startAngle,
  209. endAngle: endAngle
  210. });
  211. }
  212. return /*#__PURE__*/_react["default"].createElement("path", _extends({}, (0, _ReactUtils.filterProps)(props, true), {
  213. className: layerClass,
  214. d: path,
  215. role: "img"
  216. }));
  217. };