Skip to content

Commit

Permalink
[charts] Disable animations while zooming (#13807)
Browse files Browse the repository at this point in the history
Signed-off-by: Jose C Quintas Jr <[email protected]>
  • Loading branch information
JCQuintas committed Jul 12, 2024
1 parent 37fae37 commit 6c67a09
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
16 changes: 14 additions & 2 deletions packages/x-charts-pro/src/LineChartPro/LineChartPro.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from 'react';
import {
AreaPlot,
AreaPlotProps,
LineChartProps,
LineHighlightPlot,
LinePlot,
LinePlotProps,
MarkPlot,
} from '@mui/x-charts/LineChart';
import { ChartsOnAxisClickHandler } from '@mui/x-charts/ChartsOnAxisClickHandler';
Expand Down Expand Up @@ -56,8 +58,8 @@ const LineChartPro = React.forwardRef(function LineChartPro(props: LineChartProP
{props.onAxisClick && <ChartsOnAxisClickHandler {...axisClickHandlerProps} />}
{props.grid && <ChartsGrid {...gridProps} />}
<g {...clipPathGroupProps}>
<AreaPlot {...areaPlotProps} />
<LinePlot {...linePlotProps} />
<AreaPlotZoom {...areaPlotProps} />
<LinePlotZoom {...linePlotProps} />
<ChartsOverlay {...overlayProps} />
</g>
<ChartsAxis {...chartsAxisProps} />
Expand All @@ -78,4 +80,14 @@ function MarkPlotZoom(props: MarkPlotProps) {
return <MarkPlot {...props} skipAnimation={isInteracting ? true : props.skipAnimation} />;
}

function LinePlotZoom(props: LinePlotProps) {
const { isInteracting } = useZoom();
return <LinePlot {...props} skipAnimation={isInteracting ? true : props.skipAnimation} />;
}

function AreaPlotZoom(props: AreaPlotProps) {
const { isInteracting } = useZoom();
return <AreaPlot {...props} skipAnimation={isInteracting ? true : props.skipAnimation} />;
}

export { LineChartPro };
22 changes: 20 additions & 2 deletions packages/x-charts-pro/src/context/ZoomProvider/useSetupZoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ const isPointOutside = (
};

export const useSetupZoom = () => {
const { zoomData, setZoomData, isZoomEnabled, options } = useZoom();
const { zoomData, setZoomData, isZoomEnabled, options, setIsInteracting } = useZoom();
const area = useDrawingArea();

const svgRef = useSvgRef();
const eventCacheRef = React.useRef<PointerEvent[]>([]);
const eventPrevDiff = React.useRef<number>(0);
const interactionTimeoutRef = React.useRef<number | undefined>(undefined);

React.useEffect(() => {
const element = svgRef.current;
Expand All @@ -89,6 +90,15 @@ export const useSetupZoom = () => {
}

event.preventDefault();
if (interactionTimeoutRef.current) {
clearTimeout(interactionTimeoutRef.current);
}
setIsInteracting(true);
// Debounce transition to `isInteractive=false`.
// Useful because wheel events don't have an "end" event.
interactionTimeoutRef.current = window.setTimeout(() => {
setIsInteracting(false);
}, 166);

const newZoomData = zoomData.map((zoom) => {
const option = options[zoom.axisId];
Expand All @@ -112,6 +122,7 @@ export const useSetupZoom = () => {

function pointerDownHandler(event: PointerEvent) {
eventCacheRef.current.push(event);
setIsInteracting(true);
}

function pointerMoveHandler(event: PointerEvent) {
Expand Down Expand Up @@ -176,6 +187,10 @@ export const useSetupZoom = () => {
if (eventCacheRef.current.length < 2) {
eventPrevDiff.current = 0;
}

if (event.type === 'pointerup' || event.type === 'pointercancel') {
setIsInteracting(false);
}
}

element.addEventListener('wheel', wheelHandler);
Expand All @@ -200,8 +215,11 @@ export const useSetupZoom = () => {
element.removeEventListener('pointerleave', pointerUpHandler);
element.removeEventListener('touchstart', preventDefault);
element.removeEventListener('touchmove', preventDefault);
if (interactionTimeoutRef.current) {
clearTimeout(interactionTimeoutRef.current);
}
};
}, [svgRef, setZoomData, zoomData, area, isZoomEnabled, options]);
}, [svgRef, setZoomData, zoomData, area, isZoomEnabled, options, setIsInteracting]);
};

/**
Expand Down

0 comments on commit 6c67a09

Please sign in to comment.