Skip to content

Commit 9c00d17

Browse files
committed
feat: add simple interactivity when hovering data rows
Signed-off-by: Alejandro Parcet <[email protected]>
1 parent 41749bd commit 9c00d17

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

node/src/components/plots/MOGAPareto.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState, useCallback } from "react";
1+
import { useEffect, useState, useCallback, useRef } from "react";
22
import { Box, useTheme } from "@mui/material";
33
import Plot from "react-plotly.js";
44
import { JobsLoading } from "../data/JobsLoading";
@@ -16,6 +16,7 @@ import { useMMUXContext } from "../../context/MMUXContext";
1616
export function MOGAPareto(props: MogaParetoPropsType) {
1717
const { loading, progress, jobProgress } = props;
1818
const theme = useTheme();
19+
const ref = useRef<Plot>(null);
1920
const { selectedFunction, inputVars, distribution, outputTargets } = useFunctionContext();
2021
const { fetchedJobCollections, filterSelectedJobList, selectedJobUids } = useJobContext();
2122
const { weights } = useMMUXContext();
@@ -25,6 +26,7 @@ export function MOGAPareto(props: MogaParetoPropsType) {
2526
const [outputVarSelection, setOutputVarSelection] = useState<OutputVarSelection>({});
2627
const [optVars, setOptVars] = useState<Array<string>>([]);
2728
const [tableData, setTableData] = useState<MogaDataType | undefined>(undefined);
29+
const [hovered, setHovered] = useState<number | null>(null);
2830

2931
const calculatePerformance = useCallback(
3032
(row: { [x: string]: number }) => {
@@ -144,6 +146,26 @@ export function MOGAPareto(props: MogaParetoPropsType) {
144146
// eslint-disable-next-line react-hooks/exhaustive-deps
145147
}, [selectedFunction, outputTargets, weights, selectedJobUids]);
146148

149+
useEffect(() => {
150+
if (hovered !== null && tableData) {
151+
const hoveredRow = tableData.rows.find(r => r.NDI === hovered);
152+
console.log("hovered row:", hoveredRow);
153+
if (hoveredRow && optVars.length >= 2) {
154+
const newPlotData = [...plotData];
155+
newPlotData[3] = {
156+
name: "Hovered Sample",
157+
mode: "markers",
158+
type: "scatter",
159+
marker: { color: "red", size: 10, symbol: "circle" },
160+
x: [hoveredRow[optVars[0]]],
161+
y: [hoveredRow[optVars[1]]],
162+
};
163+
setPlotData(newPlotData);
164+
}
165+
}
166+
// eslint-disable-next-line react-hooks/exhaustive-deps
167+
}, [hovered]);
168+
147169
const layout = {
148170
title: { text: "Pareto Front Diagram" },
149171
xaxis: { title: { text: optVars[0] } },
@@ -176,8 +198,8 @@ export function MOGAPareto(props: MogaParetoPropsType) {
176198
)}
177199
{!propagating && plotData.length !== 0 && (
178200
<>
179-
<Plot data={plotData} layout={layout} style={plotStyle} />
180-
<MogaParetoTable tableData={tableData} />
201+
<Plot ref={ref} data={plotData} layout={layout} style={plotStyle} />
202+
<MogaParetoTable tableData={tableData} hovered={hovered} setHovered={setHovered} />
181203
</>
182204
)}
183205
</Box>

node/src/components/plots/MOGAParetoTable.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import { useMMUXContext } from "../../context/MMUXContext";
55

66
interface MogaParetoTableProps {
77
tableData: MogaDataType | undefined;
8+
hovered: number | null;
9+
setHovered: (x: number | null) => void;
810
}
911

1012
function getRowId(value: MogaDataRowType) {
1113
return value.NDI;
1214
}
1315

14-
function MogaParetoTable({ tableData }: MogaParetoTableProps) {
16+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
17+
function MogaParetoTable({ tableData, hovered, setHovered }: MogaParetoTableProps) {
1518
const { weights, setWeights, sortModel, setSortModel } = useMMUXContext();
1619
const [data, setData] = useState<MogaDataType | undefined>(undefined);
1720
const [localWeights, setLocalWeights] = useState(weights || {});
@@ -224,6 +227,15 @@ function MogaParetoTable({ tableData }: MogaParetoTableProps) {
224227
variant: "linear-progress",
225228
noRowsVariant: "linear-progress",
226229
},
230+
row: {
231+
onMouseEnter: (event: React.MouseEvent<HTMLElement>) => {
232+
const rowData = event.currentTarget!.dataset;
233+
setHovered(Number(rowData.id));
234+
},
235+
onMouseLeave: () => {
236+
setHovered(null);
237+
},
238+
},
227239
}}
228240
sortModel={localSortModel}
229241
onSortModelChange={handleSortModelChange}

0 commit comments

Comments
 (0)