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