recharts
Version:
React charts
266 lines (209 loc) • 9.52 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, _temp2; /**
* @fileOverview Pie Chart
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _Surface = require('../container/Surface');
var _Surface2 = _interopRequireDefault(_Surface);
var _Legend = require('../component/Legend');
var _Legend2 = _interopRequireDefault(_Legend);
var _Tooltip = require('../component/Tooltip');
var _Tooltip2 = _interopRequireDefault(_Tooltip);
var _Pie = require('../polar/Pie');
var _Pie2 = _interopRequireDefault(_Pie);
var _ReactUtils = require('../util/ReactUtils');
var _ReactUtils2 = _interopRequireDefault(_ReactUtils);
var _LodashUtils = require('../util/LodashUtils');
var _LodashUtils2 = _interopRequireDefault(_LodashUtils);
var _PolarUtils = require('../util/PolarUtils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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 PieChart = (_temp2 = _class = function (_Component) {
_inherits(PieChart, _Component);
function PieChart() {
var _Object$getPrototypeO;
var _temp, _this, _ret;
_classCallCheck(this, PieChart);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(PieChart)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.state = {
activeTooltipLabel: '',
activeTooltipPosition: 'left-bottom',
activeTooltipCoord: { x: 0, y: 0 },
isTooltipActive: false
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(PieChart, [{
key: 'getComposedData',
value: function getComposedData(item) {
var _item$props = item.props;
var fill = _item$props.fill;
var stroke = _item$props.stroke;
var strokeWidth = _item$props.strokeWidth;
var strokeDasharray = _item$props.strokeDasharray;
var data = _item$props.data;
return data.map(function (entry) {
return _extends({
fill: fill, stroke: stroke, strokeWidth: strokeWidth, strokeDasharray: strokeDasharray
}, entry);
});
}
}, {
key: 'handleMouseEnter',
value: function handleMouseEnter(el, e) {
var _this2 = this;
this.setState({
isTooltipActive: true
}, function () {
if (_this2.props.onMouseEnter) {
_this2.props.onMouseEnter(el, e);
}
});
}
}, {
key: 'handleMouseLeave',
value: function handleMouseLeave(e) {
var _this3 = this;
this.setState({
isTooltipActive: false
}, function () {
if (_this3.props.onMouseEnter) {
_this3.props.onMouseLeave(e);
}
});
}
/**
* Draw legend
* @param {Array} items The instances of Pie
* @return {ReactElement} The instance of Legend
*/
}, {
key: 'renderLegend',
value: function renderLegend(items) {
var _this4 = this;
var children = this.props.children;
var legendItem = _ReactUtils2.default.findChildByType(children, _Legend2.default);
if (!legendItem) {
return null;
}
var _props = this.props;
var width = _props.width;
var height = _props.height;
var legendData = items.reduce(function (result, child) {
var nameKey = child.props.nameKey;
var data = _this4.getComposedData(child);
return result.concat(data.map(function (entry) {
var name = entry.name;
var value = entry.value;
var rest = _objectWithoutProperties(entry, ['name', 'value']);
return _extends({ value: entry[nameKey], color: entry.fill }, rest);
}));
}, []);
return _react2.default.cloneElement(legendItem, _extends({}, _Legend2.default.getWithHeight(legendItem, width, height), {
payload: legendData
}));
}
}, {
key: 'renderTooltip',
value: function renderTooltip() {
var children = this.props.children;
var tooltipItem = _ReactUtils2.default.findChildByType(children, _Tooltip2.default);
if (!tooltipItem) {
return;
}
}
/**
* Draw the main part of bar chart
* @param {Array} items All the instance of Pie
* @return {ReactComponent} All the instance of Pie
*/
}, {
key: 'renderItems',
value: function renderItems(items) {
var _this5 = this;
var _props2 = this.props;
var width = _props2.width;
var height = _props2.height;
var margin = _props2.margin;
return items.map(function (child, i) {
var _child$props = child.props;
var innerRadius = _child$props.innerRadius;
var outerRadius = _child$props.outerRadius;
var data = _child$props.data;
var cx = _LodashUtils2.default.getPercentValue(child.props.cx, width, width / 2);
var cy = _LodashUtils2.default.getPercentValue(child.props.cy, height, height / 2);
var maxRadius = (0, _PolarUtils.getMaxRadius)(width, height, cx, cy, margin);
return _react2.default.cloneElement(child, {
key: 'recharts-pie-' + i,
cx: cx,
cy: cy,
innerRadius: _LodashUtils2.default.getPercentValue(innerRadius, maxRadius, 0),
outerRadius: _LodashUtils2.default.getPercentValue(outerRadius, maxRadius, maxRadius * 0.8),
data: _this5.getComposedData(child),
onMouseEnter: _this5.handleMouseEnter.bind(_this5),
onMouseLeave: _this5.handleMouseLeave.bind(_this5)
});
});
}
}, {
key: 'render',
value: function render() {
if (!_ReactUtils2.default.validateWidthHeight(this)) {
return null;
}
var _props3 = this.props;
var style = _props3.style;
var children = _props3.children;
var className = _props3.className;
var width = _props3.width;
var height = _props3.height;
var items = _ReactUtils2.default.findAllByType(children, _Pie2.default);
return _react2.default.createElement(
'div',
{ className: (0, _classnames2.default)('recharts-wrapper', className),
style: _extends({ position: 'relative', cursor: 'default' }, style)
},
_react2.default.createElement(
_Surface2.default,
{ width: width, height: height },
this.renderItems(items)
),
this.renderLegend(items),
this.renderTooltip(items)
);
}
}]);
return PieChart;
}(_react.Component), _class.displayName = 'PieChart', _class.propTypes = {
width: _react.PropTypes.number,
height: _react.PropTypes.number,
margin: _react.PropTypes.shape({
top: _react.PropTypes.number,
right: _react.PropTypes.number,
bottom: _react.PropTypes.number,
left: _react.PropTypes.number
}),
title: _react.PropTypes.string,
style: _react.PropTypes.object,
children: _react.PropTypes.oneOfType([_react.PropTypes.arrayOf(_react.PropTypes.node), _react.PropTypes.node]),
className: _react.PropTypes.string,
onMouseEnter: _react.PropTypes.func,
onMouseLeave: _react.PropTypes.func,
onClick: _react.PropTypes.func
}, _class.defaultProps = {
style: {},
margin: { top: 0, right: 0, bottom: 0, left: 0 }
}, _temp2);
exports.default = PieChart;