| 1 | 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; }
|
|---|
| 2 | 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; }
|
|---|
| 3 | 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; }
|
|---|
| 4 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 5 | 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); }
|
|---|
| 6 | import * as React from 'react';
|
|---|
| 7 | import { useEffect } from 'react';
|
|---|
| 8 | import { createPortal } from 'react-dom';
|
|---|
| 9 | import { DefaultTooltipContent } from './DefaultTooltipContent';
|
|---|
| 10 | import { TooltipBoundingBox } from './TooltipBoundingBox';
|
|---|
| 11 | import { getUniqPayload } from '../util/payload/getUniqPayload';
|
|---|
| 12 | import { useViewBox } from '../context/chartLayoutContext';
|
|---|
| 13 | import { useAccessibilityLayer } from '../context/accessibilityContext';
|
|---|
| 14 | import { useElementOffset } from '../util/useElementOffset';
|
|---|
| 15 | import { Cursor } from './Cursor';
|
|---|
| 16 | import { selectActiveCoordinate, selectActiveLabel, selectIsTooltipActive, selectTooltipPayload } from '../state/selectors/selectors';
|
|---|
| 17 | import { useTooltipPortal } from '../context/tooltipPortalContext';
|
|---|
| 18 | import { useAppDispatch, useAppSelector } from '../state/hooks';
|
|---|
| 19 | import { setTooltipSettingsState } from '../state/tooltipSlice';
|
|---|
| 20 | import { useTooltipChartSynchronisation } from '../synchronisation/useChartSynchronisation';
|
|---|
| 21 | import { useTooltipEventType } from '../state/selectors/selectTooltipEventType';
|
|---|
| 22 | import { resolveDefaultProps } from '../util/resolveDefaultProps';
|
|---|
| 23 | function defaultUniqBy(entry) {
|
|---|
| 24 | return entry.dataKey;
|
|---|
| 25 | }
|
|---|
| 26 | function renderContent(content, props) {
|
|---|
| 27 | if (/*#__PURE__*/React.isValidElement(content)) {
|
|---|
| 28 | return /*#__PURE__*/React.cloneElement(content, props);
|
|---|
| 29 | }
|
|---|
| 30 | if (typeof content === 'function') {
|
|---|
| 31 | return /*#__PURE__*/React.createElement(content, props);
|
|---|
| 32 | }
|
|---|
| 33 | return /*#__PURE__*/React.createElement(DefaultTooltipContent, props);
|
|---|
| 34 | }
|
|---|
| 35 | var emptyPayload = [];
|
|---|
| 36 | export var defaultTooltipProps = {
|
|---|
| 37 | allowEscapeViewBox: {
|
|---|
| 38 | x: false,
|
|---|
| 39 | y: false
|
|---|
| 40 | },
|
|---|
| 41 | animationDuration: 400,
|
|---|
| 42 | animationEasing: 'ease',
|
|---|
| 43 | axisId: 0,
|
|---|
| 44 | contentStyle: {},
|
|---|
| 45 | cursor: true,
|
|---|
| 46 | filterNull: true,
|
|---|
| 47 | includeHidden: false,
|
|---|
| 48 | isAnimationActive: 'auto',
|
|---|
| 49 | itemSorter: 'name',
|
|---|
| 50 | itemStyle: {},
|
|---|
| 51 | labelStyle: {},
|
|---|
| 52 | offset: 10,
|
|---|
| 53 | reverseDirection: {
|
|---|
| 54 | x: false,
|
|---|
| 55 | y: false
|
|---|
| 56 | },
|
|---|
| 57 | separator: ' : ',
|
|---|
| 58 | trigger: 'hover',
|
|---|
| 59 | useTranslate3d: false,
|
|---|
| 60 | wrapperStyle: {}
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * The Tooltip component displays a floating box with data values when hovering over or clicking on chart elements.
|
|---|
| 65 | *
|
|---|
| 66 | * It can be configured to show information for individual data points or for all points at a specific axis coordinate.
|
|---|
| 67 | * The appearance and content of the tooltip can be customized via props.
|
|---|
| 68 | *
|
|---|
| 69 | * @see {@link https://github.com/recharts/recharts/wiki/Tooltip-event-type-and-shared-prop Tooltip event type and shared prop wiki page}
|
|---|
| 70 | * @see {@link https://recharts.github.io/en-US/guide/activeIndex/ Active index replacement when migrating from Recharts v2 to v3}
|
|---|
| 71 | *
|
|---|
| 72 | * @consumes CartesianChartContext
|
|---|
| 73 | * @consumes PolarChartContext
|
|---|
| 74 | * @consumes TooltipEntrySettings
|
|---|
| 75 | */
|
|---|
| 76 | export function Tooltip(outsideProps) {
|
|---|
| 77 | var _useAppSelector, _ref;
|
|---|
| 78 | var props = resolveDefaultProps(outsideProps, defaultTooltipProps);
|
|---|
| 79 | var {
|
|---|
| 80 | active: activeFromProps,
|
|---|
| 81 | allowEscapeViewBox,
|
|---|
| 82 | animationDuration,
|
|---|
| 83 | animationEasing,
|
|---|
| 84 | content,
|
|---|
| 85 | filterNull,
|
|---|
| 86 | isAnimationActive,
|
|---|
| 87 | offset,
|
|---|
| 88 | payloadUniqBy,
|
|---|
| 89 | position,
|
|---|
| 90 | reverseDirection,
|
|---|
| 91 | useTranslate3d,
|
|---|
| 92 | wrapperStyle,
|
|---|
| 93 | cursor,
|
|---|
| 94 | shared,
|
|---|
| 95 | trigger,
|
|---|
| 96 | defaultIndex,
|
|---|
| 97 | portal: portalFromProps,
|
|---|
| 98 | axisId
|
|---|
| 99 | } = props;
|
|---|
| 100 | var dispatch = useAppDispatch();
|
|---|
| 101 | var defaultIndexAsString = typeof defaultIndex === 'number' ? String(defaultIndex) : defaultIndex;
|
|---|
| 102 | useEffect(() => {
|
|---|
| 103 | dispatch(setTooltipSettingsState({
|
|---|
| 104 | shared,
|
|---|
| 105 | trigger,
|
|---|
| 106 | axisId,
|
|---|
| 107 | active: activeFromProps,
|
|---|
| 108 | defaultIndex: defaultIndexAsString
|
|---|
| 109 | }));
|
|---|
| 110 | }, [dispatch, shared, trigger, axisId, activeFromProps, defaultIndexAsString]);
|
|---|
| 111 | var viewBox = useViewBox();
|
|---|
| 112 | var accessibilityLayer = useAccessibilityLayer();
|
|---|
| 113 | var tooltipEventType = useTooltipEventType(shared);
|
|---|
| 114 | var {
|
|---|
| 115 | activeIndex,
|
|---|
| 116 | isActive
|
|---|
| 117 | } = (_useAppSelector = useAppSelector(state => selectIsTooltipActive(state, tooltipEventType, trigger, defaultIndexAsString))) !== null && _useAppSelector !== void 0 ? _useAppSelector : {};
|
|---|
| 118 | var payloadFromRedux = useAppSelector(state => selectTooltipPayload(state, tooltipEventType, trigger, defaultIndexAsString));
|
|---|
| 119 | var labelFromRedux = useAppSelector(state => selectActiveLabel(state, tooltipEventType, trigger, defaultIndexAsString));
|
|---|
| 120 | var coordinate = useAppSelector(state => selectActiveCoordinate(state, tooltipEventType, trigger, defaultIndexAsString));
|
|---|
| 121 | var payload = payloadFromRedux;
|
|---|
| 122 | var tooltipPortalFromContext = useTooltipPortal();
|
|---|
| 123 | /*
|
|---|
| 124 | * The user can set `active=true` on the Tooltip in which case the Tooltip will stay always active,
|
|---|
| 125 | * or `active=false` in which case the Tooltip never shows.
|
|---|
| 126 | *
|
|---|
| 127 | * If the `active` prop is not defined then it will show and hide based on mouse or keyboard activity.
|
|---|
| 128 | */
|
|---|
| 129 | var finalIsActive = (_ref = activeFromProps !== null && activeFromProps !== void 0 ? activeFromProps : isActive) !== null && _ref !== void 0 ? _ref : false;
|
|---|
| 130 | var [lastBoundingBox, updateBoundingBox] = useElementOffset([payload, finalIsActive]);
|
|---|
| 131 | var finalLabel = tooltipEventType === 'axis' ? labelFromRedux : undefined;
|
|---|
| 132 | useTooltipChartSynchronisation(tooltipEventType, trigger, coordinate, finalLabel, activeIndex, finalIsActive);
|
|---|
| 133 | var tooltipPortal = portalFromProps !== null && portalFromProps !== void 0 ? portalFromProps : tooltipPortalFromContext;
|
|---|
| 134 | if (tooltipPortal == null || viewBox == null || tooltipEventType == null) {
|
|---|
| 135 | return null;
|
|---|
| 136 | }
|
|---|
| 137 | var finalPayload = payload !== null && payload !== void 0 ? payload : emptyPayload;
|
|---|
| 138 | if (!finalIsActive) {
|
|---|
| 139 | finalPayload = emptyPayload;
|
|---|
| 140 | }
|
|---|
| 141 | if (filterNull && finalPayload.length) {
|
|---|
| 142 | finalPayload = getUniqPayload(finalPayload.filter(entry => entry.value != null && (entry.hide !== true || props.includeHidden)), payloadUniqBy, defaultUniqBy);
|
|---|
| 143 | }
|
|---|
| 144 | var hasPayload = finalPayload.length > 0;
|
|---|
| 145 | var tooltipElement = /*#__PURE__*/React.createElement(TooltipBoundingBox, {
|
|---|
| 146 | allowEscapeViewBox: allowEscapeViewBox,
|
|---|
| 147 | animationDuration: animationDuration,
|
|---|
| 148 | animationEasing: animationEasing,
|
|---|
| 149 | isAnimationActive: isAnimationActive,
|
|---|
| 150 | active: finalIsActive,
|
|---|
| 151 | coordinate: coordinate,
|
|---|
| 152 | hasPayload: hasPayload,
|
|---|
| 153 | offset: offset,
|
|---|
| 154 | position: position,
|
|---|
| 155 | reverseDirection: reverseDirection,
|
|---|
| 156 | useTranslate3d: useTranslate3d,
|
|---|
| 157 | viewBox: viewBox,
|
|---|
| 158 | wrapperStyle: wrapperStyle,
|
|---|
| 159 | lastBoundingBox: lastBoundingBox,
|
|---|
| 160 | innerRef: updateBoundingBox,
|
|---|
| 161 | hasPortalFromProps: Boolean(portalFromProps)
|
|---|
| 162 | }, renderContent(content, _objectSpread(_objectSpread({}, props), {}, {
|
|---|
| 163 | payload: finalPayload,
|
|---|
| 164 | label: finalLabel,
|
|---|
| 165 | active: finalIsActive,
|
|---|
| 166 | activeIndex,
|
|---|
| 167 | coordinate,
|
|---|
| 168 | accessibilityLayer
|
|---|
| 169 | })));
|
|---|
| 170 | return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/createPortal(tooltipElement, tooltipPortal), finalIsActive && /*#__PURE__*/React.createElement(Cursor, {
|
|---|
| 171 | cursor: cursor,
|
|---|
| 172 | tooltipEventType: tooltipEventType,
|
|---|
| 173 | coordinate: coordinate,
|
|---|
| 174 | payload: finalPayload,
|
|---|
| 175 | index: activeIndex
|
|---|
| 176 | }));
|
|---|
| 177 | } |
|---|