Skip to content

Commit 39f2e77

Browse files
committed
resize the graph components
1 parent 58386f0 commit 39f2e77

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@chakra-ui/react": "^2.10.3",
1414
"@emotion/react": "^11.13.3",
1515
"@emotion/styled": "^11.13.0",
16+
"@react-hook/resize-observer": "^2.0.2",
1617
"framer-motion": "^11.11.9",
1718
"rdflib": "^2.2.35",
1819
"react": "^18.3.1",

src/components/Content.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1+
// Content.tsx
12
import { Box } from "@chakra-ui/react";
2-
import Graph from "./Graph.tsx";
3-
import Selections from "./Selections.tsx";
4-
import { useState } from "react";
3+
import Graph from "./Graph";
4+
import Selections from "./Selections";
5+
import { useState, useRef } from "react";
6+
import useResizeObserver from "@react-hook/resize-observer"; // Import the hook directly
57
import { GraphData } from "react-force-graph-3d";
68

79
const Content = () => {
810
const [graphData, setGraphData] = useState<GraphData>({ nodes: [], links: [] });
11+
const boxRef = useRef<HTMLDivElement | null>(null); // Create a ref for the Box
12+
const [size, setSize] = useState({ width: 0, height: 0 }); // Initialize size state
13+
14+
// Use useResizeObserver to monitor size changes
15+
useResizeObserver(boxRef, (entry) => {
16+
if (entry) {
17+
const { width, height } = entry.contentRect; // Get the new width and height
18+
setSize({ width, height }); // Update the size state
19+
}
20+
});
921

1022
return (
1123
<Box width="100%" height="100%">
1224
<Box mb={{ base: 8, md: 2 }}>
1325
<Selections setGraphData={setGraphData} />
1426
</Box>
15-
<Box width="100%" height="60vh" overflow="hidden">
16-
<Graph graphData={graphData} />
27+
<Box ref={boxRef} width="100%" height="60vh" overflow="hidden">
28+
<Graph graphData={graphData} width={size.width} height={size.height} />
1729
</Box>
1830
</Box>
1931
);

src/components/Graph.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
// Graph.tsx
12
import React from "react";
23
import ForceGraph3D, { GraphData, NodeObject, LinkObject } from "react-force-graph-3d";
34
import * as THREE from "three";
45

56
interface GraphProps {
67
graphData: GraphData;
8+
width: number;
9+
height: number;
710
}
811

9-
const Graph: React.FC<GraphProps> = ({ graphData }: GraphProps) => {
12+
const Graph: React.FC<GraphProps> = ({ graphData, width, height }) => {
1013
const getGroupColor = (group: string) => {
1114
const colors: { [key: string]: string } = {
1215
person: "red",
@@ -19,14 +22,16 @@ const Graph: React.FC<GraphProps> = ({ graphData }: GraphProps) => {
1922
service: "cyan",
2023
"": "gray", // Default group
2124
};
22-
return colors[group] || "gray"; // Default to 3 if group not found
25+
return colors[group] || "gray"; // Default to gray if group not found
2326
};
2427

2528
const getNodeById = (id: string) => graphData.nodes.find((node: NodeObject) => node.id === id);
2629

2730
return (
2831
<ForceGraph3D
2932
graphData={graphData}
33+
width={width}
34+
height={height}
3035
nodeAutoColorBy={(d) => getGroupColor(d.group || "")}
3136
linkAutoColorBy={(d) => {
3237
const sourceNode =
@@ -40,11 +45,8 @@ const Graph: React.FC<GraphProps> = ({ graphData }: GraphProps) => {
4045
linkDirectionalArrowRelPos={1}
4146
linkCurvature={0.2}
4247
nodeThreeObject={({ group }) => {
43-
// Use box geometry for nodes with empty group, otherwise use spheres
4448
const geometry =
45-
group === ""
46-
? new THREE.BoxGeometry(8, 8, 8) // Box for nodes with empty group
47-
: new THREE.SphereGeometry(5); // Sphere for other nodes
49+
group === "" ? new THREE.BoxGeometry(8, 8, 8) : new THREE.SphereGeometry(5);
4850

4951
const material = new THREE.MeshStandardMaterial({
5052
color: getGroupColor(group),

0 commit comments

Comments
 (0)