recharts
Version:
React charts
378 lines (316 loc) • 13.4 kB
JavaScript
'use strict';
var _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; };
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _class, _class2, _temp; /**
* @fileOverview Render sectors of a pie
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _pureRenderDecorator = require('pure-render-decorator');
var _pureRenderDecorator2 = _interopRequireDefault(_pureRenderDecorator);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _Layer = require('../container/Layer');
var _Layer2 = _interopRequireDefault(_Layer);
var _Sector = require('../shape/Sector');
var _Sector2 = _interopRequireDefault(_Sector);
var _Curve = require('../shape/Curve');
var _Curve2 = _interopRequireDefault(_Curve);
var _reactSmooth = require('react-smooth');
var _reactSmooth2 = _interopRequireDefault(_reactSmooth);
var _ReactUtils = require('../util/ReactUtils');
var _ReactUtils2 = _interopRequireDefault(_ReactUtils);
var _LodashUtils = require('../util/LodashUtils');
var _LodashUtils2 = _interopRequireDefault(_LodashUtils);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var RADIAN = Math.PI / 180;
var Pie = (0, _pureRenderDecorator2.default)(_class = (_temp = _class2 = function (_Component) {
_inherits(Pie, _Component);
function Pie(props, ctx) {
_classCallCheck(this, Pie);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Pie).call(this, props, ctx));
_this.state = {
isAnimationFinished: false
};
if (!_this.id) {
_this.id = 'clipPath' + Date.now();
}
return _this;
}
_createClass(Pie, [{
key: 'getDeltaAngle',
value: function getDeltaAngle() {
var _props = this.props;
var startAngle = _props.startAngle;
var endAngle = _props.endAngle;
var sign = Math.sign(endAngle - startAngle);
var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 360);
return sign * deltaAngle;
}
}, {
key: 'getSectors',
value: function getSectors() {
var _props2 = this.props;
var cx = _props2.cx;
var cy = _props2.cy;
var innerRadius = _props2.innerRadius;
var outerRadius = _props2.outerRadius;
var startAngle = _props2.startAngle;
var data = _props2.data;
var minAngle = _props2.minAngle;
var endAngle = _props2.endAngle;
var valueKey = _props2.valueKey;
var len = data.length;
var deltaAngle = this.getDeltaAngle();
var absDeltaAngle = Math.abs(deltaAngle);
var sum = data.reduce(function (result, entry) {
return result + entry[valueKey];
}, 0);
var sectors = [];
var prev = undefined;
if (sum > 0) {
sectors = data.map(function (entry, i) {
var percent = entry[valueKey] / sum;
var _startAngle = undefined;
var _endAngle = undefined;
if (i) {
_startAngle = deltaAngle < 0 ? prev.endAngle : prev.startAngle;
} else {
_startAngle = startAngle;
}
_endAngle = _startAngle + Math.sign(deltaAngle) * (minAngle + percent * (absDeltaAngle - len * minAngle));
prev = _extends({}, entry, {
cx: cx,
cy: cy,
innerRadius: innerRadius,
outerRadius: outerRadius,
startAngle: deltaAngle < 0 ? _startAngle : _endAngle,
endAngle: deltaAngle < 0 ? _endAngle : _startAngle,
payload: entry
});
return prev;
});
}
return sectors;
}
}, {
key: 'getTextAnchor',
value: function getTextAnchor(x, cx) {
if (x > cx) {
return 'start';
} else if (x < cx) {
return 'end';
}
return 'middle';
}
}, {
key: 'handleAnimationEnd',
value: function handleAnimationEnd() {
this.setState({
isAnimationFinished: true
});
}
}, {
key: 'handleSectorEnter',
value: function handleSectorEnter(data, e) {
this.props.onMouseEnter(data, e);
}
}, {
key: 'renderClipPath',
value: function renderClipPath() {
var _props3 = this.props;
var cx = _props3.cx;
var cy = _props3.cy;
var outerRadius = _props3.outerRadius;
var innerRadius = _props3.innerRadius;
var startAngle = _props3.startAngle;
var isAnimationActive = _props3.isAnimationActive;
var animationDuration = _props3.animationDuration;
var animationEasing = _props3.animationEasing;
var animationBegin = _props3.animationBegin;
return _react2.default.createElement(
'defs',
null,
_react2.default.createElement(
'clipPath',
{ id: this.id },
_react2.default.createElement(
_reactSmooth2.default,
{ easing: animationEasing,
isActive: isAnimationActive,
duration: animationDuration,
animationBegin: animationBegin,
onAnimationEnd: this.handleAnimationEnd.bind(this),
from: { endAngle: startAngle },
to: { endAngle: this.props.endAngle }
},
function (_ref) {
var endAngle = _ref.endAngle;
return _react2.default.createElement(_Sector2.default, { cx: cx,
cy: cy,
outerRadius: outerRadius,
innerRadius: innerRadius,
startAngle: startAngle,
endAngle: endAngle
});
}
)
)
);
}
}, {
key: 'renderLabels',
value: function renderLabels(sectors) {
var _this2 = this;
var isAnimationActive = this.props.isAnimationActive;
if (isAnimationActive && !this.state.isAnimationFinished) {
return null;
}
var _props4 = this.props;
var label = _props4.label;
var valueKey = _props4.valueKey;
var pieProps = _ReactUtils2.default.getPresentationAttributes(this.props);
var customLabelProps = _ReactUtils2.default.getPresentationAttributes(label);
var isLabelElement = _react2.default.isValidElement(label);
var offsetRadius = customLabelProps && customLabelProps.offsetRadius || 20;
var labels = sectors.map(function (entry, i) {
var midAngle = -RADIAN * (entry.startAngle + entry.endAngle) / 2;
var x = entry.cx + (entry.outerRadius + offsetRadius) * Math.cos(midAngle);
var y = entry.cy + (entry.outerRadius + offsetRadius) * Math.sin(midAngle);
var labelProps = _extends({}, pieProps, entry, {
stroke: 'none'
}, customLabelProps, {
index: i,
textAnchor: _this2.getTextAnchor(x, entry.cx),
x: x, y: y
});
var lineProps = _extends({}, pieProps, entry, {
fill: 'none',
stroke: entry.fill
}, customLabelProps, {
points: [{
x: entry.cx + entry.outerRadius * Math.cos(midAngle),
y: entry.cy + entry.outerRadius * Math.sin(midAngle)
}, { x: x, y: y }]
});
return isLabelElement ? _react2.default.cloneElement(label, { labelProps: labelProps, key: 'label-' + i }) : _react2.default.createElement(
'g',
{ key: 'label-' + i },
_react2.default.createElement(_Curve2.default, _extends({}, lineProps, { type: 'linear', className: 'recharts-pie-label-line' })),
_react2.default.createElement(
'text',
_extends({}, labelProps, { className: 'recharts-pie-label-text' }),
entry[valueKey]
)
);
});
return _react2.default.createElement(
_Layer2.default,
{ className: 'recharts-pie-labels' },
labels
);
}
}, {
key: 'renderSectors',
value: function renderSectors(sectors) {
var _this3 = this;
var _props5 = this.props;
var onMouseLeave = _props5.onMouseLeave;
var onClick = _props5.onClick;
return sectors.map(function (entry, i) {
return _react2.default.createElement(_Sector2.default, _extends({}, entry, {
className: 'recharts-pie-sector',
onMouseEnter: _this3.handleSectorEnter.bind(_this3, entry),
onMouseLeave: onMouseLeave,
onClick: onClick,
key: 'sector-' + i
}));
});
}
}, {
key: 'render',
value: function render() {
var _props6 = this.props;
var data = _props6.data;
var className = _props6.className;
var label = _props6.label;
var cx = _props6.cx;
var cy = _props6.cy;
var innerRadius = _props6.innerRadius;
var outerRadius = _props6.outerRadius;
if (!data || !data.length || !_LodashUtils2.default.isNumber(cx) || !_LodashUtils2.default.isNumber(cy) || !_LodashUtils2.default.isNumber(innerRadius) || !_LodashUtils2.default.isNumber(outerRadius)) {
return null;
}
var sectors = this.getSectors();
var layerClass = (0, _classnames2.default)('recharts-pie', className);
return _react2.default.createElement(
_Layer2.default,
{ className: layerClass },
this.renderClipPath(),
_react2.default.createElement(
'g',
{ clipPath: 'url(#' + this.id + ')' },
this.renderSectors(sectors)
),
label && this.renderLabels(sectors)
);
}
}]);
return Pie;
}(_react.Component), _class2.displayName = 'Pie', _class2.propTypes = _extends({}, _ReactUtils.PRESENTATION_ATTRIBUTES, {
className: _react.PropTypes.string,
cx: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
cy: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
startAngle: _react.PropTypes.number,
endAngle: _react.PropTypes.number,
innerRadius: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
outerRadius: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
nameKey: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
valueKey: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
data: _react.PropTypes.arrayOf(_react.PropTypes.object),
minAngle: _react.PropTypes.number,
legendType: _react.PropTypes.string,
label: _react.PropTypes.oneOfType([_react.PropTypes.object, _react.PropTypes.element, _react.PropTypes.bool]),
onMouseEnter: _react.PropTypes.func,
onMouseLeave: _react.PropTypes.func,
onClick: _react.PropTypes.func,
isAnimationActive: _react.PropTypes.bool,
animationBegin: _react.PropTypes.number,
animationDuration: _react.PropTypes.number,
animationEasing: _react.PropTypes.oneOfType([_react.PropTypes.func, _react.PropTypes.string])
}), _class2.defaultProps = {
stroke: '#fff',
fill: '#808080',
legendType: 'rect',
// The abscissa of pole
cx: 0,
// The ordinate of pole
cy: 0,
// The start angle of first sector
startAngle: 0,
// The direction of drawing sectors
endAngle: 360,
// The inner radius of sectors
innerRadius: 0,
// The outer radius of sectors
outerRadius: 0,
nameKey: 'name',
valueKey: 'value',
data: [],
minAngle: 0,
onMouseEnter: function onMouseEnter() {},
onMouseLeave: function onMouseLeave() {},
onClick: function onClick() {},
onAnimationEnd: function onAnimationEnd() {},
isAnimationActive: true,
animationBegin: 400,
animationDuration: 1500,
animationEasing: 'ease'
}, _temp)) || _class;
exports.default = Pie;