Skip to content

Commit 96b4f11

Browse files
committed
Improved layout to avoid overlapping nodes
1 parent 9d865a6 commit 96b4f11

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

templates/showHierarchy_Template.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
edges: edges
2222
};
2323
var options = {
24-
physics: {
25-
enabled: true,
26-
solver: 'repulsion'
27-
},
2824
edges: {
2925
smooth: false // Make edges straight lines.
3026
},
@@ -33,6 +29,7 @@
3329
shape: 'box' // The shape of the nodes.
3430
}
3531
};
32+
setRandomLayout();
3633
var container = document.getElementById('network');
3734
var network = new vis.Network(container, data, options);
3835
var seed = network.getSeed();
@@ -249,6 +246,40 @@
249246
console.log('Not implemented yet...');
250247
}
251248

249+
function setRandomLayout() {
250+
options.layout ={
251+
hierarchical: {
252+
enabled: false
253+
}
254+
};
255+
options.physics = {
256+
enabled: true,
257+
barnesHut: {
258+
springConstant: 0,
259+
avoidOverlap: 0.8
260+
}
261+
};
262+
}
263+
264+
function setHierarchicalLayout(direction, sortMethod) {
265+
options.layout = {
266+
hierarchical: {
267+
enabled: true,
268+
levelSeparation: 200,
269+
nodeSpacing: 200,
270+
direction: direction,
271+
sortMethod: sortMethod
272+
}
273+
};
274+
options.physics = {
275+
enabled: true,
276+
hierarchicalRepulsion: {
277+
springConstant: 0,
278+
avoidOverlap: 0.2
279+
}
280+
};
281+
}
282+
252283
function setNetworkLayout() {
253284
const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction');
254285
const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod');
@@ -259,16 +290,12 @@
259290
const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod');
260291
if (showHierarchicalOptionsCheckbox.checked) {
261292
if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Random') {
262-
options.layout = {};
293+
setRandomLayout();
263294
seed = Math.random();
264295
} else {
265-
options.layout = {
266-
hierarchical: {
267-
enabled: true,
268-
direction: hierarchicalOptionsDirectionSelect.value ? hierarchicalOptionsDirectionSelect.value : 'UD',
269-
sortMethod: hierarchicalOptionsSortMethodSelect.value ? hierarchicalOptionsSortMethodSelect.value : 'hubsize'
270-
}
271-
};
296+
const direction = hierarchicalOptionsDirectionSelect.value ? hierarchicalOptionsDirectionSelect.value : 'UD';
297+
const sortMethod = hierarchicalOptionsSortMethodSelect.value ? hierarchicalOptionsSortMethodSelect.value : 'hubsize';
298+
setHierarchicalLayout(direction, sortMethod);
272299
}
273300
} else {
274301
options.layout = {};

0 commit comments

Comments
 (0)