recharts
Version:
React charts
215 lines (175 loc) • 7.92 kB
JavaScript
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 The axis of polar coordinate system
*/
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 _Layer = require('../container/Layer');
var _Layer2 = _interopRequireDefault(_Layer);
var _ReactUtils = require('../util/ReactUtils');
var _ReactUtils2 = _interopRequireDefault(_ReactUtils);
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 PolarRadiusAxis = (0, _pureRenderDecorator2.default)(_class = (_temp = _class2 = function (_Component) {
_inherits(PolarRadiusAxis, _Component);
function PolarRadiusAxis() {
_classCallCheck(this, PolarRadiusAxis);
return _possibleConstructorReturn(this, Object.getPrototypeOf(PolarRadiusAxis).apply(this, arguments));
}
_createClass(PolarRadiusAxis, [{
key: 'getTickValueCoord',
/**
* Calculate the coordinate of tick
* @param {Object} radius The data of a simple tick
* @return {Object} (x, y)
*/
value: function getTickValueCoord(_ref) {
var radius = _ref.radius;
var _props = this.props;
var angle = _props.angle;
var cx = _props.cx;
var cy = _props.cy;
var sin = Math.sin(-angle * RADIAN);
var cos = Math.cos(-angle * RADIAN);
return {
x: cx + radius * cos,
y: cy + radius * sin
};
}
}, {
key: 'getTickTextAnchor',
value: function getTickTextAnchor() {
var orientation = this.props.orientation;
var textAnchor = undefined;
switch (orientation) {
case 'left':
textAnchor = 'end';
break;
case 'right':
textAnchor = 'start';
break;
default:
textAnchor = 'middle';
break;
}
return textAnchor;
}
}, {
key: 'renderAxisLine',
value: function renderAxisLine() {
var _props2 = this.props;
var cx = _props2.cx;
var cy = _props2.cy;
var angle = _props2.angle;
var ticks = _props2.ticks;
var axisLine = _props2.axisLine;
var extent = ticks.reduce(function (result, entry) {
return [Math.min(result[0], entry.radius), Math.max(result[1], entry.radius)];
}, [Infinity, -Infinity]);
var sin = Math.sin(-angle * RADIAN);
var cos = Math.cos(-angle * RADIAN);
var props = _extends({}, _ReactUtils2.default.getPresentationAttributes(this.props), {
fill: 'none'
}, _ReactUtils2.default.getPresentationAttributes(axisLine), {
x1: cx + extent[0] * cos,
y1: cy + extent[0] * sin,
x2: cx + extent[1] * cos,
y2: cy + extent[1] * sin
});
return _react2.default.createElement('line', _extends({ className: 'recharts-polar-radius-axis-line' }, props));
}
}, {
key: 'renderTicks',
value: function renderTicks() {
var _this2 = this;
var _props3 = this.props;
var ticks = _props3.ticks;
var label = _props3.label;
var angle = _props3.angle;
var tickFormatter = _props3.tickFormatter;
var stroke = _props3.stroke;
var textAnchor = this.getTickTextAnchor();
var axisProps = _ReactUtils2.default.getPresentationAttributes(this.props);
var customLabelProps = _ReactUtils2.default.getPresentationAttributes(label);
var isLabelElement = _react2.default.isValidElement(label);
var items = ticks.map(function (entry, i) {
var coord = _this2.getTickValueCoord(entry);
var labelProps = _extends({
textAnchor: textAnchor,
transform: 'rotate(' + (90 - angle) + ', ' + coord.x + ', ' + coord.y + ')'
}, axisProps, {
stroke: 'none', fill: stroke
}, customLabelProps, {
index: i, payload: entry
}, coord);
return _react2.default.createElement(
'g',
{ className: 'recharts-polar-radius-axis-tick', key: 'tick-' + i },
isLabelElement ? _react2.default.cloneElement(label, labelProps) : _react2.default.createElement(
'text',
_extends({}, labelProps, { className: 'recharts-polar-radius-axis-tick-value' }),
tickFormatter ? tickFormatter(entry.value) : entry.value
)
);
});
return _react2.default.createElement(
'g',
{ className: 'recharts-polar-radius-axis-ticks' },
items
);
}
}, {
key: 'render',
value: function render() {
var _props4 = this.props;
var ticks = _props4.ticks;
var axisLine = _props4.axisLine;
var label = _props4.label;
if (!ticks || !ticks.length) {
return null;
}
return _react2.default.createElement(
'g',
{ className: 'recharts-polar-radius-axis' },
axisLine && this.renderAxisLine(),
label && this.renderTicks()
);
}
}]);
return PolarRadiusAxis;
}(_react.Component), _class2.displayName = 'PolarRadiusAxis', _class2.propTypes = _extends({}, _ReactUtils.PRESENTATION_ATTRIBUTES, {
cx: _react.PropTypes.number,
cy: _react.PropTypes.number,
hide: _react.PropTypes.bool,
angle: _react.PropTypes.number,
tickCount: _react.PropTypes.number,
ticks: _react.PropTypes.arrayOf(_react.PropTypes.shape({
value: _react.PropTypes.any,
radius: _react.PropTypes.value
})),
orientation: _react.PropTypes.oneOf(['left', 'right', 'middle']),
axisLine: _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.object]),
label: _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.object, _react.PropTypes.element]),
tickFormatter: _react.PropTypes.func,
domain: _react.PropTypes.array
}), _class2.defaultProps = {
cx: 0,
cy: 0,
angle: 0,
orientation: 'right',
stroke: '#ccc',
axisLine: true,
label: true,
tickCount: 5
}, _temp)) || _class;
exports.default = PolarRadiusAxis;
;