recharts
Version:
React charts
316 lines (251 loc) • 12.1 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 a group of radial bar
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _Sector = require('../shape/Sector');
var _Sector2 = _interopRequireDefault(_Sector);
var _Layer = require('../container/Layer');
var _Layer2 = _interopRequireDefault(_Layer);
var _LodashUtils = require('../util/LodashUtils');
var _LodashUtils2 = _interopRequireDefault(_LodashUtils);
var _DOMUtils = require('../util/DOMUtils');
var _DOMUtils2 = _interopRequireDefault(_DOMUtils);
var _ReactUtils = require('../util/ReactUtils');
var _ReactUtils2 = _interopRequireDefault(_ReactUtils);
var _pureRenderDecorator = require('pure-render-decorator');
var _pureRenderDecorator2 = _interopRequireDefault(_pureRenderDecorator);
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 RADIAN = Math.PI / 180;
var RadialBar = (0, _pureRenderDecorator2.default)(_class = (_temp = _class2 = function (_Component) {
_inherits(RadialBar, _Component);
function RadialBar() {
_classCallCheck(this, RadialBar);
return _possibleConstructorReturn(this, Object.getPrototypeOf(RadialBar).apply(this, arguments));
}
_createClass(RadialBar, [{
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 startAngle = _props2.startAngle;
var endAngle = _props2.endAngle;
var data = _props2.data;
var minAngle = _props2.minAngle;
var maxAngle = _props2.maxAngle;
var maxValue = Math.max.apply(null, data.map(function (entry) {
return Math.abs(entry.value);
}));
var absMinAngle = Math.abs(minAngle);
var absMaxAngle = Math.abs(maxAngle);
var deltaAngle = this.getDeltaAngle();
var deltaSign = Math.sign(deltaAngle);
var gapAngle = Math.min(Math.abs(absMaxAngle - absMinAngle), 360);
var sectors = data.map(function (entry) {
var value = entry.value;
var _endAngle = maxValue === 0 ? startAngle : startAngle + Math.sign(value) * deltaSign * (absMinAngle + gapAngle * Math.abs(entry.value) / maxValue);
return _extends({}, entry, {
cx: cx, cy: cy,
startAngle: startAngle,
endAngle: _endAngle
});
});
return sectors;
}
}, {
key: 'getLabelPathArc',
value: function getLabelPathArc(data, labelContent, style) {
var label = this.props.label;
var labelProps = _react2.default.isValidElement(label) ? label.props : label;
var offsetRadius = labelProps.offsetRadius || 2;
var orientation = labelProps.orientation || 'inner';
var cx = data.cx;
var cy = data.cy;
var innerRadius = data.innerRadius;
var outerRadius = data.outerRadius;
var startAngle = data.startAngle;
var endAngle = data.endAngle;
var clockWise = this.getDeltaAngle() < 0;
var radius = clockWise ? innerRadius + offsetRadius : Math.max(outerRadius - offsetRadius, 0);
if (radius <= 0) {
return '';
}
var labelSize = _DOMUtils2.default.getStringSize(labelContent, style);
var deltaAngle = labelSize.width / (radius * RADIAN);
var _startAngle = undefined;
var _endAngle = undefined;
if (clockWise) {
_startAngle = orientation === 'inner' ? Math.min(endAngle + deltaAngle, startAngle) : endAngle;
_endAngle = _startAngle - deltaAngle;
} else {
_startAngle = orientation === 'inner' ? Math.max(endAngle - deltaAngle, startAngle) : endAngle;
_endAngle = _startAngle + deltaAngle;
}
return 'M' + (cx + radius * Math.cos(-RADIAN * _startAngle)) + '\n ' + (cy + radius * Math.sin(-RADIAN * _startAngle)) + '\n A' + radius + ',' + radius + ',0,\n ' + (deltaAngle >= 180 ? 1 : 0) + ',\n ' + (clockWise ? 1 : 0) + ',\n ' + (cx + radius * Math.cos(-RADIAN * _endAngle)) + ',\n ' + (cy + radius * Math.sin(-RADIAN * _endAngle));
}
}, {
key: 'renderSectors',
value: function renderSectors(sectors) {
var _props3 = this.props;
var className = _props3.className;
var shape = _props3.shape;
var data = _props3.data;
var baseProps = _ReactUtils2.default.getPresentationAttributes(this.props);
var isShapeElement = _react2.default.isValidElement(shape);
return sectors.map(function (entry, i) {
var value = entry.value;
var rest = _objectWithoutProperties(entry, ['value']);
var props = _extends({}, baseProps, rest, {
key: 'sector-' + i,
className: 'recharts-radial-bar-sector'
});
return isShapeElement ? _react2.default.cloneElement(shape, props) : _react2.default.createElement(_Sector2.default, props);
});
}
}, {
key: 'renderBackground',
value: function renderBackground(sectors) {
var _props4 = this.props;
var startAngle = _props4.startAngle;
var endAngle = _props4.endAngle;
var background = _props4.background;
var isBackgroundElement = _react2.default.isValidElement(background);
var backgroundProps = _ReactUtils2.default.getPresentationAttributes(background);
return sectors.map(function (entry, i) {
var value = entry.value;
var rest = _objectWithoutProperties(entry, ['value']);
var props = _extends({}, rest, {
fill: '#eee'
}, backgroundProps, {
startAngle: startAngle,
endAngle: endAngle,
index: i,
key: 'sector-' + i,
className: 'recharts-radial-bar-background-sector'
});
return isBackgroundElement ? _react2.default.cloneElement(background, props) : _react2.default.createElement(_Sector2.default, props);
});
}
}, {
key: 'renderLabels',
value: function renderLabels(sectors) {
var _this2 = this;
var label = this.props.label;
var isElement = _react2.default.isValidElement(label);
var formatter = isElement ? label.props.formatter : label.formatter;
var hasFormatter = _LodashUtils2.default.isFunction(formatter);
return sectors.map(function (entry, i) {
var content = hasFormatter ? formatter(entry.value) : entry.value;
var id = _LodashUtils2.default.getUniqueId('recharts-defs-');
var style = _ReactUtils2.default.getPresentationAttributes(label) || { fontSize: 10, fill: '#000' };
var path = _this2.getLabelPathArc(entry, content, style);
return _react2.default.createElement(
'text',
_extends({}, style, { key: 'label-' + i, className: 'recharts-radial-bar-label' }),
_react2.default.createElement(
'defs',
null,
_react2.default.createElement('path', { id: id, d: path })
),
_react2.default.createElement(
'textPath',
{ xlinkHref: '#' + id },
content
)
);
});
}
}, {
key: 'render',
value: function render() {
var _props5 = this.props;
var data = _props5.data;
var className = _props5.className;
var background = _props5.background;
var label = _props5.label;
if (!data || !data.length) {
return null;
}
var sectors = this.getSectors();
var layerClass = (0, _classnames2.default)('recharts-area', className);
return _react2.default.createElement(
_Layer2.default,
{ className: layerClass },
background && _react2.default.createElement(
_Layer2.default,
{ className: 'recharts-radial-bar-background' },
this.renderBackground(sectors)
),
_react2.default.createElement(
_Layer2.default,
{ className: 'recharts-radial-bar-sectors' },
this.renderSectors(sectors)
),
label && _react2.default.createElement(
_Layer2.default,
{ className: 'recharts-radial-bar-labels' },
this.renderLabels(sectors)
)
);
}
}]);
return RadialBar;
}(_react.Component), _class2.displayName = 'RadialBar', _class2.propTypes = _extends({}, _ReactUtils.PRESENTATION_ATTRIBUTES, {
className: _react.PropTypes.string,
shape: _react.PropTypes.object,
cx: _react.PropTypes.number,
cy: _react.PropTypes.number,
startAngle: _react.PropTypes.number,
endAngle: _react.PropTypes.number,
maxAngle: _react.PropTypes.number,
minAngle: _react.PropTypes.number,
data: _react.PropTypes.arrayOf(_react.PropTypes.shape({
cx: _react.PropTypes.number,
cy: _react.PropTypes.number,
innerRadius: _react.PropTypes.number,
outerRadius: _react.PropTypes.number,
value: _react.PropTypes.value
})),
legendType: _react.PropTypes.string,
label: _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.element, _react.PropTypes.object]),
background: _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.object, _react.PropTypes.element]),
onMouseEnter: _react.PropTypes.func,
onMouseLeave: _react.PropTypes.func,
onClick: _react.PropTypes.func
}), _class2.defaultProps = {
startAngle: 180,
endAngle: 0,
maxAngle: 135,
minAngle: 0,
stroke: '#fff',
fill: '#808080',
legendType: 'rect',
data: [],
onClick: function onClick() {},
onMouseEnter: function onMouseEnter() {},
onMouseLeave: function onMouseLeave() {}
}, _temp)) || _class;
exports.default = RadialBar;