| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.Legend = Legend;
|
|---|
| 7 | exports.legendDefaultProps = void 0;
|
|---|
| 8 | var _react = _interopRequireWildcard(require("react"));
|
|---|
| 9 | var React = _react;
|
|---|
| 10 | var _reactDom = require("react-dom");
|
|---|
| 11 | var _legendPortalContext = require("../context/legendPortalContext");
|
|---|
| 12 | var _DefaultLegendContent = require("./DefaultLegendContent");
|
|---|
| 13 | var _getUniqPayload = require("../util/payload/getUniqPayload");
|
|---|
| 14 | var _legendPayloadContext = require("../context/legendPayloadContext");
|
|---|
| 15 | var _useElementOffset = require("../util/useElementOffset");
|
|---|
| 16 | var _chartLayoutContext = require("../context/chartLayoutContext");
|
|---|
| 17 | var _legendSlice = require("../state/legendSlice");
|
|---|
| 18 | var _hooks = require("../state/hooks");
|
|---|
| 19 | var _resolveDefaultProps = require("../util/resolveDefaultProps");
|
|---|
| 20 | var _excluded = ["contextPayload"];
|
|---|
| 21 | function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|---|
| 22 | function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|---|
| 23 | function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|---|
| 24 | function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|---|
| 25 | function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|---|
| 26 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 27 | function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|---|
| 28 | function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|---|
| 29 | function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|---|
| 30 | function defaultUniqBy(entry) {
|
|---|
| 31 | return entry.value;
|
|---|
| 32 | }
|
|---|
| 33 | function LegendContent(props) {
|
|---|
| 34 | var {
|
|---|
| 35 | contextPayload
|
|---|
| 36 | } = props,
|
|---|
| 37 | otherProps = _objectWithoutProperties(props, _excluded);
|
|---|
| 38 | var finalPayload = (0, _getUniqPayload.getUniqPayload)(contextPayload, props.payloadUniqBy, defaultUniqBy);
|
|---|
| 39 | var contentProps = _objectSpread(_objectSpread({}, otherProps), {}, {
|
|---|
| 40 | payload: finalPayload
|
|---|
| 41 | });
|
|---|
| 42 | if (/*#__PURE__*/React.isValidElement(props.content)) {
|
|---|
| 43 | return /*#__PURE__*/React.cloneElement(props.content, contentProps);
|
|---|
| 44 | }
|
|---|
| 45 | if (typeof props.content === 'function') {
|
|---|
| 46 | return /*#__PURE__*/React.createElement(props.content, contentProps);
|
|---|
| 47 | }
|
|---|
| 48 | return /*#__PURE__*/React.createElement(_DefaultLegendContent.DefaultLegendContent, contentProps);
|
|---|
| 49 | }
|
|---|
| 50 | function getDefaultPosition(style, props, margin, chartWidth, chartHeight, box) {
|
|---|
| 51 | var {
|
|---|
| 52 | layout,
|
|---|
| 53 | align,
|
|---|
| 54 | verticalAlign
|
|---|
| 55 | } = props;
|
|---|
| 56 | var hPos, vPos;
|
|---|
| 57 | if (!style || (style.left === undefined || style.left === null) && (style.right === undefined || style.right === null)) {
|
|---|
| 58 | if (align === 'center' && layout === 'vertical') {
|
|---|
| 59 | hPos = {
|
|---|
| 60 | left: ((chartWidth || 0) - box.width) / 2
|
|---|
| 61 | };
|
|---|
| 62 | } else {
|
|---|
| 63 | hPos = align === 'right' ? {
|
|---|
| 64 | right: margin && margin.right || 0
|
|---|
| 65 | } : {
|
|---|
| 66 | left: margin && margin.left || 0
|
|---|
| 67 | };
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | if (!style || (style.top === undefined || style.top === null) && (style.bottom === undefined || style.bottom === null)) {
|
|---|
| 71 | if (verticalAlign === 'middle') {
|
|---|
| 72 | vPos = {
|
|---|
| 73 | top: ((chartHeight || 0) - box.height) / 2
|
|---|
| 74 | };
|
|---|
| 75 | } else {
|
|---|
| 76 | vPos = verticalAlign === 'bottom' ? {
|
|---|
| 77 | bottom: margin && margin.bottom || 0
|
|---|
| 78 | } : {
|
|---|
| 79 | top: margin && margin.top || 0
|
|---|
| 80 | };
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | return _objectSpread(_objectSpread({}, hPos), vPos);
|
|---|
| 84 | }
|
|---|
| 85 | function LegendSettingsDispatcher(props) {
|
|---|
| 86 | var dispatch = (0, _hooks.useAppDispatch)();
|
|---|
| 87 | (0, _react.useEffect)(() => {
|
|---|
| 88 | dispatch((0, _legendSlice.setLegendSettings)(props));
|
|---|
| 89 | }, [dispatch, props]);
|
|---|
| 90 | return null;
|
|---|
| 91 | }
|
|---|
| 92 | function LegendSizeDispatcher(props) {
|
|---|
| 93 | var dispatch = (0, _hooks.useAppDispatch)();
|
|---|
| 94 | (0, _react.useEffect)(() => {
|
|---|
| 95 | dispatch((0, _legendSlice.setLegendSize)(props));
|
|---|
| 96 | return () => {
|
|---|
| 97 | dispatch((0, _legendSlice.setLegendSize)({
|
|---|
| 98 | width: 0,
|
|---|
| 99 | height: 0
|
|---|
| 100 | }));
|
|---|
| 101 | };
|
|---|
| 102 | }, [dispatch, props]);
|
|---|
| 103 | return null;
|
|---|
| 104 | }
|
|---|
| 105 | function getWidthOrHeight(layout, height, width, maxWidth) {
|
|---|
| 106 | if (layout === 'vertical' && height != null) {
|
|---|
| 107 | return {
|
|---|
| 108 | height
|
|---|
| 109 | };
|
|---|
| 110 | }
|
|---|
| 111 | if (layout === 'horizontal') {
|
|---|
| 112 | return {
|
|---|
| 113 | width: width || maxWidth
|
|---|
| 114 | };
|
|---|
| 115 | }
|
|---|
| 116 | return null;
|
|---|
| 117 | }
|
|---|
| 118 | var legendDefaultProps = exports.legendDefaultProps = {
|
|---|
| 119 | align: 'center',
|
|---|
| 120 | iconSize: 14,
|
|---|
| 121 | inactiveColor: '#ccc',
|
|---|
| 122 | itemSorter: 'value',
|
|---|
| 123 | layout: 'horizontal',
|
|---|
| 124 | verticalAlign: 'bottom'
|
|---|
| 125 | };
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * @consumes CartesianChartContext
|
|---|
| 129 | * @consumes PolarChartContext
|
|---|
| 130 | */
|
|---|
| 131 | function Legend(outsideProps) {
|
|---|
| 132 | var props = (0, _resolveDefaultProps.resolveDefaultProps)(outsideProps, legendDefaultProps);
|
|---|
| 133 | var contextPayload = (0, _legendPayloadContext.useLegendPayload)();
|
|---|
| 134 | var legendPortalFromContext = (0, _legendPortalContext.useLegendPortal)();
|
|---|
| 135 | var margin = (0, _chartLayoutContext.useMargin)();
|
|---|
| 136 | var {
|
|---|
| 137 | width: widthFromProps,
|
|---|
| 138 | height: heightFromProps,
|
|---|
| 139 | wrapperStyle,
|
|---|
| 140 | portal: portalFromProps
|
|---|
| 141 | } = props;
|
|---|
| 142 | // The contextPayload is not used directly inside the hook, but we need the onBBoxUpdate call
|
|---|
| 143 | // when the payload changes, therefore it's here as a dependency.
|
|---|
| 144 | var [lastBoundingBox, updateBoundingBox] = (0, _useElementOffset.useElementOffset)([contextPayload]);
|
|---|
| 145 | var chartWidth = (0, _chartLayoutContext.useChartWidth)();
|
|---|
| 146 | var chartHeight = (0, _chartLayoutContext.useChartHeight)();
|
|---|
| 147 | if (chartWidth == null || chartHeight == null) {
|
|---|
| 148 | return null;
|
|---|
| 149 | }
|
|---|
| 150 | var maxWidth = chartWidth - ((margin === null || margin === void 0 ? void 0 : margin.left) || 0) - ((margin === null || margin === void 0 ? void 0 : margin.right) || 0);
|
|---|
| 151 | var widthOrHeight = getWidthOrHeight(props.layout, heightFromProps, widthFromProps, maxWidth);
|
|---|
| 152 | // if the user supplies their own portal, only use their defined wrapper styles
|
|---|
| 153 | var outerStyle = portalFromProps ? wrapperStyle : _objectSpread(_objectSpread({
|
|---|
| 154 | position: 'absolute',
|
|---|
| 155 | width: (widthOrHeight === null || widthOrHeight === void 0 ? void 0 : widthOrHeight.width) || widthFromProps || 'auto',
|
|---|
| 156 | height: (widthOrHeight === null || widthOrHeight === void 0 ? void 0 : widthOrHeight.height) || heightFromProps || 'auto'
|
|---|
| 157 | }, getDefaultPosition(wrapperStyle, props, margin, chartWidth, chartHeight, lastBoundingBox)), wrapperStyle);
|
|---|
| 158 | var legendPortal = portalFromProps !== null && portalFromProps !== void 0 ? portalFromProps : legendPortalFromContext;
|
|---|
| 159 | if (legendPortal == null || contextPayload == null) {
|
|---|
| 160 | return null;
|
|---|
| 161 | }
|
|---|
| 162 | var legendElement = /*#__PURE__*/React.createElement("div", {
|
|---|
| 163 | className: "recharts-legend-wrapper",
|
|---|
| 164 | style: outerStyle,
|
|---|
| 165 | ref: updateBoundingBox
|
|---|
| 166 | }, /*#__PURE__*/React.createElement(LegendSettingsDispatcher, {
|
|---|
| 167 | layout: props.layout,
|
|---|
| 168 | align: props.align,
|
|---|
| 169 | verticalAlign: props.verticalAlign,
|
|---|
| 170 | itemSorter: props.itemSorter
|
|---|
| 171 | }), !portalFromProps && /*#__PURE__*/React.createElement(LegendSizeDispatcher, {
|
|---|
| 172 | width: lastBoundingBox.width,
|
|---|
| 173 | height: lastBoundingBox.height
|
|---|
| 174 | }), /*#__PURE__*/React.createElement(LegendContent, _extends({}, props, widthOrHeight, {
|
|---|
| 175 | margin: margin,
|
|---|
| 176 | chartWidth: chartWidth,
|
|---|
| 177 | chartHeight: chartHeight,
|
|---|
| 178 | contextPayload: contextPayload
|
|---|
| 179 | })));
|
|---|
| 180 | return /*#__PURE__*/(0, _reactDom.createPortal)(legendElement, legendPortal);
|
|---|
| 181 | }
|
|---|
| 182 | Legend.displayName = 'Legend'; |
|---|