@@ -55,15 +55,22 @@ window.addEventListener("DOMContentLoaded", () => {
55
55
hljs . initHighlighting ( ) ;
56
56
} ) ;
57
57
58
+ var zoom ;
59
+ var transform ;
60
+
58
61
function showGraph ( ) {
59
62
if ( $ ( "svg#graph" ) . children ( ) . length == 0 ) {
60
63
var dotNode = document . querySelector ( "#dot" )
61
64
if ( dotNode ) {
62
65
var svg = d3 . select ( "#graph" ) ;
66
+ var radialGradient = svg . append ( "defs" ) . append ( "radialGradient" ) . attr ( "id" , "Gradient" ) ;
67
+ radialGradient . append ( "stop" ) . attr ( "stop-color" , "#ffd47f" ) . attr ( "offset" , "20%" ) ;
68
+ radialGradient . append ( "stop" ) . attr ( "stop-color" , "white" ) . attr ( "offset" , "100%" ) ;
69
+
63
70
var inner = svg . append ( "g" ) ;
64
71
65
72
// Set up zoom support
66
- var zoom = d3 . zoom ( )
73
+ zoom = d3 . zoom ( )
67
74
. on ( "zoom" , function ( { transform} ) {
68
75
inner . attr ( "transform" , transform ) ;
69
76
} ) ;
@@ -76,15 +83,60 @@ function showGraph() {
76
83
g . setNode ( v , {
77
84
labelType : "html" ,
78
85
label : g . node ( v ) . label ,
79
- style : g . node ( v ) . style
86
+ style : g . node ( v ) . style ,
87
+ id : g . node ( v ) . id
80
88
} ) ;
81
89
} ) ;
90
+ g . setNode ( "node0Cluster" , {
91
+ style : "fill: url(#Gradient);" ,
92
+ id : "node0Cluster"
93
+ } ) ;
94
+ g . setParent ( "node0" , "node0Cluster" ) ;
95
+
82
96
g . edges ( ) . forEach ( function ( v ) {
83
97
g . setEdge ( v , {
84
98
arrowhead : "vee"
85
99
} ) ;
86
100
} ) ;
87
101
render ( inner , g ) ;
102
+
103
+ // Set the 'fit to content graph' upon landing on the page
104
+ var bounds = svg . node ( ) . getBBox ( ) ;
105
+ var parent = svg . node ( ) . parentElement ;
106
+ var fullWidth = parent . clientWidth || parent . parentNode . clientWidth ,
107
+ fullHeight = parent . clientHeight || parent . parentNode . clientHeight ;
108
+ var width = bounds . width ,
109
+ height = bounds . height ;
110
+ var midX = bounds . x + width / 2 ,
111
+ midY = bounds . y + height / 2 ;
112
+ if ( width == 0 || height == 0 ) return ; // nothing to fit
113
+ var scale = Math . min ( fullWidth / width , fullHeight / height ) * 0.99 ; // 0.99 to make a little padding
114
+ var translate = [ fullWidth / 2 - scale * midX , fullHeight / 2 - scale * midY ] ;
115
+
116
+ transform = d3 . zoomIdentity
117
+ . translate ( translate [ 0 ] , translate [ 1 ] )
118
+ . scale ( scale ) ;
119
+
120
+ svg . call ( zoom . transform , transform ) ;
121
+
122
+ // This is nasty hack to prevent DagreD3 from stretching cluster. There is similar issue on github since October 2019, but haven't been answered yet. https://github.com/dagrejs/dagre-d3/issues/377
123
+ var node0 = d3 . select ( "g#node0" ) . _groups [ 0 ] [ 0 ] ;
124
+ var node0Rect = node0 . children [ 0 ] ;
125
+ var node0Cluster = d3 . select ( "g#node0Cluster" ) . _groups [ 0 ] [ 0 ] ;
126
+ var node0ClusterRect = node0Cluster . children [ 0 ] ;
127
+ node0Cluster . setAttribute ( "transform" , node0 . getAttribute ( "transform" ) ) ;
128
+ node0ClusterRect . setAttribute ( "width" , + node0Rect . getAttribute ( "width" ) + 80 ) ;
129
+ node0ClusterRect . setAttribute ( "height" , + node0Rect . getAttribute ( "height" ) + 80 ) ;
130
+ node0ClusterRect . setAttribute ( "x" , node0Rect . getAttribute ( "x" ) - 40 ) ;
131
+ node0ClusterRect . setAttribute ( "y" , node0Rect . getAttribute ( "y" ) - 40 ) ;
88
132
}
89
133
}
90
134
}
135
+
136
+ function zoomOut ( ) {
137
+ var svg = d3 . select ( "#graph" ) ;
138
+ svg
139
+ . transition ( )
140
+ . duration ( 2000 )
141
+ . call ( zoom . transform , transform ) ;
142
+ }
0 commit comments