From 2713d35b198c60b053b6de6f482f978b67e69f87 Mon Sep 17 00:00:00 2001 From: mchami Date: Mon, 26 Aug 2024 16:25:22 +0200 Subject: [PATCH 1/3] interactive but issue displaying clusters --- src/Flowchart.tsx | 35 +++++++++++++++++++++++++++++++---- src/VizComponent.tsx | 34 ++++++++++++++++++++++++---------- src/VizContent.tsx | 20 ++++++++++++++++++++ src/index.ts | 2 +- 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/src/Flowchart.tsx b/src/Flowchart.tsx index 6ab6933..2f11271 100644 --- a/src/Flowchart.tsx +++ b/src/Flowchart.tsx @@ -33,7 +33,12 @@ interface ClusterLink { } -interface Props { } +interface Props { + handleClusterClick?: ((cluster: string) => void); + handleClassClick?: ((cls: string) => void); + selectedClusters?: string[]; + seletedClasses?: string[]; +} interface State { selectedCells: NotebookCellWithID[]; @@ -123,7 +128,14 @@ class Flowchart extends Component { .attr('height', nodeHeight) .attr('rx', 10) .attr('ry', 10) - .attr('fill', d => colorScheme[d.id] || '#69b3a2'); // Replace with your color logic + .attr('fill', d => colorScheme[d.id] || '#69b3a2') + .on('click', d => { + const cls = d.target.__data__.id; + if (this.props.handleClassClick) { + this.props.handleClassClick(cls); + } + } + ); svg.selectAll('text') .data(nodes) @@ -249,7 +261,14 @@ class Flowchart extends Component { .attr('cx', d => d.x) .attr('cy', d => d.y) .attr('r', circleRadius) - .attr('fill', d => colorScheme[d.class] || '#69b3a2'); // Use color scheme based on class + .attr('fill', d => colorScheme[d.class] || '#69b3a2') + .on('click', d => { + const clstr = d.target.__data__.cluster; + if (this.props.handleClusterClick) { + this.props.handleClusterClick(clstr); + } + } + ); // Draw text labels (cluster names) svg.selectAll('text') @@ -373,6 +392,8 @@ class Flowchart extends Component { export class FlowchartWidget extends ReactWidget { graph: React.RefObject; + handleClusterClick: ((cluster: string) => void) | undefined; + handleClassClick: ((cls: string) => void) | undefined; constructor() { super(); @@ -384,10 +405,16 @@ export class FlowchartWidget extends ReactWidget { this.graph.current?.updateSelectedCells(selectedCells); } + public addProps(handleClusterClick: (cluster: string) => void, handleClassClick: (cls: string) => void): void { + this.handleClusterClick = handleClusterClick; + this.handleClassClick = handleClassClick; + this.update(); + } + render(): JSX.Element { return (
- +
); } diff --git a/src/VizComponent.tsx b/src/VizComponent.tsx index 0870b21..ff851e8 100644 --- a/src/VizComponent.tsx +++ b/src/VizComponent.tsx @@ -32,11 +32,12 @@ interface GroupedCellsProps { className: string; cells: NotebookCellWithID[]; onSelectNotebook: (notebookIds: [number]) => void; + selectedClusters: string[]; + setSelectedClusters: (clusters: string[]) => void; } -const GroupedCells: React.FC = ({ className, cells, onSelectNotebook }) => { +const GroupedCells: React.FC = ({ className, cells, onSelectNotebook, selectedClusters, setSelectedClusters }) => { const [isOpen, setIsOpen] = useState(true); - const [openClusters, setOpenClusters] = useState([]); // Manage multiple open clusters const toggleOpen = () => setIsOpen(!isOpen); @@ -53,7 +54,7 @@ const GroupedCells: React.FC = ({ className, cells, onSelectN const selectedCells = (clusterNames: string[]) => { return clusterNames.flatMap((clusterName) => - clusters[clusterName].map(cell => ({ clusterName, cell })) + clusters[clusterName]?.map(cell => ({ clusterName, cell })) ).sort((a, b) => { if (a.cell.notebook_id === b.cell.notebook_id) { return a.cell.cell_id - b.cell.cell_id; @@ -61,16 +62,20 @@ const GroupedCells: React.FC = ({ className, cells, onSelectN return a.cell.notebook_id - b.cell.notebook_id; }); }; + const handleClusterClick = (clusterName: string) => { - setOpenClusters((prev) => - prev.includes(clusterName) ? prev.filter((name) => name !== clusterName) : [...prev, clusterName] - ); + if(selectedClusters.includes(clusterName)) { + setSelectedClusters(selectedClusters.filter(cluster => cluster !== clusterName)); + } + else { + setSelectedClusters([...selectedClusters, clusterName]); + } }; const handleIdentifierClick = (clusterIdentifier: string) => { const cluster = clusterIdentifiers.find(ci => ci.identifier === clusterIdentifier); if (cluster) { - setOpenClusters([cluster.name as string]); + setSelectedClusters([cluster.name as string]); } }; @@ -98,7 +103,7 @@ const GroupedCells: React.FC = ({ className, cells, onSelectN {clusterIdentifiers.map(({ name, identifier }) => (