Skip to content

Commit 101f40e

Browse files
authored
vis: add zooming to force graph (#1497)
1 parent eb2e2ac commit 101f40e

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

packages/ott-vis-panel/src/components/ForceGraph.tsx

+40-27
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ const ForceGraph: React.FC<ForceGraphProps> = ({
135135
event.subject.fx = null;
136136
event.subject.fy = null;
137137
}
138+
139+
const zoom = d3.zoom().on("zoom", handleZoom);
140+
function handleZoom(e: any) {
141+
svg.select("g.chart").attr("transform", e.transform);
142+
}
143+
// @ts-expect-error this works fine
144+
svg.call(zoom);
138145
});
139146

140147
function radius(num: number) {
@@ -164,33 +171,39 @@ const ForceGraph: React.FC<ForceGraphProps> = ({
164171
viewBox={`${-width / 2} ${-height / 2} ${width}, ${height}`}
165172
style={{ height: "auto", maxWidth: "100%" }}
166173
>
167-
<g className="links" stroke="#999" strokeOpacity={0.6}>
168-
{links.map((link, i) => (
169-
<line key={i} strokeWidth={Math.sqrt(link.value)} />
170-
))}
171-
</g>
172-
<g className="nodes" stroke="#fff" strokeWidth={1.5}>
173-
{nodes.map((node, i) => (
174-
<>
175-
<circle
176-
key={i}
177-
r={radius(node.radius)}
178-
fill={color(node.group)}
179-
data-nodeid={node.id}
180-
/>
181-
<text
182-
textAnchor="middle"
183-
alignmentBaseline="middle"
184-
style={{ userSelect: "none", cursor: "default", pointerEvents: "none" }}
185-
fontFamily="Inter, Helvetica, Arial, sans-serif"
186-
fontSize={10}
187-
strokeWidth={0}
188-
fill="#fff"
189-
>
190-
{node.text}
191-
</text>
192-
</>
193-
))}
174+
<g className="chart">
175+
<g className="links" stroke="#999" strokeOpacity={0.6}>
176+
{links.map((link, i) => (
177+
<line key={i} strokeWidth={Math.sqrt(link.value)} />
178+
))}
179+
</g>
180+
<g className="nodes" stroke="#fff" strokeWidth={1.5}>
181+
{nodes.map((node, i) => (
182+
<>
183+
<circle
184+
key={i}
185+
r={radius(node.radius)}
186+
fill={color(node.group)}
187+
data-nodeid={node.id}
188+
/>
189+
<text
190+
textAnchor="middle"
191+
alignmentBaseline="middle"
192+
style={{
193+
userSelect: "none",
194+
cursor: "default",
195+
pointerEvents: "none",
196+
}}
197+
fontFamily="Inter, Helvetica, Arial, sans-serif"
198+
fontSize={10}
199+
strokeWidth={0}
200+
fill="#fff"
201+
>
202+
{node.text}
203+
</text>
204+
</>
205+
))}
206+
</g>
194207
</g>
195208
</svg>
196209
);

0 commit comments

Comments
 (0)