UNPKG

7.17 kBJavaScriptView Raw
1import _some from "lodash/some";
2import _isFunction from "lodash/isFunction";
3
4function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
5
6function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
7
8function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9
10function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
11
12function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
13
14function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
15
16function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
17
18function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
19
20function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
21
22function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
23
24/**
25 * @fileOverview Reference Line
26 */
27import React from 'react';
28import classNames from 'classnames';
29import { Layer } from '../container/Layer';
30import { Label } from '../component/Label';
31import { ifOverflowMatches } from '../util/IfOverflowMatches';
32import { isNumOrStr } from '../util/DataUtils';
33import { createLabeledScales, rectWithCoords } from '../util/CartesianUtils';
34import { warn } from '../util/LogUtils';
35import { filterProps } from '../util/types';
36
37var renderLine = function renderLine(option, props) {
38 var line;
39
40 if ( /*#__PURE__*/React.isValidElement(option)) {
41 line = /*#__PURE__*/React.cloneElement(option, props);
42 } else if (_isFunction(option)) {
43 line = option(props);
44 } else {
45 line = /*#__PURE__*/React.createElement("line", _extends({}, props, {
46 className: "recharts-reference-line-line"
47 }));
48 }
49
50 return line;
51}; // TODO: ScaleHelper
52
53
54var getEndPoints = function getEndPoints(scales, isFixedX, isFixedY, isSegment, props) {
55 var _props$viewBox = props.viewBox,
56 x = _props$viewBox.x,
57 y = _props$viewBox.y,
58 width = _props$viewBox.width,
59 height = _props$viewBox.height,
60 position = props.position;
61
62 if (isFixedY) {
63 var yCoord = props.y,
64 orientation = props.yAxis.orientation;
65 var coord = scales.y.apply(yCoord, {
66 position: position
67 });
68
69 if (ifOverflowMatches(props, 'discard') && !scales.y.isInRange(coord)) {
70 return null;
71 }
72
73 var points = [{
74 x: x + width,
75 y: coord
76 }, {
77 x: x,
78 y: coord
79 }];
80 return orientation === 'left' ? points.reverse() : points;
81 }
82
83 if (isFixedX) {
84 var xCoord = props.x,
85 _orientation = props.xAxis.orientation;
86
87 var _coord = scales.x.apply(xCoord, {
88 position: position
89 });
90
91 if (ifOverflowMatches(props, 'discard') && !scales.x.isInRange(_coord)) {
92 return null;
93 }
94
95 var _points = [{
96 x: _coord,
97 y: y + height
98 }, {
99 x: _coord,
100 y: y
101 }];
102 return _orientation === 'top' ? _points.reverse() : _points;
103 }
104
105 if (isSegment) {
106 var segment = props.segment;
107
108 var _points2 = segment.map(function (p) {
109 return scales.apply(p, {
110 position: position
111 });
112 });
113
114 if (ifOverflowMatches(props, 'discard') && _some(_points2, function (p) {
115 return !scales.isInRange(p);
116 })) {
117 return null;
118 }
119
120 return _points2;
121 }
122
123 return null;
124};
125
126export function ReferenceLine(props) {
127 var fixedX = props.x,
128 fixedY = props.y,
129 segment = props.segment,
130 xAxis = props.xAxis,
131 yAxis = props.yAxis,
132 shape = props.shape,
133 className = props.className,
134 alwaysShow = props.alwaysShow,
135 clipPathId = props.clipPathId;
136 warn(alwaysShow === undefined, 'The alwaysShow prop is deprecated. Please use ifOverflow="extendDomain" instead.');
137 var scales = createLabeledScales({
138 x: xAxis.scale,
139 y: yAxis.scale
140 });
141 var isX = isNumOrStr(fixedX);
142 var isY = isNumOrStr(fixedY);
143 var isSegment = segment && segment.length === 2;
144 var endPoints = getEndPoints(scales, isX, isY, isSegment, props);
145
146 if (!endPoints) {
147 return null;
148 }
149
150 var _endPoints = _slicedToArray(endPoints, 2),
151 _endPoints$ = _endPoints[0],
152 x1 = _endPoints$.x,
153 y1 = _endPoints$.y,
154 _endPoints$2 = _endPoints[1],
155 x2 = _endPoints$2.x,
156 y2 = _endPoints$2.y;
157
158 var clipPath = ifOverflowMatches(props, 'hidden') ? "url(#".concat(clipPathId, ")") : undefined;
159
160 var lineProps = _objectSpread(_objectSpread({
161 clipPath: clipPath
162 }, filterProps(props, true)), {}, {
163 x1: x1,
164 y1: y1,
165 x2: x2,
166 y2: y2
167 });
168
169 return /*#__PURE__*/React.createElement(Layer, {
170 className: classNames('recharts-reference-line', className)
171 }, renderLine(shape, lineProps), Label.renderCallByParent(props, rectWithCoords({
172 x1: x1,
173 y1: y1,
174 x2: x2,
175 y2: y2
176 })));
177}
178ReferenceLine.displayName = 'ReferenceLine';
179ReferenceLine.defaultProps = {
180 isFront: false,
181 ifOverflow: 'discard',
182 xAxisId: 0,
183 yAxisId: 0,
184 fill: 'none',
185 stroke: '#ccc',
186 fillOpacity: 1,
187 strokeWidth: 1,
188 position: 'middle'
189};
\No newline at end of file