UNPKG

recharts

Version:
182 lines (147 loc) 6.57 kB
'use strict'; 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 Polar Grid */ Object.defineProperty(exports, "__esModule", { value: true }); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _pureRenderDecorator = require('pure-render-decorator'); var _pureRenderDecorator2 = _interopRequireDefault(_pureRenderDecorator); 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 PolarGrid = (0, _pureRenderDecorator2.default)(_class = (_temp = _class2 = function (_Component) { _inherits(PolarGrid, _Component); function PolarGrid() { _classCallCheck(this, PolarGrid); return _possibleConstructorReturn(this, Object.getPrototypeOf(PolarGrid).apply(this, arguments)); } _createClass(PolarGrid, [{ key: 'renderPolarAngles', /** * Draw axis of radial line * @return {[type]} The lines */ value: function renderPolarAngles() { var _props = this.props; var cx = _props.cx; var cy = _props.cy; var innerRadius = _props.innerRadius; var outerRadius = _props.outerRadius; var polarAngles = _props.polarAngles; if (!polarAngles || !polarAngles.length) { return null; } return _react2.default.createElement( 'g', { className: 'recharts-polar-grid-angle' }, polarAngles.map(function (entry, i) { var cos = Math.cos(-entry * RADIAN); var sin = Math.sin(-entry * RADIAN); return _react2.default.createElement('line', { key: 'line-' + i, stroke: '#ccc', x1: cx + cos * innerRadius, y1: cy + sin * innerRadius, x2: cx + cos * outerRadius, y2: cy + sin * outerRadius }); }) ); } /** * Draw concentric circles * @param {Number} radius The radius of circle * @param {Number} index The index of circle * @return {ReactElement} circle */ }, { key: 'renderConcentricCircle', value: function renderConcentricCircle(radius, index) { var _props2 = this.props; var cx = _props2.cx; var cy = _props2.cy; return _react2.default.createElement('circle', { className: 'recharts-polar-grid-concentric-circle', stroke: '#ccc', fill: 'none', key: 'circle-' + index, cx: cx, cy: cy, r: radius }); } /** * Draw concentric polygons * @param {Number} radius The radius of polygon * @param {Number} index The index of polygon * @return {ReactElement} polygon */ }, { key: 'renderConcentricPolygon', value: function renderConcentricPolygon(radius, index) { var _props3 = this.props; var cx = _props3.cx; var cy = _props3.cy; var polarAngles = _props3.polarAngles; var path = ''; polarAngles.forEach(function (angle, i) { if (i) { path += 'L ' + (cx + radius * Math.cos(-angle * RADIAN)) + ',' + (cy + radius * Math.sin(-angle * RADIAN)); } else { path += 'M ' + (cx + radius * Math.cos(-angle * RADIAN)) + ',' + (cy + radius * Math.sin(-angle * RADIAN)); } }); return _react2.default.createElement('path', { className: 'recharts-polar-grid-concentric-polygon', stroke: '#ccc', fill: 'none', key: 'path-' + index, d: path + 'Z' }); } /** * Draw concentric axis * @return {ReactElement} Concentric axis * @todo Optimize the name */ }, { key: 'renderConcentricPath', value: function renderConcentricPath() { var _this2 = this; var _props4 = this.props; var polarRadius = _props4.polarRadius; var gridType = _props4.gridType; if (!polarRadius || !polarRadius.length) { return null; } return _react2.default.createElement( 'g', { className: 'recharts-polar-grid-concentric' }, polarRadius.map(function (entry, i) { return gridType === 'circle' ? _this2.renderConcentricCircle(entry, i) : _this2.renderConcentricPolygon(entry, i); }) ); } }, { key: 'render', value: function render() { var outerRadius = this.props.outerRadius; if (outerRadius <= 0) { return null; } return _react2.default.createElement( 'g', { className: 'recharts-polar-grid' }, this.renderPolarAngles(), this.renderConcentricPath() ); } }]); return PolarGrid; }(_react.Component), _class2.displayName = 'PolarGrid', _class2.propTypes = { cx: _react.PropTypes.number, cy: _react.PropTypes.number, innerRadius: _react.PropTypes.number, outerRadius: _react.PropTypes.number, polarAngles: _react.PropTypes.arrayOf(_react.PropTypes.number), polarRadius: _react.PropTypes.arrayOf(_react.PropTypes.number), gridType: _react.PropTypes.oneOf(['polygon', 'circle']) }, _class2.defaultProps = { cx: 0, cy: 0, innerRadius: 0, outerRadius: 0, gridType: 'polygon' }, _temp)) || _class; exports.default = PolarGrid;