Commit a85dd443 authored by tom's avatar tom

fix chart brushing

parent 97139626
...@@ -119,47 +119,70 @@ const ChartTooltip = ({ xScale, yScale, width, height, data, anchorEl, ...props ...@@ -119,47 +119,70 @@ const ChartTooltip = ({ xScale, yScale, width, height, data, anchorEl, ...props
} }
}, [ drawPoints, drawLine, drawContent ]); }, [ drawPoints, drawLine, drawContent ]);
React.useEffect(() => { const showContent = React.useCallback(() => {
const anchorD3 = d3.select(anchorEl); d3.select(ref.current).attr('opacity', 1);
const subscriptions: Array<string> = []; d3.select(ref.current)
.selectAll('.ChartTooltip__point')
.attr('opacity', 1);
}, []);
const hideContent = React.useCallback(() => {
d3.select(ref.current).attr('opacity', 0);
}, []);
const createPointerTracker = React.useCallback((event: PointerEvent, isSubsequentCall?: boolean) => {
let isShown = false; let isShown = false;
let isPressed = event.pointerType === 'mouse' && event.type === 'pointerdown' && !isSubsequentCall;
anchorD3 if (isPressed) {
.on('touchmove.tooltip', (event: PointerEvent) => event.preventDefault()) // prevent scrolling hideContent();
.on('pointerenter.tooltip pointerdown.tooltip', (event: PointerEvent) => { }
const newSubscriptions = trackPointer(event, {
trackPointer(event, {
move: (pointer) => { move: (pointer) => {
if (!pointer.point) { if (!pointer.point || isPressed) {
return; return;
} }
draw(pointer); draw(pointer);
if (!isShown) { if (!isShown) {
d3.select(ref.current).attr('opacity', 1); showContent();
d3.select(ref.current)
.selectAll('.ChartTooltip__point')
.attr('opacity', 1);
isShown = true; isShown = true;
} }
}, },
out: () => { out: () => {
d3.select(ref.current).attr('opacity', 0); hideContent();
isShown = false; isShown = false;
}, },
end: () => { end: (tracker) => {
d3.select(ref.current).attr('opacity', 0); hideContent();
const isOutside = tracker.sourceEvent?.offsetX && width && (tracker.sourceEvent.offsetX > width || tracker.sourceEvent.offsetX < 0);
if (!isOutside && isPressed) {
window.setTimeout(() => {
createPointerTracker(event, true);
}, 0);
}
isShown = false; isShown = false;
isPressed = false;
}, },
}); });
}, [ draw, hideContent, showContent, width ]);
React.useEffect(() => {
const anchorD3 = d3.select(anchorEl);
subscriptions.push(...newSubscriptions); anchorD3
.on('touchmove.tooltip', (event: PointerEvent) => event.preventDefault()) // prevent scrolling
.on('pointerenter.tooltip pointerdown.tooltip', (event: PointerEvent) => {
createPointerTracker(event);
}); });
return () => { return () => {
anchorD3.on('touchmove.tooltip pointerenter.tooltip pointerdown.tooltip', null); anchorD3.on('touchmove.tooltip pointerenter.tooltip pointerdown.tooltip', null);
subscriptions && anchorD3.on(subscriptions.join(' '), null);
}; };
}, [ anchorEl, draw ]); }, [ anchorEl, createPointerTracker, draw, hideContent, showContent ]);
return ( return (
<g ref={ ref } opacity={ 0 } { ...props }> <g ref={ ref } opacity={ 0 } { ...props }>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment