UNPKG

recharts

Version:
170 lines (168 loc) 8.27 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } 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; } 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; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } 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); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _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."); } function _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); } function _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; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); } /** * @fileOverview Reference Line */ import React from 'react'; import isFunction from 'lodash/isFunction'; import some from 'lodash/some'; import clsx from 'clsx'; import { Layer } from '../container/Layer'; import { Label } from '../component/Label'; import { ifOverflowMatches } from '../util/IfOverflowMatches'; import { isNumOrStr } from '../util/DataUtils'; import { createLabeledScales, rectWithCoords } from '../util/CartesianUtils'; import { warn } from '../util/LogUtils'; import { filterProps } from '../util/ReactUtils'; import { useClipPathId, useViewBox, useXAxisOrThrow, useYAxisOrThrow } from '../context/chartLayoutContext'; /** * This excludes `viewBox` prop from svg for two reasons: * 1. The components wants viewBox of object type, and svg wants string * - so there's a conflict, and the component will throw if it gets string * 2. Internally the component calls `filterProps` which filters the viewBox away anyway */ var renderLine = function renderLine(option, props) { var line; if ( /*#__PURE__*/React.isValidElement(option)) { line = /*#__PURE__*/React.cloneElement(option, props); } else if (isFunction(option)) { line = option(props); } else { line = /*#__PURE__*/React.createElement("line", _extends({}, props, { className: "recharts-reference-line-line" })); } return line; }; // TODO: ScaleHelper export var getEndPoints = function getEndPoints(scales, isFixedX, isFixedY, isSegment, viewBox, position, xAxisOrientation, yAxisOrientation, props) { var x = viewBox.x, y = viewBox.y, width = viewBox.width, height = viewBox.height; if (isFixedY) { var yCoord = props.y; var coord = scales.y.apply(yCoord, { position: position }); if (ifOverflowMatches(props, 'discard') && !scales.y.isInRange(coord)) { return null; } var points = [{ x: x + width, y: coord }, { x: x, y: coord }]; return yAxisOrientation === 'left' ? points.reverse() : points; } if (isFixedX) { var xCoord = props.x; var _coord = scales.x.apply(xCoord, { position: position }); if (ifOverflowMatches(props, 'discard') && !scales.x.isInRange(_coord)) { return null; } var _points = [{ x: _coord, y: y + height }, { x: _coord, y: y }]; return xAxisOrientation === 'top' ? _points.reverse() : _points; } if (isSegment) { var segment = props.segment; var _points2 = segment.map(function (p) { return scales.apply(p, { position: position }); }); if (ifOverflowMatches(props, 'discard') && some(_points2, function (p) { return !scales.isInRange(p); })) { return null; } return _points2; } return null; }; export function ReferenceLine(props) { var fixedX = props.x, fixedY = props.y, segment = props.segment, xAxisId = props.xAxisId, yAxisId = props.yAxisId, shape = props.shape, className = props.className, alwaysShow = props.alwaysShow; var clipPathId = useClipPathId(); var xAxis = useXAxisOrThrow(xAxisId); var yAxis = useYAxisOrThrow(yAxisId); var viewBox = useViewBox(); if (!clipPathId || !viewBox) { return null; } warn(alwaysShow === undefined, 'The alwaysShow prop is deprecated. Please use ifOverflow="extendDomain" instead.'); var scales = createLabeledScales({ x: xAxis.scale, y: yAxis.scale }); var isX = isNumOrStr(fixedX); var isY = isNumOrStr(fixedY); var isSegment = segment && segment.length === 2; var endPoints = getEndPoints(scales, isX, isY, isSegment, viewBox, props.position, xAxis.orientation, yAxis.orientation, props); if (!endPoints) { return null; } var _endPoints = _slicedToArray(endPoints, 2), _endPoints$ = _endPoints[0], x1 = _endPoints$.x, y1 = _endPoints$.y, _endPoints$2 = _endPoints[1], x2 = _endPoints$2.x, y2 = _endPoints$2.y; var clipPath = ifOverflowMatches(props, 'hidden') ? "url(#".concat(clipPathId, ")") : undefined; var lineProps = _objectSpread(_objectSpread({ clipPath: clipPath }, filterProps(props, true)), {}, { x1: x1, y1: y1, x2: x2, y2: y2 }); return /*#__PURE__*/React.createElement(Layer, { className: clsx('recharts-reference-line', className) }, renderLine(shape, lineProps), Label.renderCallByParent(props, rectWithCoords({ x1: x1, y1: y1, x2: x2, y2: y2 }))); } ReferenceLine.displayName = 'ReferenceLine'; ReferenceLine.defaultProps = { isFront: false, ifOverflow: 'discard', xAxisId: 0, yAxisId: 0, fill: 'none', stroke: '#ccc', fillOpacity: 1, strokeWidth: 1, position: 'middle' };