UNPKG

recharts

Version:
259 lines (251 loc) • 12.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultSectorProps = exports.Sector = void 0; var React = _interopRequireWildcard(require("react")); var _clsx = require("../util/clsx"); var _fillDefinitions = require("../fill/fillDefinitions"); var _PolarUtils = require("../util/PolarUtils"); var _DataUtils = require("../util/DataUtils"); var _resolveDefaultProps = require("../util/resolveDefaultProps"); var _svgPropertiesAndEvents = require("../util/svgPropertiesAndEvents"); var _round = require("../util/round"); var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7; function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _taggedTemplateLiteral(e, t) { return t || (t = e.slice(0)), Object.freeze(Object.defineProperties(e, { raw: { value: Object.freeze(t) } })); } var getDeltaAngle = (startAngle, endAngle) => { var sign = (0, _DataUtils.mathSign)(endAngle - startAngle); var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 359.999); return sign * deltaAngle; }; var getTangentCircle = _ref => { var cx = _ref.cx, cy = _ref.cy, radius = _ref.radius, angle = _ref.angle, sign = _ref.sign, isExternal = _ref.isExternal, cornerRadius = _ref.cornerRadius, cornerIsExternal = _ref.cornerIsExternal; var centerRadius = cornerRadius * (isExternal ? 1 : -1) + radius; var theta = Math.asin(cornerRadius / centerRadius) / _PolarUtils.RADIAN; var centerAngle = cornerIsExternal ? angle : angle + sign * theta; var center = (0, _PolarUtils.polarToCartesian)(cx, cy, centerRadius, centerAngle); // The coordinate of point which is tangent to the circle var circleTangency = (0, _PolarUtils.polarToCartesian)(cx, cy, radius, centerAngle); // The coordinate of point which is tangent to the radius line var lineTangencyAngle = cornerIsExternal ? angle - sign * theta : angle; var lineTangency = (0, _PolarUtils.polarToCartesian)(cx, cy, centerRadius * Math.cos(theta * _PolarUtils.RADIAN), lineTangencyAngle); return { center, circleTangency, lineTangency, theta }; }; var getSectorPath = _ref2 => { var cx = _ref2.cx, cy = _ref2.cy, innerRadius = _ref2.innerRadius, outerRadius = _ref2.outerRadius, startAngle = _ref2.startAngle, endAngle = _ref2.endAngle; var angle = getDeltaAngle(startAngle, endAngle); // When the angle of sector equals to 360, star point and end point coincide var tempEndAngle = startAngle + angle; var outerStartPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, outerRadius, startAngle); var outerEndPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, outerRadius, tempEndAngle); var path = (0, _round.roundTemplateLiteral)(_templateObject || (_templateObject = _taggedTemplateLiteral(["M ", ",", "\n A ", ",", ",0,\n ", ",", ",\n ", ",", "\n "])), outerStartPoint.x, outerStartPoint.y, outerRadius, outerRadius, +(Math.abs(angle) > 180), +(startAngle > tempEndAngle), outerEndPoint.x, outerEndPoint.y); if (innerRadius > 0) { var innerStartPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, innerRadius, startAngle); var innerEndPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, innerRadius, tempEndAngle); path += (0, _round.roundTemplateLiteral)(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["L ", ",", "\n A ", ",", ",0,\n ", ",", ",\n ", ",", " Z"])), innerEndPoint.x, innerEndPoint.y, innerRadius, innerRadius, +(Math.abs(angle) > 180), +(startAngle <= tempEndAngle), innerStartPoint.x, innerStartPoint.y); } else { path += (0, _round.roundTemplateLiteral)(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["L ", ",", " Z"])), cx, cy); } return path; }; /** * Returns the SVG path of a sector with rounded corners. * If the sector is too narrow for the full cornerRadius, the corners shrink to fit, unless forceCornerRadius is set. */ var getSectorWithCorner = _ref3 => { var cx = _ref3.cx, cy = _ref3.cy, innerRadius = _ref3.innerRadius, outerRadius = _ref3.outerRadius, cornerRadius = _ref3.cornerRadius, forceCornerRadius = _ref3.forceCornerRadius, cornerIsExternal = _ref3.cornerIsExternal, startAngle = _ref3.startAngle, endAngle = _ref3.endAngle; var sign = (0, _DataUtils.mathSign)(endAngle - startAngle); var deltaAngle = Math.abs(startAngle - endAngle); /* * In a narrow sector, the two corners of the same arc would overlap. * Instead of dropping the rounded corners, shrink them to the largest radius that still fits, like d3-shape does. * With α being half of the sector angle, a circle that touches both radial edges and the outer arc * has radius outerRadius * sin(α) / (1 + sin(α)), * and a circle that touches both radial edges and the inner arc has radius innerRadius * sin(α) / (1 - sin(α)). */ var shouldFitCorners = !forceCornerRadius && !cornerIsExternal && deltaAngle < 180; var sinHalfAngle = Math.sin(deltaAngle / 2 * _PolarUtils.RADIAN); var outerCornerRadius = shouldFitCorners ? Math.min(cornerRadius, outerRadius * sinHalfAngle / (1 + sinHalfAngle)) : cornerRadius; var innerCornerRadius = shouldFitCorners ? Math.min(cornerRadius, innerRadius * sinHalfAngle / (1 - sinHalfAngle)) : cornerRadius; var _getTangentCircle = getTangentCircle({ cx, cy, radius: outerRadius, angle: startAngle, sign, cornerRadius: outerCornerRadius, cornerIsExternal }), soct = _getTangentCircle.circleTangency, solt = _getTangentCircle.lineTangency, sot = _getTangentCircle.theta; var _getTangentCircle2 = getTangentCircle({ cx, cy, radius: outerRadius, angle: endAngle, sign: -sign, cornerRadius: outerCornerRadius, cornerIsExternal }), eoct = _getTangentCircle2.circleTangency, eolt = _getTangentCircle2.lineTangency, eot = _getTangentCircle2.theta; var outerArcAngle = cornerIsExternal ? Math.abs(startAngle - endAngle) : Math.abs(startAngle - endAngle) - sot - eot; /* * The corners only overlap here when forceCornerRadius is set, otherwise they have been shrunk to fit. * Floating point rounding can make outerArcAngle slightly negative when the corners fit exactly, * so a negative outerArcAngle alone must not drop the rounded corners. */ if (outerArcAngle < 0 && forceCornerRadius) { return (0, _round.roundTemplateLiteral)(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["M ", ",", "\n a", ",", ",0,0,1,", ",0\n a", ",", ",0,0,1,", ",0\n "])), solt.x, solt.y, cornerRadius, cornerRadius, cornerRadius * 2, cornerRadius, cornerRadius, -cornerRadius * 2); } var path = (0, _round.roundTemplateLiteral)(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["M ", ",", "\n A", ",", ",0,0,", ",", ",", "\n A", ",", ",0,", ",", ",", ",", "\n A", ",", ",0,0,", ",", ",", "\n "])), solt.x, solt.y, outerCornerRadius, outerCornerRadius, +(sign < 0), soct.x, soct.y, outerRadius, outerRadius, +(outerArcAngle > 180), +(sign < 0), eoct.x, eoct.y, outerCornerRadius, outerCornerRadius, +(sign < 0), eolt.x, eolt.y); if (innerRadius > 0) { var _getTangentCircle3 = getTangentCircle({ cx, cy, radius: innerRadius, angle: startAngle, sign, isExternal: true, cornerRadius: innerCornerRadius, cornerIsExternal }), sict = _getTangentCircle3.circleTangency, silt = _getTangentCircle3.lineTangency, sit = _getTangentCircle3.theta; var _getTangentCircle4 = getTangentCircle({ cx, cy, radius: innerRadius, angle: endAngle, sign: -sign, isExternal: true, cornerRadius: innerCornerRadius, cornerIsExternal }), eict = _getTangentCircle4.circleTangency, eilt = _getTangentCircle4.lineTangency, eit = _getTangentCircle4.theta; var innerArcAngle = cornerIsExternal ? Math.abs(startAngle - endAngle) : Math.abs(startAngle - endAngle) - sit - eit; if (innerArcAngle < 0 && cornerRadius === 0) { return "".concat(path, "L").concat(cx, ",").concat(cy, "Z"); } path += (0, _round.roundTemplateLiteral)(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["L", ",", "\n A", ",", ",0,0,", ",", ",", "\n A", ",", ",0,", ",", ",", ",", "\n A", ",", ",0,0,", ",", ",", "Z"])), eilt.x, eilt.y, innerCornerRadius, innerCornerRadius, +(sign < 0), eict.x, eict.y, innerRadius, innerRadius, +(innerArcAngle > 180), +(sign > 0), sict.x, sict.y, innerCornerRadius, innerCornerRadius, +(sign < 0), silt.x, silt.y); } else { path += (0, _round.roundTemplateLiteral)(_templateObject7 || (_templateObject7 = _taggedTemplateLiteral(["L", ",", "Z"])), cx, cy); } return path; }; /** * SVG cx, cy are `string | number | undefined`, but internally we use `number` so let's * override the types here. * @public */ /** * The props of the inner renderer, which gets the `fill` as a color or a `url(#…)` reference. */ var defaultSectorProps = exports.defaultSectorProps = { cx: 0, cy: 0, innerRadius: 0, outerRadius: 0, startAngle: 0, endAngle: 0, cornerRadius: 0, forceCornerRadius: false, cornerIsExternal: false }; var SectorImpl = sectorProps => { var props = (0, _resolveDefaultProps.resolveDefaultProps)(sectorProps, defaultSectorProps); var cx = props.cx, cy = props.cy, innerRadius = props.innerRadius, outerRadius = props.outerRadius, cornerRadius = props.cornerRadius, forceCornerRadius = props.forceCornerRadius, cornerIsExternal = props.cornerIsExternal, startAngle = props.startAngle, endAngle = props.endAngle, className = props.className; if (outerRadius < innerRadius || startAngle === endAngle) { return null; } var layerClass = (0, _clsx.clsx)('recharts-sector', className); var deltaRadius = outerRadius - innerRadius; var cr = (0, _DataUtils.getPercentValue)(cornerRadius, deltaRadius, 0, true); var path; if (cr > 0 && Math.abs(startAngle - endAngle) < 360) { path = getSectorWithCorner({ cx, cy, innerRadius, outerRadius, cornerRadius: Math.min(cr, deltaRadius / 2), forceCornerRadius, cornerIsExternal, startAngle, endAngle }); } else { path = getSectorPath({ cx, cy, innerRadius, outerRadius, startAngle, endAngle }); } return /*#__PURE__*/React.createElement("path", _extends({}, (0, _svgPropertiesAndEvents.svgPropertiesAndEvents)(props), { className: layerClass, d: path })); }; /** * Renders a sector (pie-slice or donut-segment shape) from center * coordinates, inner/outer radius, and start/end angles. Used by polar * components such as Pie, RadialBar, and PolarGrid, and as the radial * cursor shape shown by Tooltip on polar charts. * @public */ var Sector = props => { var fill = props.fill; // spreading all props first keeps `fill` in its place among the attributes return fill != null && typeof fill === 'object' ? /*#__PURE__*/React.createElement(_fillDefinitions.FillObjectPaint, { fill: fill }, paint => /*#__PURE__*/React.createElement(SectorImpl, _extends({}, props, { fill: paint }))) : /*#__PURE__*/React.createElement(SectorImpl, _extends({}, props, { fill: fill })); }; exports.Sector = Sector;