| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.ReportChartSize = exports.ReportChartMargin = void 0;
|
|---|
| 7 | exports.cartesianViewBoxToTrapezoid = cartesianViewBoxToTrapezoid;
|
|---|
| 8 | exports.useViewBox = exports.usePolarChartLayout = exports.useOffsetInternal = exports.useMargin = exports.useIsInChartContext = exports.useChartWidth = exports.useChartLayout = exports.useChartHeight = exports.useCartesianChartLayout = exports.selectPolarChartLayout = exports.selectChartLayout = void 0;
|
|---|
| 9 | var _react = require("react");
|
|---|
| 10 | var _hooks = require("../state/hooks");
|
|---|
| 11 | var _layoutSlice = require("../state/layoutSlice");
|
|---|
| 12 | var _selectChartOffsetInternal = require("../state/selectors/selectChartOffsetInternal");
|
|---|
| 13 | var _containerSelectors = require("../state/selectors/containerSelectors");
|
|---|
| 14 | var _PanoramaContext = require("./PanoramaContext");
|
|---|
| 15 | var _brushSelectors = require("../state/selectors/brushSelectors");
|
|---|
| 16 | var _ResponsiveContainer = require("../component/ResponsiveContainer");
|
|---|
| 17 | var _isWellBehavedNumber = require("../util/isWellBehavedNumber");
|
|---|
| 18 | function cartesianViewBoxToTrapezoid(box) {
|
|---|
| 19 | if (!box) {
|
|---|
| 20 | return undefined;
|
|---|
| 21 | }
|
|---|
| 22 | return {
|
|---|
| 23 | x: box.x,
|
|---|
| 24 | y: box.y,
|
|---|
| 25 | upperWidth: 'upperWidth' in box ? box.upperWidth : box.width,
|
|---|
| 26 | lowerWidth: 'lowerWidth' in box ? box.lowerWidth : box.width,
|
|---|
| 27 | width: box.width,
|
|---|
| 28 | height: box.height
|
|---|
| 29 | };
|
|---|
| 30 | }
|
|---|
| 31 | var useViewBox = () => {
|
|---|
| 32 | var _useAppSelector;
|
|---|
| 33 | var panorama = (0, _PanoramaContext.useIsPanorama)();
|
|---|
| 34 | var rootViewBox = (0, _hooks.useAppSelector)(_selectChartOffsetInternal.selectChartViewBox);
|
|---|
| 35 | var brushDimensions = (0, _hooks.useAppSelector)(_brushSelectors.selectBrushDimensions);
|
|---|
| 36 | var brushPadding = (_useAppSelector = (0, _hooks.useAppSelector)(_brushSelectors.selectBrushSettings)) === null || _useAppSelector === void 0 ? void 0 : _useAppSelector.padding;
|
|---|
| 37 | if (!panorama || !brushDimensions || !brushPadding) {
|
|---|
| 38 | return rootViewBox;
|
|---|
| 39 | }
|
|---|
| 40 | return {
|
|---|
| 41 | width: brushDimensions.width - brushPadding.left - brushPadding.right,
|
|---|
| 42 | height: brushDimensions.height - brushPadding.top - brushPadding.bottom,
|
|---|
| 43 | x: brushPadding.left,
|
|---|
| 44 | y: brushPadding.top
|
|---|
| 45 | };
|
|---|
| 46 | };
|
|---|
| 47 | exports.useViewBox = useViewBox;
|
|---|
| 48 | var manyComponentsThrowErrorsIfOffsetIsUndefined = {
|
|---|
| 49 | top: 0,
|
|---|
| 50 | bottom: 0,
|
|---|
| 51 | left: 0,
|
|---|
| 52 | right: 0,
|
|---|
| 53 | width: 0,
|
|---|
| 54 | height: 0,
|
|---|
| 55 | brushBottom: 0
|
|---|
| 56 | };
|
|---|
| 57 | /**
|
|---|
| 58 | * For internal use only. If you want this information, `import { useOffset } from 'recharts'` instead.
|
|---|
| 59 | *
|
|---|
| 60 | * Returns the offset of the chart in pixels.
|
|---|
| 61 | *
|
|---|
| 62 | * @returns {ChartOffsetInternal} The offset of the chart in pixels, or a default value if not in a chart context.
|
|---|
| 63 | */
|
|---|
| 64 | var useOffsetInternal = () => {
|
|---|
| 65 | var _useAppSelector2;
|
|---|
| 66 | return (_useAppSelector2 = (0, _hooks.useAppSelector)(_selectChartOffsetInternal.selectChartOffsetInternal)) !== null && _useAppSelector2 !== void 0 ? _useAppSelector2 : manyComponentsThrowErrorsIfOffsetIsUndefined;
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Returns the width of the chart in pixels.
|
|---|
| 71 | *
|
|---|
| 72 | * If you are using chart with hardcoded `width` prop, then the width returned will be the same
|
|---|
| 73 | * as the `width` prop on the main chart element.
|
|---|
| 74 | *
|
|---|
| 75 | * If you are using a chart with a `ResponsiveContainer`, the width will be the size of the chart
|
|---|
| 76 | * as the ResponsiveContainer has decided it would be.
|
|---|
| 77 | *
|
|---|
| 78 | * If the chart has any axes or legend, the `width` will be the size of the chart
|
|---|
| 79 | * including the axes and legend. Meaning: adding axes and legend will not change the width.
|
|---|
| 80 | *
|
|---|
| 81 | * The dimensions do not scale, meaning as user zoom in and out, the width number will not change
|
|---|
| 82 | * as the chart gets visually larger or smaller.
|
|---|
| 83 | *
|
|---|
| 84 | * Returns `undefined` if used outside a chart context.
|
|---|
| 85 | *
|
|---|
| 86 | * @returns {number | undefined} The width of the chart in pixels, or `undefined` if not in a chart context.
|
|---|
| 87 | */
|
|---|
| 88 | exports.useOffsetInternal = useOffsetInternal;
|
|---|
| 89 | var useChartWidth = () => {
|
|---|
| 90 | return (0, _hooks.useAppSelector)(_containerSelectors.selectChartWidth);
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Returns the height of the chart in pixels.
|
|---|
| 95 | *
|
|---|
| 96 | * If you are using chart with hardcoded `height` props, then the height returned will be the same
|
|---|
| 97 | * as the `height` prop on the main chart element.
|
|---|
| 98 | *
|
|---|
| 99 | * If you are using a chart with a `ResponsiveContainer`, the height will be the size of the chart
|
|---|
| 100 | * as the ResponsiveContainer has decided it would be.
|
|---|
| 101 | *
|
|---|
| 102 | * If the chart has any axes or legend, the `height` will be the size of the chart
|
|---|
| 103 | * including the axes and legend. Meaning: adding axes and legend will not change the height.
|
|---|
| 104 | *
|
|---|
| 105 | * The dimensions do not scale, meaning as user zoom in and out, the height number will not change
|
|---|
| 106 | * as the chart gets visually larger or smaller.
|
|---|
| 107 | *
|
|---|
| 108 | * Returns `undefined` if used outside a chart context.
|
|---|
| 109 | *
|
|---|
| 110 | * @returns {number | undefined} The height of the chart in pixels, or `undefined` if not in a chart context.
|
|---|
| 111 | */
|
|---|
| 112 | exports.useChartWidth = useChartWidth;
|
|---|
| 113 | var useChartHeight = () => {
|
|---|
| 114 | return (0, _hooks.useAppSelector)(_containerSelectors.selectChartHeight);
|
|---|
| 115 | };
|
|---|
| 116 |
|
|---|
| 117 | /**
|
|---|
| 118 | * Margin is the empty space around the chart. Excludes axes and legend and brushes and the like.
|
|---|
| 119 | * This is declared by the user in the chart props.
|
|---|
| 120 | * If you are interested in the space occupied by axes, legend, or brushes,
|
|---|
| 121 | * use {@link useOffset} instead, which also includes calculated widths and heights of axes and legends.
|
|---|
| 122 | *
|
|---|
| 123 | * Returns `undefined` if used outside a chart context.
|
|---|
| 124 | *
|
|---|
| 125 | * @returns {Margin | undefined} The margin of the chart in pixels, or `undefined` if not in a chart context.
|
|---|
| 126 | */
|
|---|
| 127 | exports.useChartHeight = useChartHeight;
|
|---|
| 128 | var useMargin = () => {
|
|---|
| 129 | return (0, _hooks.useAppSelector)(state => state.layout.margin);
|
|---|
| 130 | };
|
|---|
| 131 | exports.useMargin = useMargin;
|
|---|
| 132 | var selectChartLayout = state => state.layout.layoutType;
|
|---|
| 133 | exports.selectChartLayout = selectChartLayout;
|
|---|
| 134 | var useChartLayout = () => (0, _hooks.useAppSelector)(selectChartLayout);
|
|---|
| 135 | exports.useChartLayout = useChartLayout;
|
|---|
| 136 | var useCartesianChartLayout = () => {
|
|---|
| 137 | var layout = useChartLayout();
|
|---|
| 138 | if (layout === 'horizontal' || layout === 'vertical') {
|
|---|
| 139 | return layout;
|
|---|
| 140 | }
|
|---|
| 141 | return undefined;
|
|---|
| 142 | };
|
|---|
| 143 | exports.useCartesianChartLayout = useCartesianChartLayout;
|
|---|
| 144 | var selectPolarChartLayout = state => {
|
|---|
| 145 | var layout = state.layout.layoutType;
|
|---|
| 146 | if (layout === 'centric' || layout === 'radial') {
|
|---|
| 147 | return layout;
|
|---|
| 148 | }
|
|---|
| 149 | return undefined;
|
|---|
| 150 | };
|
|---|
| 151 | exports.selectPolarChartLayout = selectPolarChartLayout;
|
|---|
| 152 | var usePolarChartLayout = () => {
|
|---|
| 153 | return (0, _hooks.useAppSelector)(selectPolarChartLayout);
|
|---|
| 154 | };
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * Returns true if the component is rendered inside a chart context.
|
|---|
| 158 | * Some components may be used both inside and outside of charts,
|
|---|
| 159 | * and this hook allows them to determine if they are in a chart context or not.
|
|---|
| 160 | *
|
|---|
| 161 | * Other selectors may return undefined when used outside a chart context,
|
|---|
| 162 | * or undefined when inside a chart, but without relevant data.
|
|---|
| 163 | * This hook provides a more explicit way to check for chart context.
|
|---|
| 164 | *
|
|---|
| 165 | * @returns {boolean} True if in chart context, false otherwise.
|
|---|
| 166 | */
|
|---|
| 167 | exports.usePolarChartLayout = usePolarChartLayout;
|
|---|
| 168 | var useIsInChartContext = () => {
|
|---|
| 169 | /*
|
|---|
| 170 | * All charts provide a layout type in the chart context.
|
|---|
| 171 | * If we have a layout type, we are in a chart context.
|
|---|
| 172 | */
|
|---|
| 173 | var layout = useChartLayout();
|
|---|
| 174 | return layout !== undefined;
|
|---|
| 175 | };
|
|---|
| 176 | exports.useIsInChartContext = useIsInChartContext;
|
|---|
| 177 | var ReportChartSize = props => {
|
|---|
| 178 | var dispatch = (0, _hooks.useAppDispatch)();
|
|---|
| 179 |
|
|---|
| 180 | /*
|
|---|
| 181 | * Skip dispatching properties in panorama chart for two reasons:
|
|---|
| 182 | * 1. The root chart should be deciding on these properties, and
|
|---|
| 183 | * 2. Brush reads these properties from redux store, and so they must remain stable
|
|---|
| 184 | * to avoid circular dependency and infinite re-rendering.
|
|---|
| 185 | */
|
|---|
| 186 | var isPanorama = (0, _PanoramaContext.useIsPanorama)();
|
|---|
| 187 | var {
|
|---|
| 188 | width: widthFromProps,
|
|---|
| 189 | height: heightFromProps
|
|---|
| 190 | } = props;
|
|---|
| 191 | var responsiveContainerCalculations = (0, _ResponsiveContainer.useResponsiveContainerContext)();
|
|---|
| 192 | var width = widthFromProps;
|
|---|
| 193 | var height = heightFromProps;
|
|---|
| 194 | if (responsiveContainerCalculations) {
|
|---|
| 195 | /*
|
|---|
| 196 | * In case we receive width and height from ResponsiveContainer,
|
|---|
| 197 | * we will always prefer those.
|
|---|
| 198 | * Only in case ResponsiveContainer does not provide width or height,
|
|---|
| 199 | * we will fall back to the explicitly provided width and height.
|
|---|
| 200 | *
|
|---|
| 201 | * This to me feels backwards - we should allow override by the more specific props on individual charts, right?
|
|---|
| 202 | * But this is 3.x behaviour, so let's keep it for backwards compatibility.
|
|---|
| 203 | *
|
|---|
| 204 | * We can change this in 4.x if we want to.
|
|---|
| 205 | */
|
|---|
| 206 | width = responsiveContainerCalculations.width > 0 ? responsiveContainerCalculations.width : widthFromProps;
|
|---|
| 207 | height = responsiveContainerCalculations.height > 0 ? responsiveContainerCalculations.height : heightFromProps;
|
|---|
| 208 | }
|
|---|
| 209 | (0, _react.useEffect)(() => {
|
|---|
| 210 | if (!isPanorama && (0, _isWellBehavedNumber.isPositiveNumber)(width) && (0, _isWellBehavedNumber.isPositiveNumber)(height)) {
|
|---|
| 211 | dispatch((0, _layoutSlice.setChartSize)({
|
|---|
| 212 | width,
|
|---|
| 213 | height
|
|---|
| 214 | }));
|
|---|
| 215 | }
|
|---|
| 216 | }, [dispatch, isPanorama, width, height]);
|
|---|
| 217 | return null;
|
|---|
| 218 | };
|
|---|
| 219 | exports.ReportChartSize = ReportChartSize;
|
|---|
| 220 | var ReportChartMargin = _ref => {
|
|---|
| 221 | var {
|
|---|
| 222 | margin
|
|---|
| 223 | } = _ref;
|
|---|
| 224 | var dispatch = (0, _hooks.useAppDispatch)();
|
|---|
| 225 | (0, _react.useEffect)(() => {
|
|---|
| 226 | dispatch((0, _layoutSlice.setMargin)(margin));
|
|---|
| 227 | }, [dispatch, margin]);
|
|---|
| 228 | return null;
|
|---|
| 229 | };
|
|---|
| 230 | exports.ReportChartMargin = ReportChartMargin; |
|---|