UNPKG

recharts

Version:
277 lines (229 loc) 11.8 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } import _isNil from "lodash/isNil"; import _isFunction from "lodash/isFunction"; import _uniqBy from "lodash/uniqBy"; function 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; } function _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; } function _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; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } /** * @fileOverview Tooltip */ import React, { PureComponent } from 'react'; import { translateStyle } from 'react-smooth'; import classNames from 'classnames'; import { DefaultTooltipContent } from './DefaultTooltipContent'; import { Global } from '../util/Global'; import { isNumber } from '../util/DataUtils'; var CLS_PREFIX = 'recharts-tooltip-wrapper'; var EPS = 1; function defaultUniqBy(entry) { return entry.dataKey; } function getUniqPayload(option, payload) { if (option === true) { return _uniqBy(payload, defaultUniqBy); } if (_isFunction(option)) { return _uniqBy(payload, option); } return payload; } function renderContent(content, props) { if ( /*#__PURE__*/React.isValidElement(content)) { return /*#__PURE__*/React.cloneElement(content, props); } if (_isFunction(content)) { return /*#__PURE__*/React.createElement(content, props); } return /*#__PURE__*/React.createElement(DefaultTooltipContent, props); } export var Tooltip = /*#__PURE__*/function (_PureComponent) { _inherits(Tooltip, _PureComponent); var _super = _createSuper(Tooltip); function Tooltip() { var _this; _classCallCheck(this, Tooltip); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _this.state = { boxWidth: -1, boxHeight: -1 }; _this.wrapperNode = void 0; _this.getTranslate = function (_ref) { var key = _ref.key, tooltipDimension = _ref.tooltipDimension, viewBoxDimension = _ref.viewBoxDimension; var _this$props = _this.props, allowEscapeViewBox = _this$props.allowEscapeViewBox, coordinate = _this$props.coordinate, offset = _this$props.offset, position = _this$props.position, viewBox = _this$props.viewBox; if (position && isNumber(position[key])) { return position[key]; } var restricted = coordinate[key] - tooltipDimension - offset; var unrestricted = coordinate[key] + offset; if (allowEscapeViewBox[key]) { return unrestricted; } var tooltipBoundary = coordinate[key] + tooltipDimension + offset; var viewBoxBoundary = viewBox[key] + viewBoxDimension; if (tooltipBoundary > viewBoxBoundary) { return Math.max(restricted, viewBox[key]); } return Math.max(unrestricted, viewBox[key]); }; return _this; } _createClass(Tooltip, [{ key: "componentDidMount", value: function componentDidMount() { this.updateBBox(); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this.updateBBox(); } }, { key: "updateBBox", value: function updateBBox() { var _this$state = this.state, boxWidth = _this$state.boxWidth, boxHeight = _this$state.boxHeight; if (this.wrapperNode && this.wrapperNode.getBoundingClientRect) { var box = this.wrapperNode.getBoundingClientRect(); if (Math.abs(box.width - boxWidth) > EPS || Math.abs(box.height - boxHeight) > EPS) { this.setState({ boxWidth: box.width, boxHeight: box.height }); } } else if (boxWidth !== -1 || boxHeight !== -1) { this.setState({ boxWidth: -1, boxHeight: -1 }); } } }, { key: "render", value: function render() { var _classNames, _this2 = this; var _this$props2 = this.props, payload = _this$props2.payload, isAnimationActive = _this$props2.isAnimationActive, animationDuration = _this$props2.animationDuration, animationEasing = _this$props2.animationEasing, filterNull = _this$props2.filterNull, payloadUniqBy = _this$props2.payloadUniqBy; var finalPayload = getUniqPayload(payloadUniqBy, filterNull && payload && payload.length ? payload.filter(function (entry) { return !_isNil(entry.value); }) : payload); var hasPayload = finalPayload && finalPayload.length; var _this$props3 = this.props, content = _this$props3.content, viewBox = _this$props3.viewBox, coordinate = _this$props3.coordinate, position = _this$props3.position, active = _this$props3.active, wrapperStyle = _this$props3.wrapperStyle; var outerStyle = _objectSpread({ pointerEvents: 'none', visibility: active && hasPayload ? 'visible' : 'hidden', position: 'absolute', top: 0, left: 0 }, wrapperStyle); var translateX, translateY; if (position && isNumber(position.x) && isNumber(position.y)) { translateX = position.x; translateY = position.y; } else { var _this$state2 = this.state, boxWidth = _this$state2.boxWidth, boxHeight = _this$state2.boxHeight; if (boxWidth > 0 && boxHeight > 0 && coordinate) { translateX = this.getTranslate({ key: 'x', tooltipDimension: boxWidth, viewBoxDimension: viewBox.width }); translateY = this.getTranslate({ key: 'y', tooltipDimension: boxHeight, viewBoxDimension: viewBox.height }); } else { outerStyle.visibility = 'hidden'; } } outerStyle = _objectSpread(_objectSpread({}, translateStyle({ transform: this.props.useTranslate3d ? "translate3d(".concat(translateX, "px, ").concat(translateY, "px, 0)") : "translate(".concat(translateX, "px, ").concat(translateY, "px)") })), outerStyle); if (isAnimationActive && active) { outerStyle = _objectSpread(_objectSpread({}, translateStyle({ transition: "transform ".concat(animationDuration, "ms ").concat(animationEasing) })), outerStyle); } var cls = classNames(CLS_PREFIX, (_classNames = {}, _defineProperty(_classNames, "".concat(CLS_PREFIX, "-right"), isNumber(translateX) && coordinate && isNumber(coordinate.x) && translateX >= coordinate.x), _defineProperty(_classNames, "".concat(CLS_PREFIX, "-left"), isNumber(translateX) && coordinate && isNumber(coordinate.x) && translateX < coordinate.x), _defineProperty(_classNames, "".concat(CLS_PREFIX, "-bottom"), isNumber(translateY) && coordinate && isNumber(coordinate.y) && translateY >= coordinate.y), _defineProperty(_classNames, "".concat(CLS_PREFIX, "-top"), isNumber(translateY) && coordinate && isNumber(coordinate.y) && translateY < coordinate.y), _classNames)); return /*#__PURE__*/React.createElement("div", { className: cls, style: outerStyle, ref: function ref(node) { _this2.wrapperNode = node; } }, renderContent(content, _objectSpread(_objectSpread({}, this.props), {}, { payload: finalPayload }))); } }]); return Tooltip; }(PureComponent); Tooltip.displayName = 'Tooltip'; Tooltip.defaultProps = { active: false, allowEscapeViewBox: { x: false, y: false }, offset: 10, viewBox: { x1: 0, x2: 0, y1: 0, y2: 0 }, coordinate: { x: 0, y: 0 }, cursorStyle: {}, separator: ' : ', wrapperStyle: {}, contentStyle: {}, itemStyle: {}, labelStyle: {}, cursor: true, trigger: 'hover', isAnimationActive: !Global.isSsr, animationEasing: 'ease', animationDuration: 400, filterNull: true, useTranslate3d: false };