| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.ReferenceArea = ReferenceArea;
|
|---|
| 7 | exports.referenceAreaDefaultProps = void 0;
|
|---|
| 8 | var _react = _interopRequireWildcard(require("react"));
|
|---|
| 9 | var React = _react;
|
|---|
| 10 | var _clsx = require("clsx");
|
|---|
| 11 | var _Layer = require("../container/Layer");
|
|---|
| 12 | var _Label = require("../component/Label");
|
|---|
| 13 | var _CartesianUtils = require("../util/CartesianUtils");
|
|---|
| 14 | var _DataUtils = require("../util/DataUtils");
|
|---|
| 15 | var _Rectangle = require("../shape/Rectangle");
|
|---|
| 16 | var _referenceElementsSlice = require("../state/referenceElementsSlice");
|
|---|
| 17 | var _hooks = require("../state/hooks");
|
|---|
| 18 | var _axisSelectors = require("../state/selectors/axisSelectors");
|
|---|
| 19 | var _PanoramaContext = require("../context/PanoramaContext");
|
|---|
| 20 | var _ClipPathProvider = require("../container/ClipPathProvider");
|
|---|
| 21 | var _svgPropertiesAndEvents = require("../util/svgPropertiesAndEvents");
|
|---|
| 22 | var _resolveDefaultProps = require("../util/resolveDefaultProps");
|
|---|
| 23 | var _ZIndexLayer = require("../zIndex/ZIndexLayer");
|
|---|
| 24 | var _DefaultZIndexes = require("../zIndex/DefaultZIndexes");
|
|---|
| 25 | var _CartesianScaleHelper = require("../util/scale/CartesianScaleHelper");
|
|---|
| 26 | 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); }
|
|---|
| 27 | 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; }
|
|---|
| 28 | 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; }
|
|---|
| 29 | 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; }
|
|---|
| 30 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 31 | 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); }
|
|---|
| 32 | 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); }
|
|---|
| 33 | /*
|
|---|
| 34 | * Omit width, height, x, y from SVGPropsAndEvents because ReferenceArea receives x1, x2, y1, y2 instead.
|
|---|
| 35 | * The position is calculated internally instead.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | var getRect = (hasX1, hasX2, hasY1, hasY2, xAxisScale, yAxisScale, props) => {
|
|---|
| 39 | var _xAxisScale$map, _yAxisScale$map, _xAxisScale$map2, _yAxisScale$map2;
|
|---|
| 40 | var {
|
|---|
| 41 | x1: xValue1,
|
|---|
| 42 | x2: xValue2,
|
|---|
| 43 | y1: yValue1,
|
|---|
| 44 | y2: yValue2
|
|---|
| 45 | } = props;
|
|---|
| 46 | if (xAxisScale == null || yAxisScale == null) {
|
|---|
| 47 | return null;
|
|---|
| 48 | }
|
|---|
| 49 | var scales = new _CartesianScaleHelper.CartesianScaleHelperImpl({
|
|---|
| 50 | x: xAxisScale,
|
|---|
| 51 | y: yAxisScale
|
|---|
| 52 | });
|
|---|
| 53 | var p1 = {
|
|---|
| 54 | x: hasX1 ? (_xAxisScale$map = xAxisScale.map(xValue1, {
|
|---|
| 55 | position: 'start'
|
|---|
| 56 | })) !== null && _xAxisScale$map !== void 0 ? _xAxisScale$map : null : xAxisScale.rangeMin(),
|
|---|
| 57 | y: hasY1 ? (_yAxisScale$map = yAxisScale.map(yValue1, {
|
|---|
| 58 | position: 'start'
|
|---|
| 59 | })) !== null && _yAxisScale$map !== void 0 ? _yAxisScale$map : null : yAxisScale.rangeMin()
|
|---|
| 60 | };
|
|---|
| 61 | var p2 = {
|
|---|
| 62 | x: hasX2 ? (_xAxisScale$map2 = xAxisScale.map(xValue2, {
|
|---|
| 63 | position: 'end'
|
|---|
| 64 | })) !== null && _xAxisScale$map2 !== void 0 ? _xAxisScale$map2 : null : xAxisScale.rangeMax(),
|
|---|
| 65 | y: hasY2 ? (_yAxisScale$map2 = yAxisScale.map(yValue2, {
|
|---|
| 66 | position: 'end'
|
|---|
| 67 | })) !== null && _yAxisScale$map2 !== void 0 ? _yAxisScale$map2 : null : yAxisScale.rangeMax()
|
|---|
| 68 | };
|
|---|
| 69 | if (props.ifOverflow === 'discard' && (!scales.isInRange(p1) || !scales.isInRange(p2))) {
|
|---|
| 70 | return null;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | // @ts-expect-error we're sending nullable coordinates but rectWithPoints expects non-nullable Coordinate
|
|---|
| 74 | return (0, _CartesianUtils.rectWithPoints)(p1, p2);
|
|---|
| 75 | };
|
|---|
| 76 | var renderRect = (option, props) => {
|
|---|
| 77 | var rect;
|
|---|
| 78 | if (/*#__PURE__*/React.isValidElement(option)) {
|
|---|
| 79 | // @ts-expect-error element cloning is not typed
|
|---|
| 80 | rect = /*#__PURE__*/React.cloneElement(option, props);
|
|---|
| 81 | } else if (typeof option === 'function') {
|
|---|
| 82 | rect = option(props);
|
|---|
| 83 | } else {
|
|---|
| 84 | rect = /*#__PURE__*/React.createElement(_Rectangle.Rectangle, _extends({}, props, {
|
|---|
| 85 | className: "recharts-reference-area-rect"
|
|---|
| 86 | }));
|
|---|
| 87 | }
|
|---|
| 88 | return rect;
|
|---|
| 89 | };
|
|---|
| 90 | function ReportReferenceArea(props) {
|
|---|
| 91 | var dispatch = (0, _hooks.useAppDispatch)();
|
|---|
| 92 | (0, _react.useEffect)(() => {
|
|---|
| 93 | dispatch((0, _referenceElementsSlice.addArea)(props));
|
|---|
| 94 | return () => {
|
|---|
| 95 | dispatch((0, _referenceElementsSlice.removeArea)(props));
|
|---|
| 96 | };
|
|---|
| 97 | });
|
|---|
| 98 | return null;
|
|---|
| 99 | }
|
|---|
| 100 | function ReferenceAreaImpl(props) {
|
|---|
| 101 | var {
|
|---|
| 102 | x1,
|
|---|
| 103 | x2,
|
|---|
| 104 | y1,
|
|---|
| 105 | y2,
|
|---|
| 106 | className,
|
|---|
| 107 | shape,
|
|---|
| 108 | xAxisId,
|
|---|
| 109 | yAxisId
|
|---|
| 110 | } = props;
|
|---|
| 111 | var clipPathId = (0, _ClipPathProvider.useClipPathId)();
|
|---|
| 112 | var isPanorama = (0, _PanoramaContext.useIsPanorama)();
|
|---|
| 113 | var xAxisScale = (0, _hooks.useAppSelector)(state => (0, _axisSelectors.selectAxisScale)(state, 'xAxis', xAxisId, isPanorama));
|
|---|
| 114 | var yAxisScale = (0, _hooks.useAppSelector)(state => (0, _axisSelectors.selectAxisScale)(state, 'yAxis', yAxisId, isPanorama));
|
|---|
| 115 | if (xAxisScale == null || yAxisScale == null) {
|
|---|
| 116 | return null;
|
|---|
| 117 | }
|
|---|
| 118 | var hasX1 = (0, _DataUtils.isNumOrStr)(x1);
|
|---|
| 119 | var hasX2 = (0, _DataUtils.isNumOrStr)(x2);
|
|---|
| 120 | var hasY1 = (0, _DataUtils.isNumOrStr)(y1);
|
|---|
| 121 | var hasY2 = (0, _DataUtils.isNumOrStr)(y2);
|
|---|
| 122 | if (!hasX1 && !hasX2 && !hasY1 && !hasY2 && !shape) {
|
|---|
| 123 | return null;
|
|---|
| 124 | }
|
|---|
| 125 | var rect = getRect(hasX1, hasX2, hasY1, hasY2, xAxisScale, yAxisScale, props);
|
|---|
| 126 | if (!rect && !shape) {
|
|---|
| 127 | return null;
|
|---|
| 128 | }
|
|---|
| 129 | var isOverflowHidden = props.ifOverflow === 'hidden';
|
|---|
| 130 | var clipPath = isOverflowHidden ? "url(#".concat(clipPathId, ")") : undefined;
|
|---|
| 131 | return /*#__PURE__*/React.createElement(_ZIndexLayer.ZIndexLayer, {
|
|---|
| 132 | zIndex: props.zIndex
|
|---|
| 133 | }, /*#__PURE__*/React.createElement(_Layer.Layer, {
|
|---|
| 134 | className: (0, _clsx.clsx)('recharts-reference-area', className)
|
|---|
| 135 | }, renderRect(shape, _objectSpread(_objectSpread({
|
|---|
| 136 | clipPath
|
|---|
| 137 | }, (0, _svgPropertiesAndEvents.svgPropertiesAndEvents)(props)), rect)), rect != null && /*#__PURE__*/React.createElement(_Label.CartesianLabelContextProvider, _extends({}, rect, {
|
|---|
| 138 | lowerWidth: rect.width,
|
|---|
| 139 | upperWidth: rect.width
|
|---|
| 140 | }), /*#__PURE__*/React.createElement(_Label.CartesianLabelFromLabelProp, {
|
|---|
| 141 | label: props.label
|
|---|
| 142 | }), props.children)));
|
|---|
| 143 | }
|
|---|
| 144 | var referenceAreaDefaultProps = exports.referenceAreaDefaultProps = {
|
|---|
| 145 | ifOverflow: 'discard',
|
|---|
| 146 | xAxisId: 0,
|
|---|
| 147 | yAxisId: 0,
|
|---|
| 148 | radius: 0,
|
|---|
| 149 | fill: '#ccc',
|
|---|
| 150 | label: false,
|
|---|
| 151 | fillOpacity: 0.5,
|
|---|
| 152 | stroke: 'none',
|
|---|
| 153 | strokeWidth: 1,
|
|---|
| 154 | zIndex: _DefaultZIndexes.DefaultZIndexes.area
|
|---|
| 155 | };
|
|---|
| 156 | /**
|
|---|
| 157 | * Draws a rectangular area on the chart to highlight a specific range.
|
|---|
| 158 | *
|
|---|
| 159 | * This component, unlike {@link Rectangle} or {@link https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/rect rect}, is aware of the cartesian coordinate system,
|
|---|
| 160 | * so you specify the area by using data coordinates instead of pixels.
|
|---|
| 161 | *
|
|---|
| 162 | * ReferenceArea will calculate the pixels based on the provided data coordinates.
|
|---|
| 163 | *
|
|---|
| 164 | * If you prefer to render rectangles using pixels rather than data coordinates,
|
|---|
| 165 | * consider using the {@link Rectangle} component instead.
|
|---|
| 166 | *
|
|---|
| 167 | * @provides CartesianLabelContext
|
|---|
| 168 | * @consumes CartesianChartContext
|
|---|
| 169 | */
|
|---|
| 170 | function ReferenceArea(outsideProps) {
|
|---|
| 171 | var props = (0, _resolveDefaultProps.resolveDefaultProps)(outsideProps, referenceAreaDefaultProps);
|
|---|
| 172 | return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ReportReferenceArea, {
|
|---|
| 173 | yAxisId: props.yAxisId,
|
|---|
| 174 | xAxisId: props.xAxisId,
|
|---|
| 175 | ifOverflow: props.ifOverflow,
|
|---|
| 176 | x1: props.x1,
|
|---|
| 177 | x2: props.x2,
|
|---|
| 178 | y1: props.y1,
|
|---|
| 179 | y2: props.y2
|
|---|
| 180 | }), /*#__PURE__*/React.createElement(ReferenceAreaImpl, props));
|
|---|
| 181 | }
|
|---|
| 182 | ReferenceArea.displayName = 'ReferenceArea'; |
|---|