Skip to content
Snippets Groups Projects
Commit 2e147ef6 authored by Marcos Pieras's avatar Marcos Pieras
Browse files

Feat(vis1 d)/add tooltip

parent 0c1623fc
No related branches found
No related tags found
1 merge request!326Feat(vis1 d)/add tooltip
Pipeline #141557 passed
...@@ -57,7 +57,6 @@ export const getPlotData = ( ...@@ -57,7 +57,6 @@ export const getPlotData = (
x: xValues, x: xValues,
y: yValues, y: yValues,
marker: { color: primaryColor }, marker: { color: primaryColor },
hoverinfo: 'none',
}, },
]; ];
case 'scatter': case 'scatter':
...@@ -68,7 +67,6 @@ export const getPlotData = ( ...@@ -68,7 +67,6 @@ export const getPlotData = (
y: yValues, y: yValues,
mode: 'markers', mode: 'markers',
marker: { color: primaryColor, size: 12 }, marker: { color: primaryColor, size: 12 },
hoverinfo: 'none',
}, },
]; ];
case 'line': case 'line':
...@@ -79,7 +77,6 @@ export const getPlotData = ( ...@@ -79,7 +77,6 @@ export const getPlotData = (
y: yValues, y: yValues,
mode: 'lines', mode: 'lines',
line: { color: primaryColor }, line: { color: primaryColor },
hoverinfo: 'none',
}, },
]; ];
case 'histogram': case 'histogram':
...@@ -88,7 +85,6 @@ export const getPlotData = ( ...@@ -88,7 +85,6 @@ export const getPlotData = (
type: 'histogram', type: 'histogram',
x: xAxisData, x: xAxisData,
marker: { color: primaryColor }, marker: { color: primaryColor },
hoverinfo: 'none',
}, },
]; ];
case 'pie': case 'pie':
...@@ -98,7 +94,6 @@ export const getPlotData = ( ...@@ -98,7 +94,6 @@ export const getPlotData = (
labels: xValues.map(String), labels: xValues.map(String),
values: xAxisData, values: xAxisData,
marker: { colors: mainColors }, marker: { colors: mainColors },
hoverinfo: 'none',
}, },
]; ];
...@@ -118,7 +113,7 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({ ...@@ -118,7 +113,7 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({
}) => { }) => {
const internalRef = useRef<HTMLDivElement>(null); const internalRef = useRef<HTMLDivElement>(null);
const [divSize, setDivSize] = useState({ width: 0, height: 0 }); const [divSize, setDivSize] = useState({ width: 0, height: 0 });
const [hoveredPoint, setHoveredPoint] = useState<{ left: number; top: number; value: number } | null>(null); const [hoveredPoint, setHoveredPoint] = useState<{ left: number; top: number; xValue: number; yValue: number } | null>(null);
useEffect(() => { useEffect(() => {
const handleResize = () => { const handleResize = () => {
...@@ -141,25 +136,28 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({ ...@@ -141,25 +136,28 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({
const handleHover = (event: any) => { const handleHover = (event: any) => {
const { points } = event; const { points } = event;
if (points.length) { if (points.length) {
const point = points[0]; const point = points[0];
const plotRect = internalRef.current?.getBoundingClientRect(); // Get the plot's bounding box const plotRect = internalRef.current?.getBoundingClientRect();
if (plotRect) { if (plotRect) {
// Calculate the position of the tooltip const xIndex = point.xaxis.d2p(point.x);
const xIndex = point.xaxis.d2p(point.x); // Convert x value to pixel position const yIndex = point.yaxis.d2p(point.y);
const yIndex = point.yaxis.d2p(point.y); // Convert y value to pixel position
setHoveredPoint({ setHoveredPoint({
left: xIndex, // Center tooltip above the point left: plotRect.left + xIndex,
top: plotRect.top + yIndex, // Position below the point top: plotRect.top + yIndex,
value: point.y, // Value to display xValue: point.x,
yValue: point.y,
}); });
} }
} }
}; };
const handleUnhover = () => {
setHoveredPoint(null);
};
return ( return (
<div className="h-full w-full flex items-center justify-center overflow-hidden relative" ref={internalRef}> <div className="h-full w-full flex items-center justify-center overflow-hidden relative" ref={internalRef}>
<Plot <Plot
...@@ -168,7 +166,6 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({ ...@@ -168,7 +166,6 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({
responsive: true, responsive: true,
scrollZoom: false, scrollZoom: false,
displayModeBar: false, displayModeBar: false,
staticPlot: true,
displaylogo: false, displaylogo: false,
}} }}
layout={{ layout={{
...@@ -179,7 +176,7 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({ ...@@ -179,7 +176,7 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({
font: { font: {
family: 'Inter, sans-serif', family: 'Inter, sans-serif',
size: 12, size: 12,
color: '#374151', // change to gp default color color: '#374151',
}, },
xaxis: { xaxis: {
title: showAxis ? (xAxisLabel ? xAxisLabel : '') : '', title: showAxis ? (xAxisLabel ? xAxisLabel : '') : '',
...@@ -195,11 +192,20 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({ ...@@ -195,11 +192,20 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({
showline: true, showline: true,
zeroline: false, zeroline: false,
}, },
hoverlabel: {
bgcolor: 'rgba(255, 255, 255, 0.8)',
bordercolor: 'rgba(0, 0, 0, 0.2)',
font: {
family: 'Inter, sans-serif',
size: 14,
color: '#374151',
},
},
}} }}
onHover={handleHover} onHover={handleHover}
onUnhover={() => setHoveredPoint(null)} onUnhover={handleUnhover}
/> />
{/*
{hoveredPoint && ( {hoveredPoint && (
<div> <div>
<Tooltip open={true} showArrow={true}> <Tooltip open={true} showArrow={true}>
...@@ -209,16 +215,17 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({ ...@@ -209,16 +215,17 @@ export const CustomChartPlotly: React.FC<CustomChartPlotlyProps> = ({
position: 'absolute', position: 'absolute',
left: hoveredPoint.left, left: hoveredPoint.left,
top: hoveredPoint.top, top: hoveredPoint.top,
transform: 'translate(-50%, -100%)', transform: 'translate(-50%, 0%)',
}} }}
> >
<div> <div>
<strong>Value:</strong> {hoveredPoint.value} <br /> <strong>{hoveredPoint.xValue}</strong>: {hoveredPoint.yValue}
</div> </div>
</TooltipContent> </TooltipContent>
</Tooltip> </Tooltip>
</div> </div>
)} )}
*/}
</div> </div>
); );
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment