recharts
Version:
React charts
233 lines (232 loc) • 13.6 kB
JavaScript
;
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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Legend = void 0;
var _isFunction2 = _interopRequireDefault(require("lodash/isFunction"));
var _uniqBy2 = _interopRequireDefault(require("lodash/uniqBy"));
var _react = _interopRequireWildcard(require("react"));
var _DefaultLegendContent = require("./DefaultLegendContent");
var _DataUtils = require("../util/DataUtils");
var _excluded = ["ref"];
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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; }
function defaultUniqBy(entry) {
return entry.value;
}
function getUniqPayload(option, payload) {
if (option === true) {
return (0, _uniqBy2["default"])(payload, defaultUniqBy);
}
if ((0, _isFunction2["default"])(option)) {
return (0, _uniqBy2["default"])(payload, option);
}
return payload;
}
function renderContent(content, props) {
if ( /*#__PURE__*/_react["default"].isValidElement(content)) {
return /*#__PURE__*/_react["default"].cloneElement(content, props);
}
if ((0, _isFunction2["default"])(content)) {
return /*#__PURE__*/_react["default"].createElement(content, props);
}
var ref = props.ref,
otherProps = _objectWithoutProperties(props, _excluded);
return /*#__PURE__*/_react["default"].createElement(_DefaultLegendContent.DefaultLegendContent, otherProps);
}
var EPS = 1;
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["default"].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' && (0, _DataUtils.isNumber)(item.props.height)) {
return {
height: item.props.height
};
}
if (layout === 'horizontal') {
return {
width: item.props.width || chartWidth
};
}
return null;
}
}]);
return Legend;
}(_react.PureComponent);
exports.Legend = Legend;
_defineProperty(Legend, "displayName", 'Legend');
_defineProperty(Legend, "defaultProps", {
iconSize: 14,
layout: 'horizontal',
align: 'center',
verticalAlign: 'bottom'
});