Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[charts] Fix tooltip follow mouse #15189

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions docs/data/charts/tooltip/CustomAxisTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import Paper from '@mui/material/Paper';
import Typography from '@mui/material/Typography';
import { useAxisTooltip } from '@mui/x-charts/ChartsTooltip';
import { useSvgRef } from '@mui/x-charts/hooks';
import { generateVirtualElement } from './generateVirtualElement';

function usePointer() {
const svgRef = useSvgRef();
const popperRef = React.useRef(null);
const virtualElement = React.useRef(generateVirtualElement({ x: 0, y: 0 }));
const positionRef = React.useRef({ x: 0, y: 0 });

// Use a ref to avoid rerendering on every mousemove event.
const [pointer, setPointer] = React.useState({
Expand Down Expand Up @@ -43,10 +42,10 @@ function usePointer() {
};

const handleMove = (event) => {
virtualElement.current = generateVirtualElement({
positionRef.current = {
x: event.clientX,
y: event.clientY,
});
};
popperRef.current?.update();
};

Expand All @@ -61,7 +60,23 @@ function usePointer() {
};
}, [svgRef]);

return { ...pointer, popperRef, anchorEl: virtualElement.current };
return {
...pointer,
popperRef,
anchorEl: {
getBoundingClientRect: () => ({
x: positionRef.current.x,
y: positionRef.current.y,
top: positionRef.current.y,
left: positionRef.current.x,
right: positionRef.current.x,
bottom: positionRef.current.y,
width: 0,
height: 0,
toJSON: () => '',
}),
},
};
}

