UNPKG

recharts

Version:
144 lines (143 loc) 6.11 kB
var _excluded = ["dangerouslySetInnerHTML", "ticks", "scale"], _excluded2 = ["id", "scale"]; 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); } function _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; } function _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; } /** * @fileOverview X Axis */ import * as React from 'react'; import { useLayoutEffect, useRef } from 'react'; import { clsx } from 'clsx'; import { CartesianAxis, defaultCartesianAxisProps } from './CartesianAxis'; import { useAppDispatch, useAppSelector } from '../state/hooks'; import { addXAxis, replaceXAxis, removeXAxis } from '../state/cartesianAxisSlice'; import { implicitXAxis, selectTicksOfAxis, selectXAxisPosition, selectXAxisSettingsNoDefaults, selectXAxisSize } from '../state/selectors/axisSelectors'; import { selectAxisViewBox } from '../state/selectors/selectChartOffsetInternal'; import { useIsPanorama } from '../context/PanoramaContext'; import { resolveDefaultProps } from '../util/resolveDefaultProps'; import { axisPropsAreEqual } from '../util/axisPropsAreEqual'; function SetXAxisSettings(settings) { var dispatch = useAppDispatch(); var prevSettingsRef = useRef(null); useLayoutEffect(() => { if (prevSettingsRef.current === null) { dispatch(addXAxis(settings)); } else if (prevSettingsRef.current !== settings) { dispatch(replaceXAxis({ prev: prevSettingsRef.current, next: settings })); } prevSettingsRef.current = settings; }, [settings, dispatch]); useLayoutEffect(() => { return () => { if (prevSettingsRef.current) { dispatch(removeXAxis(prevSettingsRef.current)); prevSettingsRef.current = null; } }; }, [dispatch]); return null; } var XAxisImpl = props => { var { xAxisId, className } = props; var viewBox = useAppSelector(selectAxisViewBox); var isPanorama = useIsPanorama(); var axisType = 'xAxis'; var cartesianTickItems = useAppSelector(state => selectTicksOfAxis(state, axisType, xAxisId, isPanorama)); var axisSize = useAppSelector(state => selectXAxisSize(state, xAxisId)); var position = useAppSelector(state => selectXAxisPosition(state, xAxisId)); /* * Here we select settings from the store and prefer to use them instead of the actual props * so that the chart is consistent. If we used the props directly, some components will use axis settings * from state and some from props and because there is a render step between these two, they might be showing different things. * https://github.com/recharts/recharts/issues/6257 */ var synchronizedSettings = useAppSelector(state => selectXAxisSettingsNoDefaults(state, xAxisId)); if (axisSize == null || position == null || synchronizedSettings == null) { return null; } var { dangerouslySetInnerHTML, ticks, scale: del } = props, allOtherProps = _objectWithoutProperties(props, _excluded); var { id, scale: del2 } = synchronizedSettings, restSynchronizedSettings = _objectWithoutProperties(synchronizedSettings, _excluded2); return /*#__PURE__*/React.createElement(CartesianAxis, _extends({}, allOtherProps, restSynchronizedSettings, { x: position.x, y: position.y, width: axisSize.width, height: axisSize.height, className: clsx("recharts-".concat(axisType, " ").concat(axisType), className), viewBox: viewBox, ticks: cartesianTickItems, axisType: axisType })); }; export var xAxisDefaultProps = { allowDataOverflow: implicitXAxis.allowDataOverflow, allowDecimals: implicitXAxis.allowDecimals, allowDuplicatedCategory: implicitXAxis.allowDuplicatedCategory, angle: implicitXAxis.angle, axisLine: defaultCartesianAxisProps.axisLine, height: implicitXAxis.height, hide: false, includeHidden: implicitXAxis.includeHidden, interval: implicitXAxis.interval, minTickGap: implicitXAxis.minTickGap, mirror: implicitXAxis.mirror, orientation: implicitXAxis.orientation, padding: implicitXAxis.padding, reversed: implicitXAxis.reversed, scale: implicitXAxis.scale, tick: implicitXAxis.tick, tickCount: implicitXAxis.tickCount, tickLine: defaultCartesianAxisProps.tickLine, tickSize: defaultCartesianAxisProps.tickSize, type: implicitXAxis.type, xAxisId: 0 }; var XAxisSettingsDispatcher = outsideProps => { var props = resolveDefaultProps(outsideProps, xAxisDefaultProps); return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SetXAxisSettings, { allowDataOverflow: props.allowDataOverflow, allowDecimals: props.allowDecimals, allowDuplicatedCategory: props.allowDuplicatedCategory, angle: props.angle, dataKey: props.dataKey, domain: props.domain, height: props.height, hide: props.hide, id: props.xAxisId, includeHidden: props.includeHidden, interval: props.interval, minTickGap: props.minTickGap, mirror: props.mirror, name: props.name, orientation: props.orientation, padding: props.padding, reversed: props.reversed, scale: props.scale, tick: props.tick, tickCount: props.tickCount, tickFormatter: props.tickFormatter, ticks: props.ticks, type: props.type, unit: props.unit }), /*#__PURE__*/React.createElement(XAxisImpl, props)); }; /** * @provides CartesianLabelContext */ export var XAxis = /*#__PURE__*/React.memo(XAxisSettingsDispatcher, axisPropsAreEqual); XAxis.displayName = 'XAxis';