|
|
import { useEffect } from 'react'; |
|
|
import { PayloadAction } from '@reduxjs/toolkit'; |
|
|
import { useAppDispatch, useAppSelector } from '../state/hooks'; |
|
|
import { selectEventEmitter, selectSyncId, selectSyncMethod } from '../state/selectors/rootPropsSelectors'; |
|
|
import { BRUSH_SYNC_EVENT, eventCenter, TOOLTIP_SYNC_EVENT } from '../util/Events'; |
|
|
import { createEventEmitter } from '../state/optionsSlice'; |
|
|
import { setSyncInteraction, TooltipIndex, TooltipSyncState } from '../state/tooltipSlice'; |
|
|
import { selectTooltipDataKey } from '../state/selectors/selectors'; |
|
|
import { ChartCoordinate, Coordinate, TickItem, TooltipEventType } from '../util/types'; |
|
|
import { TooltipTrigger } from '../chart/types'; |
|
|
import { selectTooltipAxisTicks } from '../state/selectors/tooltipSelectors'; |
|
|
import { selectSynchronisedTooltipState } from './syncSelectors'; |
|
|
import { useChartLayout, useViewBox } from '../context/chartLayoutContext'; |
|
|
import { BrushStartEndIndex } from '../context/brushUpdateContext'; |
|
|
import { setDataStartEndIndexes } from '../state/chartDataSlice'; |
|
|
import { MouseHandlerDataParam } from './types'; |
|
|
|
|
|
const noop = () => {}; |
|
|
|
|
|
function useTooltipSyncEventsListener() { |
|
|
const mySyncId = useAppSelector(selectSyncId); |
|
|
const myEventEmitter = useAppSelector(selectEventEmitter); |
|
|
const dispatch = useAppDispatch(); |
|
|
const syncMethod = useAppSelector(selectSyncMethod); |
|
|
const tooltipTicks = useAppSelector(selectTooltipAxisTicks); |
|
|
const layout = useChartLayout(); |
|
|
const viewBox = useViewBox(); |
|
|
|
|
|
const className = useAppSelector(state => state.rootProps.className); |
|
|
useEffect(() => { |
|
|
if (mySyncId == null) { |
|
|
|
|
|
return noop; |
|
|
} |
|
|
|
|
|
const listener = (incomingSyncId: number | string, action: PayloadAction<TooltipSyncState>, emitter: symbol) => { |
|
|
if (myEventEmitter === emitter) { |
|
|
|
|
|
return; |
|
|
} |
|
|
if (mySyncId !== incomingSyncId) { |
|
|
|
|
|
return; |
|
|
} |
|
|
if (syncMethod === 'index') { |
|
|
dispatch(action); |
|
|
|
|
|
return; |
|
|
} |
|
|
|
|
|
if (tooltipTicks == null) { |
|
|
|
|
|
return; |
|
|
} |
|
|
|
|
|
let activeTick: TickItem | undefined; |
|
|
if (typeof syncMethod === 'function') { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const syncMethodParam: MouseHandlerDataParam = { |
|
|
activeTooltipIndex: action.payload.index == null ? undefined : Number(action.payload.index), |
|
|
isTooltipActive: action.payload.active, |
|
|
activeIndex: action.payload.index == null ? undefined : Number(action.payload.index), |
|
|
activeLabel: action.payload.label, |
|
|
activeDataKey: action.payload.dataKey, |
|
|
activeCoordinate: action.payload.coordinate, |
|
|
}; |
|
|
|
|
|
const activeTooltipIndex = syncMethod(tooltipTicks, syncMethodParam); |
|
|
activeTick = tooltipTicks[activeTooltipIndex]; |
|
|
} else if (syncMethod === 'value') { |
|
|
|
|
|
activeTick = tooltipTicks.find(tick => String(tick.value) === action.payload.label); |
|
|
} |
|
|
|
|
|
const { coordinate } = action.payload; |
|
|
|
|
|
if (activeTick == null || action.payload.active === false || coordinate == null || viewBox == null) { |
|
|
dispatch( |
|
|
setSyncInteraction({ |
|
|
active: false, |
|
|
coordinate: undefined, |
|
|
dataKey: undefined, |
|
|
index: null, |
|
|
label: undefined, |
|
|
}), |
|
|
); |
|
|
return; |
|
|
} |
|
|
|
|
|
const { x, y } = coordinate; |
|
|
const validateChartX = Math.min(x, viewBox.x + viewBox.width); |
|
|
const validateChartY = Math.min(y, viewBox.y + viewBox.height); |
|
|
const activeCoordinate: Coordinate = { |
|
|
x: layout === 'horizontal' ? activeTick.coordinate : validateChartX, |
|
|
y: layout === 'horizontal' ? validateChartY : activeTick.coordinate, |
|
|
}; |
|
|
|
|
|
const syncAction = setSyncInteraction({ |
|
|
active: action.payload.active, |
|
|
coordinate: activeCoordinate, |
|
|
dataKey: action.payload.dataKey, |
|
|
index: String(activeTick.index), |
|
|
label: action.payload.label, |
|
|
}); |
|
|
dispatch(syncAction); |
|
|
}; |
|
|
eventCenter.on(TOOLTIP_SYNC_EVENT, listener); |
|
|
|
|
|
return () => { |
|
|
eventCenter.off(TOOLTIP_SYNC_EVENT, listener); |
|
|
}; |
|
|
}, [className, dispatch, myEventEmitter, mySyncId, syncMethod, tooltipTicks, layout, viewBox]); |
|
|
} |
|
|
|
|
|
function useBrushSyncEventsListener() { |
|
|
const mySyncId = useAppSelector(selectSyncId); |
|
|
const myEventEmitter = useAppSelector(selectEventEmitter); |
|
|
const dispatch = useAppDispatch(); |
|
|
useEffect(() => { |
|
|
if (mySyncId == null) { |
|
|
|
|
|
return noop; |
|
|
} |
|
|
|
|
|
const listener = (incomingSyncId: number | string, action: BrushStartEndIndex, emitter: symbol) => { |
|
|
if (myEventEmitter === emitter) { |
|
|
|
|
|
return; |
|
|
} |
|
|
if (mySyncId === incomingSyncId) { |
|
|
dispatch(setDataStartEndIndexes(action)); |
|
|
} |
|
|
}; |
|
|
eventCenter.on(BRUSH_SYNC_EVENT, listener); |
|
|
|
|
|
return () => { |
|
|
eventCenter.off(BRUSH_SYNC_EVENT, listener); |
|
|
}; |
|
|
}, [dispatch, myEventEmitter, mySyncId]); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useSynchronisedEventsFromOtherCharts() { |
|
|
const dispatch = useAppDispatch(); |
|
|
useEffect(() => { |
|
|
dispatch(createEventEmitter()); |
|
|
}, [dispatch]); |
|
|
|
|
|
useTooltipSyncEventsListener(); |
|
|
useBrushSyncEventsListener(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useTooltipChartSynchronisation( |
|
|
tooltipEventType: TooltipEventType | undefined, |
|
|
trigger: TooltipTrigger, |
|
|
activeCoordinate: ChartCoordinate | undefined, |
|
|
activeLabel: string | number | undefined, |
|
|
activeIndex: TooltipIndex | undefined, |
|
|
isTooltipActive: boolean, |
|
|
) { |
|
|
const activeDataKey = useAppSelector(state => selectTooltipDataKey(state, tooltipEventType, trigger)); |
|
|
const eventEmitterSymbol = useAppSelector(selectEventEmitter); |
|
|
const syncId = useAppSelector(selectSyncId); |
|
|
const syncMethod = useAppSelector(selectSyncMethod); |
|
|
const tooltipState = useAppSelector(selectSynchronisedTooltipState); |
|
|
const isReceivingSynchronisation = tooltipState?.active; |
|
|
useEffect(() => { |
|
|
if (isReceivingSynchronisation) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
} |
|
|
if (syncId == null) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
} |
|
|
if (eventEmitterSymbol == null) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
} |
|
|
const syncAction = setSyncInteraction({ |
|
|
active: isTooltipActive, |
|
|
coordinate: activeCoordinate, |
|
|
dataKey: activeDataKey, |
|
|
index: activeIndex, |
|
|
label: typeof activeLabel === 'number' ? String(activeLabel) : activeLabel, |
|
|
}); |
|
|
eventCenter.emit(TOOLTIP_SYNC_EVENT, syncId, syncAction, eventEmitterSymbol); |
|
|
}, [ |
|
|
isReceivingSynchronisation, |
|
|
activeCoordinate, |
|
|
activeDataKey, |
|
|
activeIndex, |
|
|
activeLabel, |
|
|
eventEmitterSymbol, |
|
|
syncId, |
|
|
syncMethod, |
|
|
isTooltipActive, |
|
|
]); |
|
|
} |
|
|
|
|
|
export function useBrushChartSynchronisation() { |
|
|
const syncId = useAppSelector(selectSyncId); |
|
|
const eventEmitterSymbol = useAppSelector(selectEventEmitter); |
|
|
const brushStartIndex = useAppSelector(state => state.chartData.dataStartIndex); |
|
|
const brushEndIndex = useAppSelector(state => state.chartData.dataEndIndex); |
|
|
|
|
|
useEffect(() => { |
|
|
if (syncId == null || brushStartIndex == null || brushEndIndex == null || eventEmitterSymbol == null) { |
|
|
return; |
|
|
} |
|
|
const syncAction: BrushStartEndIndex = { startIndex: brushStartIndex, endIndex: brushEndIndex }; |
|
|
eventCenter.emit(BRUSH_SYNC_EVENT, syncId, syncAction, eventEmitterSymbol); |
|
|
}, [brushEndIndex, brushStartIndex, eventEmitterSymbol, syncId]); |
|
|
} |
|
|
|