source: node_modules/recharts/es6/cartesian/ReferenceArea.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

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