export function CustomAxisTooltip() {
Expand Down
25 changes: 20 additions & 5 deletions docs/data/charts/tooltip/CustomAxisTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Paper from '@mui/material/Paper';
import Typography from '@mui/material/Typography';
import { useAxisTooltip } from '@mui/x-charts/ChartsTooltip';
import { useSvgRef } from '@mui/x-charts/hooks';
import { generateVirtualElement } from './generateVirtualElement';

type PointerState = {
isActive: boolean;
Expand All @@ -16,7 +15,7 @@ type PointerState = {
function usePointer(): PointerState & Pick<PopperProps, 'popperRef' | 'anchorEl'> {
const svgRef = useSvgRef();
const popperRef: PopperProps['popperRef'] = React.useRef(null);
const virtualElement = React.useRef(generateVirtualElement({ x: 0, y: 0 }));
const positionRef = React.useRef({ x: 0, y: 0 });

// Use a ref to avoid rerendering on every mousemove event.
const [pointer, setPointer] = React.useState<PointerState>({
Expand Down Expand Up @@ -49,10 +48,10 @@ function usePointer(): PointerState & Pick<PopperProps, 'popperRef' | 'anchorEl'
};

const handleMove = (event: PointerEvent) => {
virtualElement.current = generateVirtualElement({
positionRef.current = {
x: event.clientX,
y: event.clientY,
});
};
popperRef.current?.update();
};

Expand All @@ -67,7 +66,23 @@ function usePointer(): PointerState & Pick<PopperProps, 'popperRef' | 'anchorEl'
};
}, [svgRef]);

return { ...pointer, popperRef, anchorEl: virtualElement.current };
return {
...pointer,
popperRef,
anchorEl: {
getBoundingClientRect: () => ({
x: positionRef.current.x,
y: positionRef.current.y,
top: positionRef.current.y,
left: positionRef.current.x,
right: positionRef.current.x,
bottom: positionRef.current.y,
width: 0,
height: 0,
toJSON: () => '',
}),
},
};
}

export function CustomAxisTooltip() {
Expand Down
25 changes: 20 additions & 5 deletions docs/data/charts/tooltip/CustomItemTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { useItemTooltip } from '@mui/x-charts/ChartsTooltip';
import { useSvgRef } from '@mui/x-charts/hooks';
import { generateVirtualElement } from './generateVirtualElement';

function usePointer() {
const svgRef = useSvgRef();
const popperRef = React.useRef(null);
const virtualElement = React.useRef(generateVirtualElement({ x: 0, y: 0 }));
const positionRef = React.useRef({ x: 0, y: 0 });

// Use a ref to avoid rerendering on every mousemove event.
const [pointer, setPointer] = React.useState({
Expand Down Expand Up @@ -44,10 +43,10 @@ function usePointer() {
};

const handleMove = (event) => {
virtualElement.current = generateVirtualElement({
positionRef.current = {
x: event.clientX,
y: event.clientY,
});
};
popperRef.current?.update();
};

Expand All @@ -62,7 +61,23 @@ function usePointer() {
};
}, [svgRef]);

return { ...pointer, popperRef, anchorEl: virtualElement.current };
return {
...pointer,
popperRef,
anchorEl: {
getBoundingClientRect: () => ({
x: positionRef.current.x,
y: positionRef.current.y,
top: positionRef.current.y,
left: positionRef.current.x,
right: positionRef.current.x,
bottom: positionRef.current.y,
width: 0,
height: 0,
toJSON: () => '',
}),
},
};
}

export function CustomItemTooltip() {
Expand Down
25 changes: 20 additions & 5 deletions docs/data/charts/tooltip/CustomItemTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { useItemTooltip } from '@mui/x-charts/ChartsTooltip';
import { useSvgRef } from '@mui/x-charts/hooks';
import { generateVirtualElement } from './generateVirtualElement';

type PointerState = {
isActive: boolean;
Expand All @@ -17,7 +16,7 @@ type PointerState = {
function usePointer(): PointerState & Pick<PopperProps, 'popperRef' | 'anchorEl'> {
const svgRef = useSvgRef();
const popperRef: PopperProps['popperRef'] = React.useRef(null);
const virtualElement = React.useRef(generateVirtualElement({ x: 0, y: 0 }));
const positionRef = React.useRef({ x: 0, y: 0 });

// Use a ref to avoid rerendering on every mousemove event.
const [pointer, setPointer] = React.useState<PointerState>({
Expand Down Expand Up @@ -50,10 +49,10 @@ function usePointer(): PointerState & Pick<PopperProps, 'popperRef' | 'anchorEl'
};

const handleMove = (event: PointerEvent) => {
virtualElement.current = generateVirtualElement({
positionRef.current = {
x: event.clientX,
y: event.clientY,
});
};
popperRef.current?.update();
};

Expand All @@ -68,7 +67,23 @@ function usePointer(): PointerState & Pick<PopperProps, 'popperRef' | 'anchorEl'
};
}, [svgRef]);

return { ...pointer, popperRef, anchorEl: virtualElement.current };
return {
...pointer,
popperRef,
anchorEl: {
getBoundingClientRect: () => ({
x: positionRef.current.x,
y: positionRef.current.y,
top: positionRef.current.y,
left: positionRef.current.x,
right: positionRef.current.x,
bottom: positionRef.current.y,
width: 0,
height: 0,
toJSON: () => '',
}),
},
};
}

export function CustomItemTooltip() {
Expand Down
25 changes: 20 additions & 5 deletions docs/data/charts/tooltip/ItemTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import Popper from '@mui/material/Popper';
import { useItemTooltip } from '@mui/x-charts/ChartsTooltip';
import { useSvgRef } from '@mui/x-charts/hooks';
import { CustomItemTooltipContent } from './CustomItemTooltipContent';
import { generateVirtualElement } from './generateVirtualElement';

function usePointer() {
const svgRef = useSvgRef();
const popperRef = React.useRef(null);
const virtualElement = React.useRef(generateVirtualElement({ x: 0, y: 0 }));
const positionRef = React.useRef({ x: 0, y: 0 });

// Use a ref to avoid rerendering on every mousemove event.
const [pointer, setPointer] = React.useState({
Expand Down Expand Up @@ -42,10 +41,10 @@ function usePointer() {
};

const handleMove = (event) => {
virtualElement.current = generateVirtualElement({
positionRef.current = {
x: event.clientX,
y: event.clientY,
});
};
popperRef.current?.update();
};

Expand All @@ -60,7 +59,23 @@ function usePointer() {
};
}, [svgRef]);

return { ...pointer, popperRef, anchorEl: virtualElement.current };
return {
...pointer,
popperRef,
anchorEl: {
getBoundingClientRect: () => ({
x: positionRef.current.x,
y: positionRef.current.y,
top: positionRef.current.y,
left: positionRef.current.x,
right: positionRef.current.x,
bottom: positionRef.current.y,
width: 0,
height: 0,
toJSON: () => '',
}),
},
};
}

export function ItemTooltip() {
Expand Down
25 changes: 20 additions & 5 deletions docs/data/charts/tooltip/ItemTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Popper, { PopperProps } from '@mui/material/Popper';
import { useItemTooltip } from '@mui/x-charts/ChartsTooltip';
import { useSvgRef } from '@mui/x-charts/hooks';
import { CustomItemTooltipContent } from './CustomItemTooltipContent';
import { generateVirtualElement } from './generateVirtualElement';

type PointerState = {
isActive: boolean;
Expand All @@ -15,7 +14,7 @@ type PointerState = {
function usePointer(): PointerState & Pick<PopperProps, 'popperRef' | 'anchorEl'> {
const svgRef = useSvgRef();
const popperRef: PopperProps['popperRef'] = React.useRef(null);
const virtualElement = React.useRef(generateVirtualElement({ x: 0, y: 0 }));
const positionRef = React.useRef({ x: 0, y: 0 });

// Use a ref to avoid rerendering on every mousemove event.
const [pointer, setPointer] = React.useState<PointerState>({
Expand Down Expand Up @@ -48,10 +47,10 @@ function usePointer(): PointerState & Pick<PopperProps, 'popperRef' | 'anchorEl'
};

const handleMove = (event: PointerEvent) => {
virtualElement.current = generateVirtualElement({
positionRef.current = {
x: event.clientX,
y: event.clientY,
});
};
popperRef.current?.update();
};

Expand All @@ -66,7 +65,23 @@ function usePointer(): PointerState & Pick<PopperProps, 'popperRef' | 'anchorEl'
};
}, [svgRef]);

return { ...pointer, popperRef, anchorEl: virtualElement.current };
return {
...pointer,
popperRef,
anchorEl: {
getBoundingClientRect: () => ({
x: positionRef.current.x,
y: positionRef.current.y,
top: positionRef.current.y,
left: positionRef.current.x,
right: positionRef.current.x,
bottom: positionRef.current.y,
width: 0,
height: 0,
toJSON: () => '',
}),
},
};
}

export function ItemTooltip() {
Expand Down
24 changes: 17 additions & 7 deletions docs/data/charts/tooltip/ItemTooltipFixedY.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Popper from '@mui/material/Popper';
import { useItemTooltip } from '@mui/x-charts/ChartsTooltip';
import { useDrawingArea, useSvgRef } from '@mui/x-charts/hooks';
import { CustomItemTooltipContent } from './CustomItemTooltipContent';
import { generateVirtualElement } from './generateVirtualElement';

function usePointer() {
const svgRef = useSvgRef();
Expand Down Expand Up @@ -56,7 +55,7 @@ export function ItemTooltipFixedY() {
const { isActive } = usePointer();

const popperRef = React.useRef(null);
const virtualElement = React.useRef(generateVirtualElement({ x: 0, y: 0 }));
const positionRef = React.useRef({ x: 0, y: 0 });
const svgRef = useSvgRef(); // Get the ref of the <svg/> component.
const drawingArea = useDrawingArea(); // Get the dimensions of the chart inside the <svg/>.

Expand All @@ -67,11 +66,10 @@ export function ItemTooltipFixedY() {
}

const handleMove = (event) => {
virtualElement.current = generateVirtualElement({
positionRef.current = {
x: event.clientX,
// Add the y-coordinate of the <svg/> to the to margin between the <svg/> and the drawing area
y: svgRef.current.getBoundingClientRect().top + drawingArea.top,
});
y: event.clientY,
};
popperRef.current?.update();
};

Expand All @@ -96,7 +94,19 @@ export function ItemTooltipFixedY() {
}}
open
placement="top"
anchorEl={virtualElement.current}
anchorEl={{
getBoundingClientRect: () => ({
x: positionRef.current.x,
y: positionRef.current.y,
top: positionRef.current.y,
left: positionRef.current.x,
right: positionRef.current.x,
bottom: positionRef.current.y,
width: 0,
height: 0,
toJSON: () => '',
}),
}}
popperRef={popperRef}
>
<CustomItemTooltipContent {...tooltipData} />
Expand Down
Loading
Loading