source: node_modules/recharts/es6/component/Legend.js@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 7.9 KB
Line 
1var _excluded = ["contextPayload"];
2function _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); }
3function 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; }
4function _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; }
5function _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; }
6function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
7function _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); }
8function _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; }
9function _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; }
10import * as React from 'react';
11import { useEffect } from 'react';
12import { createPortal } from 'react-dom';
13import { useLegendPortal } from '../context/legendPortalContext';
14import { DefaultLegendContent } from './DefaultLegendContent';
15import { getUniqPayload } from '../util/payload/getUniqPayload';
16import { useLegendPayload } from '../context/legendPayloadContext';
17import { useElementOffset } from '../util/useElementOffset';
18import { useChartHeight, useChartWidth, useMargin } from '../context/chartLayoutContext';
19import { setLegendSettings, setLegendSize } from '../state/legendSlice';
20import { useAppDispatch } from '../state/hooks';
21import { resolveDefaultProps } from '../util/resolveDefaultProps';
22function defaultUniqBy(entry) {
23 return entry.value;
24}
25function LegendContent(props) {
26 var {
27 contextPayload
28 } = props,
29 otherProps = _objectWithoutProperties(props, _excluded);
30 var finalPayload = getUniqPayload(contextPayload, props.payloadUniqBy, defaultUniqBy);
31 var contentProps = _objectSpread(_objectSpread({}, otherProps), {}, {
32 payload: finalPayload
33 });
34 if (/*#__PURE__*/React.isValidElement(props.content)) {
35 return /*#__PURE__*/React.cloneElement(props.content, contentProps);
36 }
37 if (typeof props.content === 'function') {
38 return /*#__PURE__*/React.createElement(props.content, contentProps);
39 }
40 return /*#__PURE__*/React.createElement(DefaultLegendContent, contentProps);
41}
42function getDefaultPosition(style, props, margin, chartWidth, chartHeight, box) {
43 var {
44 layout,
45 align,
46 verticalAlign
47 } = props;
48 var hPos, vPos;
49 if (!style || (style.left === undefined || style.left === null) && (style.right === undefined || style.right === null)) {
50 if (align === 'center' && layout === 'vertical') {
51 hPos = {
52 left: ((chartWidth || 0) - box.width) / 2
53 };
54 } else {
55 hPos = align === 'right' ? {
56 right: margin && margin.right || 0
57 } : {
58 left: margin && margin.left || 0
59 };
60 }
61 }
62 if (!style || (style.top === undefined || style.top === null) && (style.bottom === undefined || style.bottom === null)) {
63 if (verticalAlign === 'middle') {
64 vPos = {
65 top: ((chartHeight || 0) - box.height) / 2
66 };
67 } else {
68 vPos = verticalAlign === 'bottom' ? {
69 bottom: margin && margin.bottom || 0
70 } : {
71 top: margin && margin.top || 0
72 };
73 }
74 }
75 return _objectSpread(_objectSpread({}, hPos), vPos);
76}
77function LegendSettingsDispatcher(props) {
78 var dispatch = useAppDispatch();
79 useEffect(() => {
80 dispatch(setLegendSettings(props));
81 }, [dispatch, props]);
82 return null;
83}
84function LegendSizeDispatcher(props) {
85 var dispatch = useAppDispatch();
86 useEffect(() => {
87 dispatch(setLegendSize(props));
88 return () => {
89 dispatch(setLegendSize({
90 width: 0,
91 height: 0
92 }));
93 };
94 }, [dispatch, props]);
95 return null;
96}
97function getWidthOrHeight(layout, height, width, maxWidth) {
98 if (layout === 'vertical' && height != null) {
99 return {
100 height
101 };
102 }
103 if (layout === 'horizontal') {
104 return {
105 width: width || maxWidth
106 };
107 }
108 return null;
109}
110export var legendDefaultProps = {
111 align: 'center',
112 iconSize: 14,
113 inactiveColor: '#ccc',
114 itemSorter: 'value',
115 layout: 'horizontal',
116 verticalAlign: 'bottom'
117};
118
119/**
120 * @consumes CartesianChartContext
121 * @consumes PolarChartContext
122 */
123export function Legend(outsideProps) {
124 var props = resolveDefaultProps(outsideProps, legendDefaultProps);
125 var contextPayload = useLegendPayload();
126 var legendPortalFromContext = useLegendPortal();
127 var margin = useMargin();
128 var {
129 width: widthFromProps,
130 height: heightFromProps,
131 wrapperStyle,
132 portal: portalFromProps
133 } = props;
134 // The contextPayload is not used directly inside the hook, but we need the onBBoxUpdate call
135 // when the payload changes, therefore it's here as a dependency.
136 var [lastBoundingBox, updateBoundingBox] = useElementOffset([contextPayload]);
137 var chartWidth = useChartWidth();
138 var chartHeight = useChartHeight();
139 if (chartWidth == null || chartHeight == null) {
140 return null;
141 }
142 var maxWidth = chartWidth - ((margin === null || margin === void 0 ? void 0 : margin.left) || 0) - ((margin === null || margin === void 0 ? void 0 : margin.right) || 0);
143 var widthOrHeight = getWidthOrHeight(props.layout, heightFromProps, widthFromProps, maxWidth);
144 // if the user supplies their own portal, only use their defined wrapper styles
145 var outerStyle = portalFromProps ? wrapperStyle : _objectSpread(_objectSpread({
146 position: 'absolute',
147 width: (widthOrHeight === null || widthOrHeight === void 0 ? void 0 : widthOrHeight.width) || widthFromProps || 'auto',
148 height: (widthOrHeight === null || widthOrHeight === void 0 ? void 0 : widthOrHeight.height) || heightFromProps || 'auto'
149 }, getDefaultPosition(wrapperStyle, props, margin, chartWidth, chartHeight, lastBoundingBox)), wrapperStyle);
150 var legendPortal = portalFromProps !== null && portalFromProps !== void 0 ? portalFromProps : legendPortalFromContext;
151 if (legendPortal == null || contextPayload == null) {
152 return null;
153 }
154 var legendElement = /*#__PURE__*/React.createElement("div", {
155 className: "recharts-legend-wrapper",
156 style: outerStyle,
157 ref: updateBoundingBox
158 }, /*#__PURE__*/React.createElement(LegendSettingsDispatcher, {
159 layout: props.layout,
160 align: props.align,
161 verticalAlign: props.verticalAlign,
162 itemSorter: props.itemSorter
163 }), !portalFromProps && /*#__PURE__*/React.createElement(LegendSizeDispatcher, {
164 width: lastBoundingBox.width,
165 height: lastBoundingBox.height
166 }), /*#__PURE__*/React.createElement(LegendContent, _extends({}, props, widthOrHeight, {
167 margin: margin,
168 chartWidth: chartWidth,
169 chartHeight: chartHeight,
170 contextPayload: contextPayload
171 })));
172 return /*#__PURE__*/createPortal(legendElement, legendPortal);
173}
174Legend.displayName = 'Legend';
Note: See TracBrowser for help on using the repository browser.