UNPKG

recharts

Version:
226 lines 12 kB
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } import _isFunction from "lodash/isFunction"; import _uniqBy from "lodash/uniqBy"; var _excluded = ["ref"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } /** * @fileOverview Legend */ import React, { PureComponent } from 'react'; import { DefaultLegendContent } from './DefaultLegendContent'; import { isNumber } from '../util/DataUtils'; function defaultUniqBy(entry) { return entry.value; } function getUniqPayload(option, payload) { if (option === true) { return _uniqBy(payload, defaultUniqBy); } if (_isFunction(option)) { return _uniqBy(payload, option); } return payload; } function renderContent(content, props) { if ( /*#__PURE__*/React.isValidElement(content)) { return /*#__PURE__*/React.cloneElement(content, props); } if (_isFunction(content)) { return /*#__PURE__*/React.createElement(content, props); } var ref = props.ref, otherProps = _objectWithoutProperties(props, _excluded); return /*#__PURE__*/React.createElement(DefaultLegendContent, otherProps); } var EPS = 1; export var Legend = /*#__PURE__*/function (_PureComponent) { _inherits(Legend, _PureComponent); var _super = _createSuper(Legend); function Legend() { var _this; _classCallCheck(this, Legend); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "state", { boxWidth: -1, boxHeight: -1 }); return _this; } _createClass(Legend, [{ key: "componentDidMount", value: function componentDidMount() { this.updateBBox(); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this.updateBBox(); } }, { key: "getBBox", value: function getBBox() { if (this.wrapperNode && this.wrapperNode.getBoundingClientRect) { return this.wrapperNode.getBoundingClientRect(); } return null; } }, { key: "getBBoxSnapshot", value: function getBBoxSnapshot() { var _this$state = this.state, boxWidth = _this$state.boxWidth, boxHeight = _this$state.boxHeight; if (boxWidth >= 0 && boxHeight >= 0) { return { width: boxWidth, height: boxHeight }; } return null; } }, { key: "getDefaultPosition", value: function getDefaultPosition(style) { var _this$props = this.props, layout = _this$props.layout, align = _this$props.align, verticalAlign = _this$props.verticalAlign, margin = _this$props.margin, chartWidth = _this$props.chartWidth, chartHeight = _this$props.chartHeight; var hPos, vPos; if (!style || (style.left === undefined || style.left === null) && (style.right === undefined || style.right === null)) { if (align === 'center' && layout === 'vertical') { var _box = this.getBBoxSnapshot() || { width: 0 }; hPos = { left: ((chartWidth || 0) - _box.width) / 2 }; } else { hPos = align === 'right' ? { right: margin && margin.right || 0 } : { left: margin && margin.left || 0 }; } } if (!style || (style.top === undefined || style.top === null) && (style.bottom === undefined || style.bottom === null)) { if (verticalAlign === 'middle') { var _box2 = this.getBBoxSnapshot() || { height: 0 }; vPos = { top: ((chartHeight || 0) - _box2.height) / 2 }; } else { vPos = verticalAlign === 'bottom' ? { bottom: margin && margin.bottom || 0 } : { top: margin && margin.top || 0 }; } } return _objectSpread(_objectSpread({}, hPos), vPos); } }, { key: "updateBBox", value: function updateBBox() { var _this$state2 = this.state, boxWidth = _this$state2.boxWidth, boxHeight = _this$state2.boxHeight; var onBBoxUpdate = this.props.onBBoxUpdate; if (this.wrapperNode && this.wrapperNode.getBoundingClientRect) { var _box3 = this.wrapperNode.getBoundingClientRect(); if (Math.abs(_box3.width - boxWidth) > EPS || Math.abs(_box3.height - boxHeight) > EPS) { this.setState({ boxWidth: _box3.width, boxHeight: _box3.height }, function () { if (onBBoxUpdate) { onBBoxUpdate(_box3); } }); } } else if (boxWidth !== -1 || boxHeight !== -1) { this.setState({ boxWidth: -1, boxHeight: -1 }, function () { if (onBBoxUpdate) { onBBoxUpdate(null); } }); } } }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, content = _this$props2.content, width = _this$props2.width, height = _this$props2.height, wrapperStyle = _this$props2.wrapperStyle, payloadUniqBy = _this$props2.payloadUniqBy, payload = _this$props2.payload; var outerStyle = _objectSpread(_objectSpread({ position: 'absolute', width: width || 'auto', height: height || 'auto' }, this.getDefaultPosition(wrapperStyle)), wrapperStyle); return /*#__PURE__*/React.createElement("div", { className: "recharts-legend-wrapper", style: outerStyle, ref: function ref(node) { _this2.wrapperNode = node; } }, renderContent(content, _objectSpread(_objectSpread({}, this.props), {}, { payload: getUniqPayload(payloadUniqBy, payload) }))); } }], [{ key: "getWithHeight", value: function getWithHeight(item, chartWidth) { var layout = item.props.layout; if (layout === 'vertical' && isNumber(item.props.height)) { return { height: item.props.height }; } if (layout === 'horizontal') { return { width: item.props.width || chartWidth }; } return null; } }]); return Legend; }(PureComponent); _defineProperty(Legend, "displayName", 'Legend'); _defineProperty(Legend, "defaultProps", { iconSize: 14, layout: 'horizontal', align: 'center', verticalAlign: 'bottom' });