| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.useResponsiveContainerContext = exports.ResponsiveContainer = void 0;
|
|---|
| 7 | var _clsx = require("clsx");
|
|---|
| 8 | var _react = _interopRequireWildcard(require("react"));
|
|---|
| 9 | var React = _react;
|
|---|
| 10 | var _throttle = _interopRequireDefault(require("es-toolkit/compat/throttle"));
|
|---|
| 11 | var _DataUtils = require("../util/DataUtils");
|
|---|
| 12 | var _LogUtils = require("../util/LogUtils");
|
|---|
| 13 | var _responsiveContainerUtils = require("./responsiveContainerUtils");
|
|---|
| 14 | var _isWellBehavedNumber = require("../util/isWellBehavedNumber");
|
|---|
| 15 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|---|
| 16 | 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); }
|
|---|
| 17 | 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); }
|
|---|
| 18 | 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; }
|
|---|
| 19 | 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; }
|
|---|
| 20 | 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; }
|
|---|
| 21 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 22 | 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); }
|
|---|
| 23 | var ResponsiveContainerContext = /*#__PURE__*/(0, _react.createContext)(_responsiveContainerUtils.defaultResponsiveContainerProps.initialDimension);
|
|---|
| 24 | function isAcceptableSize(size) {
|
|---|
| 25 | return (0, _isWellBehavedNumber.isPositiveNumber)(size.width) && (0, _isWellBehavedNumber.isPositiveNumber)(size.height);
|
|---|
| 26 | }
|
|---|
| 27 | function ResponsiveContainerContextProvider(_ref) {
|
|---|
| 28 | var {
|
|---|
| 29 | children,
|
|---|
| 30 | width,
|
|---|
| 31 | height
|
|---|
| 32 | } = _ref;
|
|---|
| 33 | var size = (0, _react.useMemo)(() => ({
|
|---|
| 34 | width,
|
|---|
| 35 | height
|
|---|
| 36 | }), [width, height]);
|
|---|
| 37 | if (!isAcceptableSize(size)) {
|
|---|
| 38 | /*
|
|---|
| 39 | * Don't render the container if width or height is non-positive because
|
|---|
| 40 | * in that case the chart will not be rendered properly anyway.
|
|---|
| 41 | * We will instead wait for the next resize event to provide the correct dimensions.
|
|---|
| 42 | */
|
|---|
| 43 | return null;
|
|---|
| 44 | }
|
|---|
| 45 | return /*#__PURE__*/React.createElement(ResponsiveContainerContext.Provider, {
|
|---|
| 46 | value: size
|
|---|
| 47 | }, children);
|
|---|
| 48 | }
|
|---|
| 49 | var useResponsiveContainerContext = () => (0, _react.useContext)(ResponsiveContainerContext);
|
|---|
| 50 | exports.useResponsiveContainerContext = useResponsiveContainerContext;
|
|---|
| 51 | var SizeDetectorContainer = /*#__PURE__*/(0, _react.forwardRef)((_ref2, ref) => {
|
|---|
| 52 | var {
|
|---|
| 53 | aspect,
|
|---|
| 54 | initialDimension = _responsiveContainerUtils.defaultResponsiveContainerProps.initialDimension,
|
|---|
| 55 | width,
|
|---|
| 56 | height,
|
|---|
| 57 | /*
|
|---|
| 58 | * default min-width to 0 if not specified - 'auto' causes issues with flexbox
|
|---|
| 59 | * https://github.com/recharts/recharts/issues/172
|
|---|
| 60 | */
|
|---|
| 61 | minWidth = _responsiveContainerUtils.defaultResponsiveContainerProps.minWidth,
|
|---|
| 62 | minHeight,
|
|---|
| 63 | maxHeight,
|
|---|
| 64 | children,
|
|---|
| 65 | debounce = _responsiveContainerUtils.defaultResponsiveContainerProps.debounce,
|
|---|
| 66 | id,
|
|---|
| 67 | className,
|
|---|
| 68 | onResize,
|
|---|
| 69 | style = {}
|
|---|
| 70 | } = _ref2;
|
|---|
| 71 | var containerRef = (0, _react.useRef)(null);
|
|---|
| 72 | /*
|
|---|
| 73 | * We are using a ref to avoid re-creating the ResizeObserver when the onResize function changes.
|
|---|
| 74 | * The ref is updated on every render, so the latest onResize function is always available in the effect.
|
|---|
| 75 | */
|
|---|
| 76 | var onResizeRef = (0, _react.useRef)();
|
|---|
| 77 | onResizeRef.current = onResize;
|
|---|
| 78 | (0, _react.useImperativeHandle)(ref, () => containerRef.current);
|
|---|
| 79 | var [sizes, setSizes] = (0, _react.useState)({
|
|---|
| 80 | containerWidth: initialDimension.width,
|
|---|
| 81 | containerHeight: initialDimension.height
|
|---|
| 82 | });
|
|---|
| 83 | var setContainerSize = (0, _react.useCallback)((newWidth, newHeight) => {
|
|---|
| 84 | setSizes(prevState => {
|
|---|
| 85 | var roundedWidth = Math.round(newWidth);
|
|---|
| 86 | var roundedHeight = Math.round(newHeight);
|
|---|
| 87 | if (prevState.containerWidth === roundedWidth && prevState.containerHeight === roundedHeight) {
|
|---|
| 88 | return prevState;
|
|---|
| 89 | }
|
|---|
| 90 | return {
|
|---|
| 91 | containerWidth: roundedWidth,
|
|---|
| 92 | containerHeight: roundedHeight
|
|---|
| 93 | };
|
|---|
| 94 | });
|
|---|
| 95 | }, []);
|
|---|
| 96 | (0, _react.useEffect)(() => {
|
|---|
| 97 | if (containerRef.current == null || typeof ResizeObserver === 'undefined') {
|
|---|
| 98 | return _DataUtils.noop;
|
|---|
| 99 | }
|
|---|
| 100 | var callback = entries => {
|
|---|
| 101 | var _onResizeRef$current;
|
|---|
| 102 | var entry = entries[0];
|
|---|
| 103 | if (entry == null) {
|
|---|
| 104 | return;
|
|---|
| 105 | }
|
|---|
| 106 | var {
|
|---|
| 107 | width: containerWidth,
|
|---|
| 108 | height: containerHeight
|
|---|
| 109 | } = entry.contentRect;
|
|---|
| 110 | setContainerSize(containerWidth, containerHeight);
|
|---|
| 111 | (_onResizeRef$current = onResizeRef.current) === null || _onResizeRef$current === void 0 || _onResizeRef$current.call(onResizeRef, containerWidth, containerHeight);
|
|---|
| 112 | };
|
|---|
| 113 | if (debounce > 0) {
|
|---|
| 114 | callback = (0, _throttle.default)(callback, debounce, {
|
|---|
| 115 | trailing: true,
|
|---|
| 116 | leading: false
|
|---|
| 117 | });
|
|---|
| 118 | }
|
|---|
| 119 | var observer = new ResizeObserver(callback);
|
|---|
| 120 | var {
|
|---|
| 121 | width: containerWidth,
|
|---|
| 122 | height: containerHeight
|
|---|
| 123 | } = containerRef.current.getBoundingClientRect();
|
|---|
| 124 | setContainerSize(containerWidth, containerHeight);
|
|---|
| 125 | observer.observe(containerRef.current);
|
|---|
| 126 | return () => {
|
|---|
| 127 | observer.disconnect();
|
|---|
| 128 | };
|
|---|
| 129 | }, [setContainerSize, debounce]);
|
|---|
| 130 | var {
|
|---|
| 131 | containerWidth,
|
|---|
| 132 | containerHeight
|
|---|
| 133 | } = sizes;
|
|---|
| 134 | (0, _LogUtils.warn)(!aspect || aspect > 0, 'The aspect(%s) must be greater than zero.', aspect);
|
|---|
| 135 | var {
|
|---|
| 136 | calculatedWidth,
|
|---|
| 137 | calculatedHeight
|
|---|
| 138 | } = (0, _responsiveContainerUtils.calculateChartDimensions)(containerWidth, containerHeight, {
|
|---|
| 139 | width,
|
|---|
| 140 | height,
|
|---|
| 141 | aspect,
|
|---|
| 142 | maxHeight
|
|---|
| 143 | });
|
|---|
| 144 | (0, _LogUtils.warn)(calculatedWidth != null && calculatedWidth > 0 || calculatedHeight != null && calculatedHeight > 0, "The width(%s) and height(%s) of chart should be greater than 0,\n please check the style of container, or the props width(%s) and height(%s),\n or add a minWidth(%s) or minHeight(%s) or use aspect(%s) to control the\n height and width.", calculatedWidth, calculatedHeight, width, height, minWidth, minHeight, aspect);
|
|---|
| 145 | return /*#__PURE__*/React.createElement("div", {
|
|---|
| 146 | id: id ? "".concat(id) : undefined,
|
|---|
| 147 | className: (0, _clsx.clsx)('recharts-responsive-container', className),
|
|---|
| 148 | style: _objectSpread(_objectSpread({}, style), {}, {
|
|---|
| 149 | width,
|
|---|
| 150 | height,
|
|---|
| 151 | minWidth,
|
|---|
| 152 | minHeight,
|
|---|
| 153 | maxHeight
|
|---|
| 154 | }),
|
|---|
| 155 | ref: containerRef
|
|---|
| 156 | }, /*#__PURE__*/React.createElement("div", {
|
|---|
| 157 | style: (0, _responsiveContainerUtils.getInnerDivStyle)({
|
|---|
| 158 | width,
|
|---|
| 159 | height
|
|---|
| 160 | })
|
|---|
| 161 | }, /*#__PURE__*/React.createElement(ResponsiveContainerContextProvider, {
|
|---|
| 162 | width: calculatedWidth,
|
|---|
| 163 | height: calculatedHeight
|
|---|
| 164 | }, children)));
|
|---|
| 165 | });
|
|---|
| 166 |
|
|---|
| 167 | /**
|
|---|
| 168 | * The `ResponsiveContainer` component is a container that adjusts its width and height based on the size of its parent element.
|
|---|
| 169 | * It is used to create responsive charts that adapt to different screen sizes.
|
|---|
| 170 | *
|
|---|
| 171 | * This component uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver ResizeObserver} API to monitor changes to the size of its parent element.
|
|---|
| 172 | * If you need to support older browsers that do not support this API, you may need to include a polyfill.
|
|---|
| 173 | *
|
|---|
| 174 | * @see {@link https://recharts.github.io/en-US/guide/sizes/ Chart size guide}
|
|---|
| 175 | *
|
|---|
| 176 | * @provides ResponsiveContainerContext
|
|---|
| 177 | */
|
|---|
| 178 | var ResponsiveContainer = exports.ResponsiveContainer = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
|
|---|
| 179 | var responsiveContainerContext = useResponsiveContainerContext();
|
|---|
| 180 | if ((0, _isWellBehavedNumber.isPositiveNumber)(responsiveContainerContext.width) && (0, _isWellBehavedNumber.isPositiveNumber)(responsiveContainerContext.height)) {
|
|---|
| 181 | /*
|
|---|
| 182 | * If we detect that we are already inside another ResponsiveContainer,
|
|---|
| 183 | * we do not attempt to add another layer of responsiveness.
|
|---|
| 184 | */
|
|---|
| 185 | return props.children;
|
|---|
| 186 | }
|
|---|
| 187 | var {
|
|---|
| 188 | width,
|
|---|
| 189 | height
|
|---|
| 190 | } = (0, _responsiveContainerUtils.getDefaultWidthAndHeight)({
|
|---|
| 191 | width: props.width,
|
|---|
| 192 | height: props.height,
|
|---|
| 193 | aspect: props.aspect
|
|---|
| 194 | });
|
|---|
| 195 |
|
|---|
| 196 | /*
|
|---|
| 197 | * Let's try to get the calculated dimensions without having the div container set up.
|
|---|
| 198 | * Sometimes this does produce fixed, positive dimensions. If so, we can skip rendering the div and monitoring its size.
|
|---|
| 199 | */
|
|---|
| 200 | var {
|
|---|
| 201 | calculatedWidth,
|
|---|
| 202 | calculatedHeight
|
|---|
| 203 | } = (0, _responsiveContainerUtils.calculateChartDimensions)(undefined, undefined, {
|
|---|
| 204 | width,
|
|---|
| 205 | height,
|
|---|
| 206 | aspect: props.aspect,
|
|---|
| 207 | maxHeight: props.maxHeight
|
|---|
| 208 | });
|
|---|
| 209 | if ((0, _DataUtils.isNumber)(calculatedWidth) && (0, _DataUtils.isNumber)(calculatedHeight)) {
|
|---|
| 210 | /*
|
|---|
| 211 | * If it just so happens that the combination of width, height, and aspect ratio
|
|---|
| 212 | * results in fixed dimensions, then we don't need to monitor the container's size.
|
|---|
| 213 | * We can just provide these fixed dimensions to the context.
|
|---|
| 214 | *
|
|---|
| 215 | * Note that here we are not checking for positive numbers;
|
|---|
| 216 | * if the user provides a zero or negative width/height, we will just pass that along
|
|---|
| 217 | * as whatever size we detect won't be helping anyway.
|
|---|
| 218 | */
|
|---|
| 219 | return /*#__PURE__*/React.createElement(ResponsiveContainerContextProvider, {
|
|---|
| 220 | width: calculatedWidth,
|
|---|
| 221 | height: calculatedHeight
|
|---|
| 222 | }, props.children);
|
|---|
| 223 | }
|
|---|
| 224 | /*
|
|---|
| 225 | * Static analysis did not produce fixed dimensions,
|
|---|
| 226 | * so we need to render a special div and monitor its size.
|
|---|
| 227 | */
|
|---|
| 228 | return /*#__PURE__*/React.createElement(SizeDetectorContainer, _extends({}, props, {
|
|---|
| 229 | width: width,
|
|---|
| 230 | height: height,
|
|---|
| 231 | ref: ref
|
|---|
| 232 | }));
|
|---|
| 233 | }); |
|---|