source: node_modules/recharts/es6/cartesian/ReferenceLine.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: 9.7 KB
Line 
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); }
7/**
8 * @fileOverview Reference Line
9 */
10import * as React from 'react';
11import { useEffect } from 'react';
12import { clsx } from 'clsx';
13import { Layer } from '../container/Layer';
14import { CartesianLabelContextProvider, CartesianLabelFromLabelProp } from '../component/Label';
15import { isNumOrStr } from '../util/DataUtils';
16import { rectWithCoords } from '../util/CartesianUtils';
17import { useViewBox } from '../context/chartLayoutContext';
18import { addLine, removeLine } from '../state/referenceElementsSlice';
19import { useAppDispatch, useAppSelector } from '../state/hooks';
20import { selectAxisScale, selectXAxisSettings, selectYAxisSettings } from '../state/selectors/axisSelectors';
21import { useIsPanorama } from '../context/PanoramaContext';
22import { useClipPathId } from '../container/ClipPathProvider';
23import { svgPropertiesAndEvents } from '../util/svgPropertiesAndEvents';
24import { resolveDefaultProps } from '../util/resolveDefaultProps';
25import { ZIndexLayer } from '../zIndex/ZIndexLayer';
26import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
27import { isWellBehavedNumber } from '../util/isWellBehavedNumber';
28import { CartesianScaleHelperImpl } from '../util/scale/CartesianScaleHelper';
29
30/**
31 * Single point that defines one end of a segment.
32 * These coordinates are in data space, meaning that you should provide
33 * values that correspond to the data domain of the axes.
34 * So you would provide a value of `Page A` to indicate the data value `Page A`
35 * and then recharts will convert that to pixels.
36 *
37 * Likewise for numbers. If your x-axis goes from 0 to 100,
38 * and you want the line to end at 50, you would provide `50` here.
39 *
40 * @inline
41 */
42
43/**
44 * This excludes `viewBox` prop from svg for two reasons:
45 * 1. The components wants viewBox of object type, and svg wants string
46 * - so there's a conflict, and the component will throw if it gets string
47 * 2. Internally the component calls `svgPropertiesNoEvents` which filters the viewBox away anyway
48 */
49
50var renderLine = (option, props) => {
51 var line;
52 if (/*#__PURE__*/React.isValidElement(option)) {
53 // @ts-expect-error element cloning is not typed
54 line = /*#__PURE__*/React.cloneElement(option, props);
55 } else if (typeof option === 'function') {
56 line = option(props);
57 } else {
58 if (!isWellBehavedNumber(props.x1) || !isWellBehavedNumber(props.y1) || !isWellBehavedNumber(props.x2) || !isWellBehavedNumber(props.y2)) {
59 return null;
60 }
61 line = /*#__PURE__*/React.createElement("line", _extends({}, props, {
62 className: "recharts-reference-line-line"
63 }));
64 }
65 return line;
66};
67var getHorizontalLineEndPoints = (yCoord, ifOverflow, position, yAxisOrientation, yAxisScale, viewBox) => {
68 var {
69 x,
70 width
71 } = viewBox;
72 var coord = yAxisScale.map(yCoord, {
73 position
74 });
75 // don't render the line if the scale can't compute a result that makes sense
76 if (!isWellBehavedNumber(coord)) {
77 return null;
78 }
79 if (ifOverflow === 'discard' && !yAxisScale.isInRange(coord)) {
80 return null;
81 }
82 var points = [{
83 x: x + width,
84 y: coord
85 }, {
86 x,
87 y: coord
88 }];
89 return yAxisOrientation === 'left' ? points.reverse() : points;
90};
91var getVerticalLineEndPoints = (xCoord, ifOverflow, position, xAxisOrientation, xAxisScale, viewBox) => {
92 var {
93 y,
94 height
95 } = viewBox;
96 var coord = xAxisScale.map(xCoord, {
97 position
98 });
99 // don't render the line if the scale can't compute a result that makes sense
100 if (!isWellBehavedNumber(coord)) {
101 return null;
102 }
103 if (ifOverflow === 'discard' && !xAxisScale.isInRange(coord)) {
104 return null;
105 }
106 var points = [{
107 x: coord,
108 y: y + height
109 }, {
110 x: coord,
111 y
112 }];
113 return xAxisOrientation === 'top' ? points.reverse() : points;
114};
115var getSegmentLineEndPoints = (segment, ifOverflow, position, scales) => {
116 var points = [scales.mapWithFallback(segment[0], {
117 position,
118 fallback: 'rangeMin'
119 }), scales.mapWithFallback(segment[1], {
120 position,
121 fallback: 'rangeMax'
122 })];
123 if (ifOverflow === 'discard' && points.some(p => !scales.isInRange(p))) {
124 return null;
125 }
126 return points;
127};
128export var getEndPoints = (xAxisScale, yAxisScale, viewBox, position, xAxisOrientation, yAxisOrientation, props) => {
129 var {
130 x: xCoord,
131 y: yCoord,
132 segment,
133 ifOverflow
134 } = props;
135 var isFixedX = isNumOrStr(xCoord);
136 var isFixedY = isNumOrStr(yCoord);
137 if (isFixedY) {
138 return getHorizontalLineEndPoints(yCoord, ifOverflow, position, yAxisOrientation, yAxisScale, viewBox);
139 }
140 if (isFixedX) {
141 return getVerticalLineEndPoints(xCoord, ifOverflow, position, xAxisOrientation, xAxisScale, viewBox);
142 }
143 if (segment != null && segment.length === 2) {
144 return getSegmentLineEndPoints(segment, ifOverflow, position, new CartesianScaleHelperImpl({
145 x: xAxisScale,
146 y: yAxisScale
147 }));
148 }
149 return null;
150};
151function ReportReferenceLine(props) {
152 var dispatch = useAppDispatch();
153 useEffect(() => {
154 dispatch(addLine(props));
155 return () => {
156 dispatch(removeLine(props));
157 };
158 });
159 return null;
160}
161function ReferenceLineImpl(props) {
162 var {
163 xAxisId,
164 yAxisId,
165 shape,
166 className,
167 ifOverflow
168 } = props;
169 var isPanorama = useIsPanorama();
170 var clipPathId = useClipPathId();
171 var xAxis = useAppSelector(state => selectXAxisSettings(state, xAxisId));
172 var yAxis = useAppSelector(state => selectYAxisSettings(state, yAxisId));
173 var xAxisScale = useAppSelector(state => selectAxisScale(state, 'xAxis', xAxisId, isPanorama));
174 var yAxisScale = useAppSelector(state => selectAxisScale(state, 'yAxis', yAxisId, isPanorama));
175 var viewBox = useViewBox();
176 if (!clipPathId || !viewBox || xAxis == null || yAxis == null || xAxisScale == null || yAxisScale == null) {
177 return null;
178 }
179 var endPoints = getEndPoints(xAxisScale, yAxisScale, viewBox, props.position, xAxis.orientation, yAxis.orientation, props);
180 if (!endPoints) {
181 return null;
182 }
183 var point1 = endPoints[0];
184 var point2 = endPoints[1];
185 if (point1 == null || point2 == null) {
186 return null;
187 }
188 var {
189 x: x1,
190 y: y1
191 } = point1;
192 var {
193 x: x2,
194 y: y2
195 } = point2;
196 var clipPath = ifOverflow === 'hidden' ? "url(#".concat(clipPathId, ")") : undefined;
197 var lineProps = _objectSpread(_objectSpread({
198 clipPath
199 }, svgPropertiesAndEvents(props)), {}, {
200 x1,
201 y1,
202 x2,
203 y2
204 });
205 var rect = rectWithCoords({
206 x1,
207 y1,
208 x2,
209 y2
210 });
211 return /*#__PURE__*/React.createElement(ZIndexLayer, {
212 zIndex: props.zIndex
213 }, /*#__PURE__*/React.createElement(Layer, {
214 className: clsx('recharts-reference-line', className)
215 }, renderLine(shape, lineProps), /*#__PURE__*/React.createElement(CartesianLabelContextProvider, _extends({}, rect, {
216 lowerWidth: rect.width,
217 upperWidth: rect.width
218 }), /*#__PURE__*/React.createElement(CartesianLabelFromLabelProp, {
219 label: props.label
220 }), props.children)));
221}
222export var referenceLineDefaultProps = {
223 ifOverflow: 'discard',
224 xAxisId: 0,
225 yAxisId: 0,
226 fill: 'none',
227 label: false,
228 stroke: '#ccc',
229 fillOpacity: 1,
230 strokeWidth: 1,
231 position: 'middle',
232 zIndex: DefaultZIndexes.line
233};
234/**
235 * Draws a line on the chart connecting two points.
236 *
237 * This component, unlike {@link https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/line line}, is aware of the cartesian coordinate system,
238 * so you specify the dimensions by using data coordinates instead of pixels.
239 *
240 * ReferenceLine will calculate the pixels based on the provided data coordinates.
241 *
242 * If you prefer to render using pixels rather than data coordinates,
243 * consider using the {@link https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/line line SVG element} instead.
244 *
245 * @provides CartesianLabelContext
246 * @consumes CartesianChartContext
247 */
248export function ReferenceLine(outsideProps) {
249 var props = resolveDefaultProps(outsideProps, referenceLineDefaultProps);
250 return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ReportReferenceLine, {
251 yAxisId: props.yAxisId,
252 xAxisId: props.xAxisId,
253 ifOverflow: props.ifOverflow,
254 x: props.x,
255 y: props.y,
256 segment: props.segment
257 }), /*#__PURE__*/React.createElement(ReferenceLineImpl, props));
258}
259ReferenceLine.displayName = 'ReferenceLine';
Note: See TracBrowser for help on using the repository browser.