29
29
} ,
30
30
layout : { } // The layout of the directed graph
31
31
} ;
32
- let defaultGraphDirection = '' ; // The graph direction from the dgml file itself
32
+ const defaultGraphDirection = '' ; // The graph direction from the dgml file itself
33
33
const container = document . getElementById ( 'network' ) ;
34
34
const hierarchicalOptionsDirection = document . getElementById ( 'hierarchicalOptions_direction' ) ;
35
35
const hierarchicalOptionsSortMethod = document . getElementById ( 'hierarchicalOptions_sortmethod' ) ;
259
259
}
260
260
}
261
261
262
- function storeCoordinates ( node ) {
263
- if ( node . x !== undefined && node . y !== undefined ) {
264
- nodeCoordinates [ node . id ] = { x : node . x , y : node . y } ;
265
- }
266
- delete node . x ;
267
- delete node . y ;
268
- delete node . fixed ;
262
+ function storeCoordinates ( ) {
263
+ nodes . forEach ( node => {
264
+ if ( node . x !== undefined && node . y !== undefined ) {
265
+ nodeCoordinates [ node . id ] = { x : node . x , y : node . y } ;
266
+ }
267
+ delete node . x ;
268
+ delete node . y ;
269
+ delete node . fixed ;
270
+ } ) ;
269
271
}
270
272
271
- function restoreCoordinates ( node ) {
272
- if ( node . id in nodeCoordinates ) {
273
- var nodeCoords = nodeCoordinates [ node . id ] ;
274
- if ( nodeCoords . x !== undefined && nodeCoords . y !== undefined ) {
275
- node . x = nodeCoords . x ;
276
- node . y = nodeCoords . y ;
277
- node . fixed = { x : true , y : true } ;
273
+ function restoreCoordinates ( ) {
274
+ nodes . forEach ( function ( node ) {
275
+ if ( node . id in nodeCoordinates ) {
276
+ var nodeCoords = nodeCoordinates [ node . id ] ;
277
+ nodes . update ( {
278
+ id : node . id ,
279
+ fixed : true ,
280
+ x : nodeCoords . x ,
281
+ y : nodeCoords . y
282
+ } ) ;
278
283
}
279
- }
284
+ } ) ;
285
+ }
286
+
287
+ function setHierarchicalLayout ( direction , sortMethod ) {
288
+ options . layout = {
289
+ hierarchical : {
290
+ enabled : true ,
291
+ levelSeparation : 200 ,
292
+ nodeSpacing : 200 ,
293
+ direction : direction ,
294
+ sortMethod : sortMethod
295
+ }
296
+ } ;
297
+ options . physics = {
298
+ enabled : true ,
299
+ hierarchicalRepulsion : {
300
+ springConstant : 0 ,
301
+ avoidOverlap : 0.2
302
+ }
303
+ } ;
280
304
}
281
305
282
306
function setNetworkLayout ( ) {
295
319
avoidOverlap : 0.8
296
320
}
297
321
} ;
322
+ var unfixNodes = false ;
298
323
if ( showHierarchicalOptionsCheckbox . checked ) {
299
324
if ( hierarchicalOptionsDirectionSelect . value && hierarchicalOptionsDirectionSelect . value === 'Random' ) {
300
- nodes . getIds ( ) . forEach ( id => storeCoordinates ( nodes . get ( id ) ) ) ;
325
+ storeCoordinates ( ) ;
301
326
seed = Math . random ( ) ;
302
327
options . layout . randomSeed = seed ;
303
328
} else if ( hierarchicalOptionsDirectionSelect . value && hierarchicalOptionsDirectionSelect . value === 'Fixed' ) {
304
- nodes . getIds ( ) . forEach ( id => restoreCoordinates ( nodes . get ( id ) ) ) ;
329
+ restoreCoordinates ( ) ;
330
+ options . physics . enabled = false ;
331
+ unfixNodes = true ;
305
332
} else {
306
- nodes . getIds ( ) . forEach ( id => storeCoordinates ( nodes . get ( id ) ) ) ;
307
- options . layout = {
308
- hierarchical : {
309
- enabled : true ,
310
- levelSeparation : 200 ,
311
- nodeSpacing : 200 ,
312
- direction : hierarchicalOptionsDirectionSelect . value ? hierarchicalOptionsDirectionSelect . value : defaultGraphDirection ,
313
- sortMethod : hierarchicalOptionsSortMethodSelect . value ? hierarchicalOptionsSortMethodSelect . value : 'hubsize'
314
- }
315
- } ;
316
- options . physics = {
317
- enabled : true ,
318
- hierarchicalRepulsion : {
319
- springConstant : 0 ,
320
- avoidOverlap : 0.2
321
- }
322
- } ;
333
+ storeCoordinates ( ) ;
334
+ const direction = hierarchicalOptionsDirectionSelect . value ? hierarchicalOptionsDirectionSelect . value : defaultGraphDirection ;
335
+ const sortMethod = hierarchicalOptionsSortMethodSelect . value ? hierarchicalOptionsSortMethodSelect . value : 'hubsize' ;
336
+ setHierarchicalLayout ( direction , sortMethod ) ;
323
337
}
324
338
} else {
325
339
if ( defaultGraphDirection !== '' ) {
326
- options . layout = {
327
- hierarchical : {
328
- enabled : true ,
329
- levelSeparation : 200 ,
330
- nodeSpacing : 200 ,
331
- direction : defaultGraphDirection ,
332
- }
333
- } ;
334
- options . physics = {
335
- enabled : true ,
336
- hierarchicalRepulsion : {
337
- springConstant : 0 ,
338
- avoidOverlap : 0.2
339
- }
340
- } ;
341
- nodes . getIds ( ) . forEach ( id => storeCoordinates ( nodes . get ( id ) ) ) ;
340
+ storeCoordinates ( ) ;
341
+ setHierarchicalLayout ( defaultGraphDirection , 'hubsize' ) ;
342
342
} else {
343
- nodes . getIds ( ) . forEach ( id => restoreCoordinates ( nodes . get ( id ) ) ) ;
343
+ restoreCoordinates ( ) ;
344
+ unfixNodes = false ;
344
345
}
345
346
}
347
+ console . log ( options ) ;
346
348
var network = new vis . Network ( container , data , options ) ;
349
+ if ( unfixNodes ) {
350
+ nodes . forEach ( function ( node ) {
351
+ nodes . update ( { id : node . id , fixed : false } ) ;
352
+ } ) ;
353
+ }
347
354
network . on ( "stabilizationIterationsDone" , function ( ) {
348
355
network . setOptions ( {
349
356
physics : { enabled : false }
350
357
} ) ;
351
- } ) ;
358
+ nodes . forEach ( function ( node ) {
359
+ nodes . update ( { id : node . id , fixed : false } ) ;
360
+ } ) ;
361
+ } ) ;
352
362
}
353
363
} ( ) ) ;
0 commit comments