| 1 | var _excluded = ["x", "y"];
|
|---|
| 2 | 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; }
|
|---|
| 3 | 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; }
|
|---|
| 4 | 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; }
|
|---|
| 5 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 6 | 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); }
|
|---|
| 7 | function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|---|
| 8 | function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|---|
| 9 | import { useEffect } from 'react';
|
|---|
| 10 | import { useAppDispatch, useAppSelector } from '../state/hooks';
|
|---|
| 11 | import { selectEventEmitter, selectSyncId, selectSyncMethod } from '../state/selectors/rootPropsSelectors';
|
|---|
| 12 | import { BRUSH_SYNC_EVENT, eventCenter, TOOLTIP_SYNC_EVENT } from '../util/Events';
|
|---|
| 13 | import { createEventEmitter } from '../state/optionsSlice';
|
|---|
| 14 | import { setSyncInteraction } from '../state/tooltipSlice';
|
|---|
| 15 | import { selectTooltipDataKey } from '../state/selectors/selectors';
|
|---|
| 16 | import { selectTooltipAxisTicks } from '../state/selectors/tooltipSelectors';
|
|---|
| 17 | import { selectSynchronisedTooltipState } from './syncSelectors';
|
|---|
| 18 | import { useChartLayout, useViewBox } from '../context/chartLayoutContext';
|
|---|
| 19 | import { setDataStartEndIndexes } from '../state/chartDataSlice';
|
|---|
| 20 | import { noop } from '../util/DataUtils';
|
|---|
| 21 | function useTooltipSyncEventsListener() {
|
|---|
| 22 | var mySyncId = useAppSelector(selectSyncId);
|
|---|
| 23 | var myEventEmitter = useAppSelector(selectEventEmitter);
|
|---|
| 24 | var dispatch = useAppDispatch();
|
|---|
| 25 | var syncMethod = useAppSelector(selectSyncMethod);
|
|---|
| 26 | var tooltipTicks = useAppSelector(selectTooltipAxisTicks);
|
|---|
| 27 | var layout = useChartLayout();
|
|---|
| 28 | var viewBox = useViewBox();
|
|---|
| 29 | var className = useAppSelector(state => state.rootProps.className);
|
|---|
| 30 | useEffect(() => {
|
|---|
| 31 | if (mySyncId == null) {
|
|---|
| 32 | // This chart is not synchronised with any other chart so we don't need to listen for any events.
|
|---|
| 33 | return noop;
|
|---|
| 34 | }
|
|---|
| 35 | var listener = (incomingSyncId, action, emitter) => {
|
|---|
| 36 | if (myEventEmitter === emitter) {
|
|---|
| 37 | // We don't want to dispatch actions that we sent ourselves.
|
|---|
| 38 | return;
|
|---|
| 39 | }
|
|---|
| 40 | if (mySyncId !== incomingSyncId) {
|
|---|
| 41 | // This event is not for this chart
|
|---|
| 42 | return;
|
|---|
| 43 | }
|
|---|
| 44 | if (syncMethod === 'index') {
|
|---|
| 45 | var _action$payload;
|
|---|
| 46 | if (viewBox && action !== null && action !== void 0 && (_action$payload = action.payload) !== null && _action$payload !== void 0 && _action$payload.coordinate && action.payload.sourceViewBox) {
|
|---|
| 47 | var _action$payload$coord = action.payload.coordinate,
|
|---|
| 48 | {
|
|---|
| 49 | x: _x,
|
|---|
| 50 | y: _y
|
|---|
| 51 | } = _action$payload$coord,
|
|---|
| 52 | otherCoordinateProps = _objectWithoutProperties(_action$payload$coord, _excluded);
|
|---|
| 53 | var {
|
|---|
| 54 | x: sourceX,
|
|---|
| 55 | y: sourceY,
|
|---|
| 56 | width: sourceWidth,
|
|---|
| 57 | height: sourceHeight
|
|---|
| 58 | } = action.payload.sourceViewBox;
|
|---|
| 59 | var scaledCoordinate = _objectSpread(_objectSpread({}, otherCoordinateProps), {}, {
|
|---|
| 60 | x: viewBox.x + (sourceWidth ? (_x - sourceX) / sourceWidth : 0) * viewBox.width,
|
|---|
| 61 | y: viewBox.y + (sourceHeight ? (_y - sourceY) / sourceHeight : 0) * viewBox.height
|
|---|
| 62 | });
|
|---|
| 63 | dispatch(_objectSpread(_objectSpread({}, action), {}, {
|
|---|
| 64 | payload: _objectSpread(_objectSpread({}, action.payload), {}, {
|
|---|
| 65 | coordinate: scaledCoordinate
|
|---|
| 66 | })
|
|---|
| 67 | }));
|
|---|
| 68 | } else {
|
|---|
| 69 | dispatch(action);
|
|---|
| 70 | }
|
|---|
| 71 | return;
|
|---|
| 72 | }
|
|---|
| 73 | if (tooltipTicks == null) {
|
|---|
| 74 | // for the other two sync methods, we need the ticks to be available
|
|---|
| 75 | return;
|
|---|
| 76 | }
|
|---|
| 77 | var activeTick;
|
|---|
| 78 | if (typeof syncMethod === 'function') {
|
|---|
| 79 | /*
|
|---|
| 80 | * This is what the data shape in 2.x CategoricalChartState used to look like.
|
|---|
| 81 | * In 3.x we store things differently but let's try to keep the old shape for compatibility.
|
|---|
| 82 | */
|
|---|
| 83 | var syncMethodParam = {
|
|---|
| 84 | activeTooltipIndex: action.payload.index == null ? undefined : Number(action.payload.index),
|
|---|
| 85 | isTooltipActive: action.payload.active,
|
|---|
| 86 | activeIndex: action.payload.index == null ? undefined : Number(action.payload.index),
|
|---|
| 87 | activeLabel: action.payload.label,
|
|---|
| 88 | activeDataKey: action.payload.dataKey,
|
|---|
| 89 | activeCoordinate: action.payload.coordinate
|
|---|
| 90 | };
|
|---|
| 91 | // Call a callback function. If there is an application specific algorithm
|
|---|
| 92 | var activeTooltipIndex = syncMethod(tooltipTicks, syncMethodParam);
|
|---|
| 93 | activeTick = tooltipTicks[activeTooltipIndex];
|
|---|
| 94 | } else if (syncMethod === 'value') {
|
|---|
| 95 | // labels are always strings, tick.value might be a string or a number, depending on axis type
|
|---|
| 96 | activeTick = tooltipTicks.find(tick => String(tick.value) === action.payload.label);
|
|---|
| 97 | }
|
|---|
| 98 | var {
|
|---|
| 99 | coordinate
|
|---|
| 100 | } = action.payload;
|
|---|
| 101 | if (activeTick == null || action.payload.active === false || coordinate == null || viewBox == null) {
|
|---|
| 102 | dispatch(setSyncInteraction({
|
|---|
| 103 | active: false,
|
|---|
| 104 | coordinate: undefined,
|
|---|
| 105 | dataKey: undefined,
|
|---|
| 106 | index: null,
|
|---|
| 107 | label: undefined,
|
|---|
| 108 | sourceViewBox: undefined,
|
|---|
| 109 | graphicalItemId: undefined
|
|---|
| 110 | }));
|
|---|
| 111 | return;
|
|---|
| 112 | }
|
|---|
| 113 | var {
|
|---|
| 114 | x,
|
|---|
| 115 | y
|
|---|
| 116 | } = coordinate;
|
|---|
| 117 | var validateChartX = Math.min(x, viewBox.x + viewBox.width);
|
|---|
| 118 | var validateChartY = Math.min(y, viewBox.y + viewBox.height);
|
|---|
| 119 | var activeCoordinate = {
|
|---|
| 120 | x: layout === 'horizontal' ? activeTick.coordinate : validateChartX,
|
|---|
| 121 | y: layout === 'horizontal' ? validateChartY : activeTick.coordinate
|
|---|
| 122 | };
|
|---|
| 123 | var syncAction = setSyncInteraction({
|
|---|
| 124 | active: action.payload.active,
|
|---|
| 125 | coordinate: activeCoordinate,
|
|---|
| 126 | dataKey: action.payload.dataKey,
|
|---|
| 127 | index: String(activeTick.index),
|
|---|
| 128 | label: action.payload.label,
|
|---|
| 129 | sourceViewBox: action.payload.sourceViewBox,
|
|---|
| 130 | graphicalItemId: action.payload.graphicalItemId
|
|---|
| 131 | });
|
|---|
| 132 | dispatch(syncAction);
|
|---|
| 133 | };
|
|---|
| 134 | eventCenter.on(TOOLTIP_SYNC_EVENT, listener);
|
|---|
| 135 | return () => {
|
|---|
| 136 | eventCenter.off(TOOLTIP_SYNC_EVENT, listener);
|
|---|
| 137 | };
|
|---|
| 138 | }, [className, dispatch, myEventEmitter, mySyncId, syncMethod, tooltipTicks, layout, viewBox]);
|
|---|
| 139 | }
|
|---|
| 140 | function useBrushSyncEventsListener() {
|
|---|
| 141 | var mySyncId = useAppSelector(selectSyncId);
|
|---|
| 142 | var myEventEmitter = useAppSelector(selectEventEmitter);
|
|---|
| 143 | var dispatch = useAppDispatch();
|
|---|
| 144 | useEffect(() => {
|
|---|
| 145 | if (mySyncId == null) {
|
|---|
| 146 | // This chart is not synchronised with any other chart so we don't need to listen for any events.
|
|---|
| 147 | return noop;
|
|---|
| 148 | }
|
|---|
| 149 | var listener = (incomingSyncId, action, emitter) => {
|
|---|
| 150 | if (myEventEmitter === emitter) {
|
|---|
| 151 | // We don't want to dispatch actions that we sent ourselves.
|
|---|
| 152 | return;
|
|---|
| 153 | }
|
|---|
| 154 | if (mySyncId === incomingSyncId) {
|
|---|
| 155 | dispatch(setDataStartEndIndexes(action));
|
|---|
| 156 | }
|
|---|
| 157 | };
|
|---|
| 158 | eventCenter.on(BRUSH_SYNC_EVENT, listener);
|
|---|
| 159 | return () => {
|
|---|
| 160 | eventCenter.off(BRUSH_SYNC_EVENT, listener);
|
|---|
| 161 | };
|
|---|
| 162 | }, [dispatch, myEventEmitter, mySyncId]);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /**
|
|---|
| 166 | * Will receive synchronisation events from other charts.
|
|---|
| 167 | *
|
|---|
| 168 | * Reads syncMethod from state and decides how to synchronise the tooltip based on that.
|
|---|
| 169 | *
|
|---|
| 170 | * @returns void
|
|---|
| 171 | */
|
|---|
| 172 | export function useSynchronisedEventsFromOtherCharts() {
|
|---|
| 173 | var dispatch = useAppDispatch();
|
|---|
| 174 | useEffect(() => {
|
|---|
| 175 | dispatch(createEventEmitter());
|
|---|
| 176 | }, [dispatch]);
|
|---|
| 177 | useTooltipSyncEventsListener();
|
|---|
| 178 | useBrushSyncEventsListener();
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /**
|
|---|
| 182 | * Will send events to other charts.
|
|---|
| 183 | * If syncId is undefined, no events will be sent.
|
|---|
| 184 | *
|
|---|
| 185 | * This ignores the syncMethod, because that is set and computed on the receiving end.
|
|---|
| 186 | *
|
|---|
| 187 | * @param tooltipEventType from Tooltip
|
|---|
| 188 | * @param trigger from Tooltip
|
|---|
| 189 | * @param activeCoordinate from state
|
|---|
| 190 | * @param activeLabel from state
|
|---|
| 191 | * @param activeIndex from state
|
|---|
| 192 | * @param isTooltipActive from state
|
|---|
| 193 | * @returns void
|
|---|
| 194 | */
|
|---|
| 195 | export function useTooltipChartSynchronisation(tooltipEventType, trigger, activeCoordinate, activeLabel, activeIndex, isTooltipActive) {
|
|---|
| 196 | var activeDataKey = useAppSelector(state => selectTooltipDataKey(state, tooltipEventType, trigger));
|
|---|
| 197 | var eventEmitterSymbol = useAppSelector(selectEventEmitter);
|
|---|
| 198 | var syncId = useAppSelector(selectSyncId);
|
|---|
| 199 | var syncMethod = useAppSelector(selectSyncMethod);
|
|---|
| 200 | var tooltipState = useAppSelector(selectSynchronisedTooltipState);
|
|---|
| 201 | var isReceivingSynchronisation = tooltipState === null || tooltipState === void 0 ? void 0 : tooltipState.active;
|
|---|
| 202 | var viewBox = useViewBox();
|
|---|
| 203 | useEffect(() => {
|
|---|
| 204 | if (isReceivingSynchronisation) {
|
|---|
| 205 | /*
|
|---|
| 206 | * This chart currently has active tooltip, synchronised from another chart.
|
|---|
| 207 | * Let's not send any outgoing synchronisation events while that's happening
|
|---|
| 208 | * to avoid infinite loops.
|
|---|
| 209 | */
|
|---|
| 210 | return;
|
|---|
| 211 | }
|
|---|
| 212 | if (syncId == null) {
|
|---|
| 213 | /*
|
|---|
| 214 | * syncId is not set, means that this chart is not synchronised with any other chart,
|
|---|
| 215 | * means we don't need to send synchronisation events
|
|---|
| 216 | */
|
|---|
| 217 | return;
|
|---|
| 218 | }
|
|---|
| 219 | if (eventEmitterSymbol == null) {
|
|---|
| 220 | /*
|
|---|
| 221 | * When using Recharts internal hooks and selectors outside charts context,
|
|---|
| 222 | * these properties will be undefined. Let's return silently instead of throwing an error.
|
|---|
| 223 | */
|
|---|
| 224 | return;
|
|---|
| 225 | }
|
|---|
| 226 | var syncAction = setSyncInteraction({
|
|---|
| 227 | active: isTooltipActive,
|
|---|
| 228 | coordinate: activeCoordinate,
|
|---|
| 229 | dataKey: activeDataKey,
|
|---|
| 230 | index: activeIndex,
|
|---|
| 231 | label: typeof activeLabel === 'number' ? String(activeLabel) : activeLabel,
|
|---|
| 232 | sourceViewBox: viewBox,
|
|---|
| 233 | graphicalItemId: undefined
|
|---|
| 234 | });
|
|---|
| 235 | eventCenter.emit(TOOLTIP_SYNC_EVENT, syncId, syncAction, eventEmitterSymbol);
|
|---|
| 236 | }, [isReceivingSynchronisation, activeCoordinate, activeDataKey, activeIndex, activeLabel, eventEmitterSymbol, syncId, syncMethod, isTooltipActive, viewBox]);
|
|---|
| 237 | }
|
|---|
| 238 | export function useBrushChartSynchronisation() {
|
|---|
| 239 | var syncId = useAppSelector(selectSyncId);
|
|---|
| 240 | var eventEmitterSymbol = useAppSelector(selectEventEmitter);
|
|---|
| 241 | var brushStartIndex = useAppSelector(state => state.chartData.dataStartIndex);
|
|---|
| 242 | var brushEndIndex = useAppSelector(state => state.chartData.dataEndIndex);
|
|---|
| 243 | useEffect(() => {
|
|---|
| 244 | if (syncId == null || brushStartIndex == null || brushEndIndex == null || eventEmitterSymbol == null) {
|
|---|
| 245 | return;
|
|---|
| 246 | }
|
|---|
| 247 | var syncAction = {
|
|---|
| 248 | startIndex: brushStartIndex,
|
|---|
| 249 | endIndex: brushEndIndex
|
|---|
| 250 | };
|
|---|
| 251 | eventCenter.emit(BRUSH_SYNC_EVENT, syncId, syncAction, eventEmitterSymbol);
|
|---|
| 252 | }, [brushEndIndex, brushStartIndex, eventEmitterSymbol, syncId]);
|
|---|
| 253 | } |
|---|