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