@@ -126,11 +126,24 @@ class DependencyGraph extends Component<
126
126
}
127
127
128
128
drawGraph ( ) {
129
- let svg = d3 . select ( this . svg . current ) ;
129
+ let svg = d3 . select ( this . svg . current ! ) as unknown as d3 . Selection < Element , unknown , any , any > ;
130
130
let inner : any = d3 . select ( this . innerG . current ) ;
131
+
131
132
this . d3render ( inner , this . g ) ;
132
- inner . attr ( "transform" , `translate(20, 20)` ) ;
133
- svg . attr ( "height" , this . g . graph ( ) . height + 40 ) ;
133
+
134
+ // Enable pan/zoom
135
+ const zoom = d3 . zoom ( )
136
+ . on ( "zoom" , ( ) => {
137
+ inner . attr ( "transform" , d3 . event . transform ) ;
138
+ } ) ;
139
+ svg . call ( zoom ) ;
140
+
141
+ const { width, height } = inner . node ( ) . getBBox ( ) ;
142
+ // Scale to fit, but no bigger than 2x
143
+ const scale = Math . min ( Math . min ( this . svg . current ! . clientWidth / width , this . svg . current ! . clientHeight / height ) * 0.95 , 2 ) ;
144
+ zoom . scaleTo ( svg , scale ) ;
145
+ // Center horizontally
146
+ zoom . translateTo ( svg , width / 2 , 0 ) ;
134
147
}
135
148
async componentDidMount ( ) {
136
149
await SERVER . initConnections ( ) ;
@@ -165,7 +178,7 @@ class DependencyGraph extends Component<
165
178
disableContentPaddings = { true }
166
179
>
167
180
< div className = "holder" >
168
- < svg height = "100" style = { { width : "100%" } } ref = { this . svg } >
181
+ < svg height = "100" style = { { width : "100%" , height : "500px" } } ref = { this . svg } >
169
182
< g ref = { this . innerG } />
170
183
</ svg >
171
184
</ div >
0 commit comments