UNPKG

recharts

Version:
187 lines (185 loc) 10.6 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.ReferenceLine = ReferenceLine; var _some2 = _interopRequireDefault(require("lodash/some")); var _isFunction2 = _interopRequireDefault(require("lodash/isFunction")); var _react = _interopRequireDefault(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _Layer = require("../container/Layer"); var _Label = require("../component/Label"); var _IfOverflowMatches = require("../util/IfOverflowMatches"); var _DataUtils = require("../util/DataUtils"); var _CartesianUtils = require("../util/CartesianUtils"); var _LogUtils = require("../util/LogUtils"); var _ReactUtils = require("../util/ReactUtils"); var _excluded = ["isFront", "ifOverflow", "xAxisId", "yAxisId", "position", "fill", "fillOpacity", "stroke", "strokeWidth"]; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } 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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } 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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } 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); } var renderLine = function renderLine(option, props) { var line; if ( /*#__PURE__*/_react["default"].isValidElement(option)) { line = /*#__PURE__*/_react["default"].cloneElement(option, props); } else if ((0, _isFunction2["default"])(option)) { line = option(props); } else { line = /*#__PURE__*/_react["default"].createElement("line", _extends({}, props, { className: "recharts-reference-line-line" })); } return line; }; // TODO: ScaleHelper var getEndPoints = function getEndPoints(scales, isFixedX, isFixedY, isSegment, props) { var _props$viewBox = props.viewBox, x = _props$viewBox.x, y = _props$viewBox.y, width = _props$viewBox.width, height = _props$viewBox.height, position = props.position; if (isFixedY) { var yCoord = props.y, orientation = props.yAxis.orientation; var coord = scales.y.apply(yCoord, { position: position }); if ((0, _IfOverflowMatches.ifOverflowMatches)(props, 'discard') && !scales.y.isInRange(coord)) { return null; } var points = [{ x: x + width, y: coord }, { x: x, y: coord }]; return orientation === 'left' ? points.reverse() : points; } if (isFixedX) { var xCoord = props.x, _orientation = props.xAxis.orientation; var _coord = scales.x.apply(xCoord, { position: position }); if ((0, _IfOverflowMatches.ifOverflowMatches)(props, 'discard') && !scales.x.isInRange(_coord)) { return null; } var _points = [{ x: _coord, y: y + height }, { x: _coord, y: y }]; return _orientation === 'top' ? _points.reverse() : _points; } if (isSegment) { var segment = props.segment; var _points2 = segment.map(function (p) { return scales.apply(p, { position: position }); }); if ((0, _IfOverflowMatches.ifOverflowMatches)(props, 'discard') && (0, _some2["default"])(_points2, function (p) { return !scales.isInRange(p); })) { return null; } return _points2; } return null; }; function ReferenceLine(_ref) { var _ref$isFront = _ref.isFront, isFront = _ref$isFront === void 0 ? false : _ref$isFront, _ref$ifOverflow = _ref.ifOverflow, ifOverflow = _ref$ifOverflow === void 0 ? 'discard' : _ref$ifOverflow, _ref$xAxisId = _ref.xAxisId, xAxisId = _ref$xAxisId === void 0 ? 0 : _ref$xAxisId, _ref$yAxisId = _ref.yAxisId, yAxisId = _ref$yAxisId === void 0 ? 0 : _ref$yAxisId, _ref$position = _ref.position, position = _ref$position === void 0 ? 'middle' : _ref$position, _ref$fill = _ref.fill, fill = _ref$fill === void 0 ? 'none' : _ref$fill, _ref$fillOpacity = _ref.fillOpacity, fillOpacity = _ref$fillOpacity === void 0 ? 1 : _ref$fillOpacity, _ref$stroke = _ref.stroke, stroke = _ref$stroke === void 0 ? '#ccc' : _ref$stroke, _ref$strokeWidth = _ref.strokeWidth, strokeWidth = _ref$strokeWidth === void 0 ? 1 : _ref$strokeWidth, restProps = _objectWithoutProperties(_ref, _excluded); // Props with default values added. var props = _objectSpread({ isFront: isFront, ifOverflow: ifOverflow, xAxisId: xAxisId, yAxisId: yAxisId, fill: fill, stroke: stroke, fillOpacity: fillOpacity, strokeWidth: strokeWidth, position: position }, restProps); var fixedX = props.x, fixedY = props.y, segment = props.segment, xAxis = props.xAxis, yAxis = props.yAxis, shape = props.shape, className = props.className, alwaysShow = props.alwaysShow, clipPathId = props.clipPathId; (0, _LogUtils.warn)(alwaysShow === undefined, 'The alwaysShow prop is deprecated. Please use ifOverflow="extendDomain" instead.'); var scales = (0, _CartesianUtils.createLabeledScales)({ x: xAxis.scale, y: yAxis.scale }); var isX = (0, _DataUtils.isNumOrStr)(fixedX); var isY = (0, _DataUtils.isNumOrStr)(fixedY); var isSegment = segment && segment.length === 2; var endPoints = getEndPoints(scales, isX, isY, isSegment, 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 = (0, _IfOverflowMatches.ifOverflowMatches)(props, 'hidden') ? "url(#".concat(clipPathId, ")") : undefined; var lineProps = _objectSpread(_objectSpread({ clipPath: clipPath }, (0, _ReactUtils.filterProps)(props, true)), {}, { x1: x1, y1: y1, x2: x2, y2: y2 }); return /*#__PURE__*/_react["default"].createElement(_Layer.Layer, { className: (0, _classnames["default"])('recharts-reference-line', className) }, renderLine(shape, lineProps), _Label.Label.renderCallByParent(props, (0, _CartesianUtils.rectWithCoords)({ x1: x1, y1: y1, x2: x2, y2: y2 }))); } ReferenceLine.displayName = 'ReferenceLine';