recharts
Version:
React charts
284 lines (240 loc) • 12.8 kB
JavaScript
var _class, _class2, _temp;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _extends() { _extends = Object.assign || 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); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
/**
* @fileOverview Sector
*/
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import pureRender from '../util/PureRender';
import { PRESENTATION_ATTRIBUTES, getPresentationAttributes, filterEventAttributes } from '../util/ReactUtils';
import { polarToCartesian, RADIAN } from '../util/PolarUtils';
import { getPercentValue, mathSign } from '../util/DataUtils';
var getDeltaAngle = function getDeltaAngle(startAngle, endAngle) {
var sign = mathSign(endAngle - startAngle);
var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 359.999);
return sign * deltaAngle;
};
var getTangentCircle = function 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) / RADIAN;
var centerAngle = cornerIsExternal ? angle : angle + sign * theta;
var center = polarToCartesian(cx, cy, centerRadius, centerAngle); // The coordinate of point which is tangent to the circle
var circleTangency = 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 = polarToCartesian(cx, cy, centerRadius * Math.cos(theta * RADIAN), lineTangencyAngle);
return {
center: center,
circleTangency: circleTangency,
lineTangency: lineTangency,
theta: theta
};
};
var getSectorPath = function 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 = polarToCartesian(cx, cy, outerRadius, startAngle);
var outerEndPoint = polarToCartesian(cx, cy, outerRadius, tempEndAngle);
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 ");
if (innerRadius > 0) {
var innerStartPoint = polarToCartesian(cx, cy, innerRadius, startAngle);
var innerEndPoint = polarToCartesian(cx, cy, innerRadius, tempEndAngle);
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");
} else {
path += "L ".concat(cx, ",").concat(cy, " Z");
}
return path;
};
var getSectorWithCorner = function 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 = mathSign(endAngle - startAngle);
var _getTangentCircle = getTangentCircle({
cx: cx,
cy: cy,
radius: outerRadius,
angle: startAngle,
sign: sign,
cornerRadius: cornerRadius,
cornerIsExternal: cornerIsExternal
}),
soct = _getTangentCircle.circleTangency,
solt = _getTangentCircle.lineTangency,
sot = _getTangentCircle.theta;
var _getTangentCircle2 = getTangentCircle({
cx: cx,
cy: cy,
radius: outerRadius,
angle: endAngle,
sign: -sign,
cornerRadius: cornerRadius,
cornerIsExternal: cornerIsExternal
}),
eoct = _getTangentCircle2.circleTangency,
eolt = _getTangentCircle2.lineTangency,
eot = _getTangentCircle2.theta;
var outerArcAngle = Math.abs(startAngle - endAngle) - sot - eot;
if (outerArcAngle < 0) {
if (forceCornerRadius) {
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 ");
}
return getSectorPath({
cx: cx,
cy: cy,
innerRadius: innerRadius,
outerRadius: outerRadius,
startAngle: startAngle,
endAngle: endAngle
});
}
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 ");
if (innerRadius > 0) {
var _getTangentCircle3 = getTangentCircle({
cx: cx,
cy: cy,
radius: innerRadius,
angle: startAngle,
sign: sign,
isExternal: true,
cornerRadius: cornerRadius,
cornerIsExternal: cornerIsExternal
}),
sict = _getTangentCircle3.circleTangency,
silt = _getTangentCircle3.lineTangency,
sit = _getTangentCircle3.theta;
var _getTangentCircle4 = getTangentCircle({
cx: cx,
cy: cy,
radius: innerRadius,
angle: endAngle,
sign: -sign,
isExternal: true,
cornerRadius: cornerRadius,
cornerIsExternal: cornerIsExternal
}),
eict = _getTangentCircle4.circleTangency,
eilt = _getTangentCircle4.lineTangency,
eit = _getTangentCircle4.theta;
var innerArcAngle = Math.abs(startAngle - endAngle) - sit - eit;
if (innerArcAngle < 0) {
return "".concat(path, "L").concat(cx, ",").concat(cy, "Z");
}
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");
} else {
path += "L".concat(cx, ",").concat(cy, "Z");
}
return path;
};
var Sector = pureRender(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
_inherits(Sector, _Component);
function Sector() {
_classCallCheck(this, Sector);
return _possibleConstructorReturn(this, _getPrototypeOf(Sector).apply(this, arguments));
}
_createClass(Sector, [{
key: "render",
value: function render() {
var _this$props = this.props,
cx = _this$props.cx,
cy = _this$props.cy,
innerRadius = _this$props.innerRadius,
outerRadius = _this$props.outerRadius,
cornerRadius = _this$props.cornerRadius,
forceCornerRadius = _this$props.forceCornerRadius,
cornerIsExternal = _this$props.cornerIsExternal,
startAngle = _this$props.startAngle,
endAngle = _this$props.endAngle,
className = _this$props.className;
if (outerRadius < innerRadius || startAngle === endAngle) {
return null;
}
var layerClass = classNames('recharts-sector', className);
var deltaRadius = outerRadius - innerRadius;
var cr = getPercentValue(cornerRadius, deltaRadius, 0, true);
var path;
if (cr > 0 && Math.abs(startAngle - endAngle) < 360) {
path = getSectorWithCorner({
cx: cx,
cy: cy,
innerRadius: innerRadius,
outerRadius: outerRadius,
cornerRadius: Math.min(cr, deltaRadius / 2),
forceCornerRadius: forceCornerRadius,
cornerIsExternal: cornerIsExternal,
startAngle: startAngle,
endAngle: endAngle
});
} else {
path = getSectorPath({
cx: cx,
cy: cy,
innerRadius: innerRadius,
outerRadius: outerRadius,
startAngle: startAngle,
endAngle: endAngle
});
}
return React.createElement("path", _extends({}, getPresentationAttributes(this.props), filterEventAttributes(this.props), {
className: layerClass,
d: path
}));
}
}]);
return Sector;
}(Component), _class2.displayName = 'Sector', _class2.propTypes = _objectSpread({}, PRESENTATION_ATTRIBUTES, {
className: PropTypes.string,
cx: PropTypes.number,
cy: PropTypes.number,
innerRadius: PropTypes.number,
outerRadius: PropTypes.number,
startAngle: PropTypes.number,
endAngle: PropTypes.number,
cornerRadius: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
forceCornerRadius: PropTypes.bool,
cornerIsExternal: PropTypes.bool
}), _class2.defaultProps = {
cx: 0,
cy: 0,
innerRadius: 0,
outerRadius: 0,
startAngle: 0,
endAngle: 0,
cornerRadius: 0,
forceCornerRadius: false,
cornerIsExternal: false
}, _temp)) || _class;
export default Sector;