ResponsiveContainer.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  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(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; }
  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. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  8. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  9. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  10. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
  11. function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
  12. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  13. /**
  14. * @fileOverview Wrapper component to make charts adapt to the size of parent * DOM
  15. */
  16. import clsx from 'clsx';
  17. import React, { forwardRef, cloneElement, useState, useImperativeHandle, useRef, useEffect, useMemo, useCallback } from 'react';
  18. import throttle from 'lodash/throttle';
  19. import { isPercent } from '../util/DataUtils';
  20. import { warn } from '../util/LogUtils';
  21. import { getDisplayName } from '../util/ReactUtils';
  22. export var ResponsiveContainer = /*#__PURE__*/forwardRef(function (_ref, ref) {
  23. var aspect = _ref.aspect,
  24. _ref$initialDimension = _ref.initialDimension,
  25. initialDimension = _ref$initialDimension === void 0 ? {
  26. width: -1,
  27. height: -1
  28. } : _ref$initialDimension,
  29. _ref$width = _ref.width,
  30. width = _ref$width === void 0 ? '100%' : _ref$width,
  31. _ref$height = _ref.height,
  32. height = _ref$height === void 0 ? '100%' : _ref$height,
  33. _ref$minWidth = _ref.minWidth,
  34. minWidth = _ref$minWidth === void 0 ? 0 : _ref$minWidth,
  35. minHeight = _ref.minHeight,
  36. maxHeight = _ref.maxHeight,
  37. children = _ref.children,
  38. _ref$debounce = _ref.debounce,
  39. debounce = _ref$debounce === void 0 ? 0 : _ref$debounce,
  40. id = _ref.id,
  41. className = _ref.className,
  42. onResize = _ref.onResize,
  43. _ref$style = _ref.style,
  44. style = _ref$style === void 0 ? {} : _ref$style;
  45. var containerRef = useRef(null);
  46. var onResizeRef = useRef();
  47. onResizeRef.current = onResize;
  48. useImperativeHandle(ref, function () {
  49. return Object.defineProperty(containerRef.current, 'current', {
  50. get: function get() {
  51. // eslint-disable-next-line no-console
  52. console.warn('The usage of ref.current.current is deprecated and will no longer be supported.');
  53. return containerRef.current;
  54. },
  55. configurable: true
  56. });
  57. });
  58. var _useState = useState({
  59. containerWidth: initialDimension.width,
  60. containerHeight: initialDimension.height
  61. }),
  62. _useState2 = _slicedToArray(_useState, 2),
  63. sizes = _useState2[0],
  64. setSizes = _useState2[1];
  65. var setContainerSize = useCallback(function (newWidth, newHeight) {
  66. setSizes(function (prevState) {
  67. var roundedWidth = Math.round(newWidth);
  68. var roundedHeight = Math.round(newHeight);
  69. if (prevState.containerWidth === roundedWidth && prevState.containerHeight === roundedHeight) {
  70. return prevState;
  71. }
  72. return {
  73. containerWidth: roundedWidth,
  74. containerHeight: roundedHeight
  75. };
  76. });
  77. }, []);
  78. useEffect(function () {
  79. var callback = function callback(entries) {
  80. var _onResizeRef$current;
  81. var _entries$0$contentRec = entries[0].contentRect,
  82. containerWidth = _entries$0$contentRec.width,
  83. containerHeight = _entries$0$contentRec.height;
  84. setContainerSize(containerWidth, containerHeight);
  85. (_onResizeRef$current = onResizeRef.current) === null || _onResizeRef$current === void 0 || _onResizeRef$current.call(onResizeRef, containerWidth, containerHeight);
  86. };
  87. if (debounce > 0) {
  88. callback = throttle(callback, debounce, {
  89. trailing: true,
  90. leading: false
  91. });
  92. }
  93. var observer = new ResizeObserver(callback);
  94. var _containerRef$current = containerRef.current.getBoundingClientRect(),
  95. containerWidth = _containerRef$current.width,
  96. containerHeight = _containerRef$current.height;
  97. setContainerSize(containerWidth, containerHeight);
  98. observer.observe(containerRef.current);
  99. return function () {
  100. observer.disconnect();
  101. };
  102. }, [setContainerSize, debounce]);
  103. var chartContent = useMemo(function () {
  104. var containerWidth = sizes.containerWidth,
  105. containerHeight = sizes.containerHeight;
  106. if (containerWidth < 0 || containerHeight < 0) {
  107. return null;
  108. }
  109. warn(isPercent(width) || isPercent(height), "The width(%s) and height(%s) are both fixed numbers,\n maybe you don't need to use a ResponsiveContainer.", width, height);
  110. warn(!aspect || aspect > 0, 'The aspect(%s) must be greater than zero.', aspect);
  111. var calculatedWidth = isPercent(width) ? containerWidth : width;
  112. var calculatedHeight = isPercent(height) ? containerHeight : height;
  113. if (aspect && aspect > 0) {
  114. // Preserve the desired aspect ratio
  115. if (calculatedWidth) {
  116. // Will default to using width for aspect ratio
  117. calculatedHeight = calculatedWidth / aspect;
  118. } else if (calculatedHeight) {
  119. // But we should also take height into consideration
  120. calculatedWidth = calculatedHeight * aspect;
  121. }
  122. // if maxHeight is set, overwrite if calculatedHeight is greater than maxHeight
  123. if (maxHeight && calculatedHeight > maxHeight) {
  124. calculatedHeight = maxHeight;
  125. }
  126. }
  127. warn(calculatedWidth > 0 || 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);
  128. var isCharts = !Array.isArray(children) && getDisplayName(children.type).endsWith('Chart');
  129. return React.Children.map(children, function (child) {
  130. if ( /*#__PURE__*/React.isValidElement(child)) {
  131. return /*#__PURE__*/cloneElement(child, _objectSpread({
  132. width: calculatedWidth,
  133. height: calculatedHeight
  134. }, isCharts ? {
  135. style: _objectSpread({
  136. height: '100%',
  137. width: '100%',
  138. maxHeight: calculatedHeight,
  139. maxWidth: calculatedWidth
  140. }, child.props.style)
  141. } : {}));
  142. }
  143. return child;
  144. });
  145. }, [aspect, children, height, maxHeight, minHeight, minWidth, sizes, width]);
  146. return /*#__PURE__*/React.createElement("div", {
  147. id: id ? "".concat(id) : undefined,
  148. className: clsx('recharts-responsive-container', className),
  149. style: _objectSpread(_objectSpread({}, style), {}, {
  150. width: width,
  151. height: height,
  152. minWidth: minWidth,
  153. minHeight: minHeight,
  154. maxHeight: maxHeight
  155. }),
  156. ref: containerRef
  157. }, chartContent);
  158. });