UNPKG

recharts

Version:
137 lines (106 loc) 5.67 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 Legend */ 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 _Surface = require('../container/Surface'); var _Surface2 = _interopRequireDefault(_Surface); var _DefaultLegendContent = require('./DefaultLegendContent'); var _DefaultLegendContent2 = _interopRequireDefault(_DefaultLegendContent); 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 SIZE = 32; var Legend = (0, _pureRenderDecorator2.default)(_class = (_temp = _class2 = function (_Component) { _inherits(Legend, _Component); function Legend() { _classCallCheck(this, Legend); return _possibleConstructorReturn(this, Object.getPrototypeOf(Legend).apply(this, arguments)); } _createClass(Legend, [{ key: 'getDefaultPosition', value: function getDefaultPosition(style) { var _props = this.props; var layout = _props.layout; var align = _props.align; var verticalAlign = _props.verticalAlign; var hPos = undefined; var vPos = undefined; if (!style || (style.left === undefined || style.left === null) && (style.right === undefined || style.right === null)) { hPos = align === 'right' ? { right: 0 } : { left: 0 }; } if (!style || (style.top === undefined || style.top === null) && (style.bottom === undefined || style.bottom === null)) { if (layout === 'vertical') { vPos = verticalAlign === 'bottom' ? { bottom: 0 } : { top: 0 }; } else { vPos = verticalAlign === 'top' ? { top: 0 } : { bottom: 0 }; } } return _extends({}, hPos, vPos); } }, { key: 'render', value: function render() { var _props2 = this.props; var content = _props2.content; var width = _props2.width; var height = _props2.height; var layout = _props2.layout; var wrapperStyle = _props2.wrapperStyle; var outerStyle = _extends({ position: 'absolute', width: width || 'auto', height: height || 'auto' }, this.getDefaultPosition(wrapperStyle), wrapperStyle); return _react2.default.createElement( 'div', { className: 'recharts-legend-wrapper', style: outerStyle }, _react2.default.isValidElement(content) ? _react2.default.cloneElement(content, this.props) : _react2.default.createElement(_DefaultLegendContent2.default, this.props) ); } }], [{ key: 'getWithHeight', value: function getWithHeight(item, chartWidth, chartHeight) { var layout = item.props.layout; if (layout === 'vertical') { return { height: item.props.height || chartHeight }; } return { width: item.props.width || chartWidth }; } }]); return Legend; }(_react.Component), _class2.displayName = 'Legend', _class2.propTypes = { content: _react.PropTypes.element, wrapperStyle: _react.PropTypes.object, width: _react.PropTypes.number, height: _react.PropTypes.number, iconSize: _react.PropTypes.number, layout: _react.PropTypes.oneOf(['horizontal', 'vertical']), align: _react.PropTypes.oneOf(['center', 'left', 'right']), verticalAlign: _react.PropTypes.oneOf(['top', 'bottom', 'middle']), payload: _react.PropTypes.arrayOf(_react.PropTypes.shape({ value: _react.PropTypes.any, id: _react.PropTypes.any, type: _react.PropTypes.oneOf(['line', 'scatter', 'square', 'rect']) })) }, _class2.defaultProps = { iconSize: 14, layout: 'horizontal', align: 'center', verticalAlign: 'bottom', payload: [] }, _temp)) || _class; exports.default = Legend;