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