UNPKG

recharts

Version:
151 lines (117 loc) 6.82 kB
'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 Rectangle */ 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 _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 Rectangle = (0, _pureRenderDecorator2.default)(_class = (_temp = _class2 = function (_Component) { _inherits(Rectangle, _Component); function Rectangle() { _classCallCheck(this, Rectangle); return _possibleConstructorReturn(this, Object.getPrototypeOf(Rectangle).apply(this, arguments)); } _createClass(Rectangle, [{ key: 'getPath', value: function getPath(x, y, width, height, radius) { var maxRadius = Math.min(width / 2, height / 2); var newRadius = []; var path = undefined; if (maxRadius > 0 && radius instanceof Array) { for (var i = 0, len = 4; i < len; i++) { newRadius[i] = radius[i] > maxRadius ? maxRadius : radius[i]; } path = 'M' + x + ',' + (y + newRadius[0]); if (newRadius[0] > 0) { path += 'A ' + newRadius[0] + ',' + newRadius[0] + ',0,0,1,' + (x + newRadius[0]) + ',' + y; } path += 'L ' + (x + width - newRadius[1]) + ',' + y; if (newRadius[1] > 0) { path += 'A ' + newRadius[1] + ',' + newRadius[1] + ',0,0,1,' + (x + width) + ',' + (y + newRadius[1]); } path += 'L ' + (x + width) + ',' + (y + height - newRadius[2]); if (newRadius[2] > 0) { path += 'A ' + newRadius[2] + ',' + newRadius[2] + ',0,0,1,' + (x + width - newRadius[2]) + ',' + (y + height); } path += 'L ' + (x + newRadius[3]) + ',' + (y + height); if (newRadius[3] > 0) { path += 'A ' + newRadius[3] + ',' + newRadius[3] + ',0,0,1,' + x + ',' + (y + height - newRadius[3]); } path += 'Z'; } else if (maxRadius > 0 && radius === +radius && radius > 0) { newRadius = radius > maxRadius ? maxRadius : radius; path = 'M ' + x + ',' + (y + newRadius) + ' A ' + newRadius + ',' + newRadius + ',0,0,1,' + (x + newRadius) + ',' + y + ('L ' + (x + width - newRadius) + ',' + y + ' A ' + newRadius + ',' + newRadius + ',0,0,1,' + (x + width) + ',' + (y + newRadius)) + ('L ' + (x + width) + ',' + (y + height - newRadius) + ' A ' + newRadius + ',' + newRadius + ',0,0,1,' + (x + width - newRadius) + ',' + (y + height)) + ('L ' + (x + newRadius) + ',' + (y + height) + ' A ' + newRadius + ',' + newRadius + ',0,0,1,' + x + ',' + (y + height - newRadius) + ' Z'); } else { path = 'M ' + x + ',' + y + ' h ' + width + ' v ' + height + ' h ' + -width + ' Z'; } return path; } }, { key: 'render', value: function render() { var _props = this.props; var x = _props.x; var y = _props.y; var width = _props.width; var height = _props.height; var radius = _props.radius; var onClick = _props.onClick; var onMouseEnter = _props.onMouseEnter; var onMouseLeave = _props.onMouseLeave; var className = _props.className; if (x !== +x || y !== +y || width !== +width || height !== +height) { return null; } var layerClass = (0, _classnames2.default)('recharts-rectangle', className); return _react2.default.createElement('path', _extends({}, _ReactUtils2.default.getPresentationAttributes(this.props), { className: layerClass, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, onClick: onClick, d: this.getPath(x, y, width, height, radius) })); } }]); return Rectangle; }(_react.Component), _class2.displayName = 'Rectangle', _class2.propTypes = _extends({}, _ReactUtils.PRESENTATION_ATTRIBUTES, { className: _react.PropTypes.string, x: _react.PropTypes.number, y: _react.PropTypes.number, width: _react.PropTypes.number, height: _react.PropTypes.number, radius: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.array]), onMouseEnter: _react.PropTypes.func, onMouseLeave: _react.PropTypes.func, onClick: _react.PropTypes.func }), _class2.defaultProps = { x: 0, y: 0, width: 0, height: 0, // The radius of border // The radius of four corners when radius is a number // The radius of left-top, right-top, right-bottom, left-bottom when radius is an array radius: 0, stroke: 'none', strokeWidth: 1, strokeDasharray: 'none', fill: '#000', onMouseEnter: function onMouseEnter() {}, onMouseLeave: function onMouseLeave() {}, onClick: function onClick() {} }, _temp)) || _class; exports.default = Rectangle;