Skip to content

Commit 85bd9cd

Browse files
committed
Improving layout.
Refactoring.
1 parent 66bd035 commit 85bd9cd

File tree

1 file changed

+67
-47
lines changed

1 file changed

+67
-47
lines changed

templates/dgmlViewer_Template.js

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@
1616
};
1717
var edges = new vis.DataSet([]);
1818

19-
var data = {
19+
let data = {
2020
nodes: nodes,
2121
edges: edges
2222
};
23-
var options = {
24-
physics: {
25-
enabled: true,
26-
solver: 'repulsion'
27-
},
23+
const options = {
2824
edges: {
2925
smooth: false // Make edges straight lines.
3026
},
@@ -33,43 +29,39 @@
3329
},
3430
layout: {} // The layout of the directed graph
3531
};
36-
var defaultGraphDirection = ''; // The graph direction from the dgml file itself
37-
setDefaultGraphDirection();
38-
var container = document.getElementById('network');
39-
var seed = Math.random();
40-
options.layout.randomSeed = seed;
41-
var network = new vis.Network(container, data, options);
42-
network.on("stabilizationIterationsDone", function () {
43-
network.setOptions( { physics: false } );
44-
});
45-
46-
const vscode = acquireVsCodeApi();
32+
let defaultGraphDirection = ''; // The graph direction from the dgml file itself
33+
const container = document.getElementById('network');
34+
const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction');
35+
const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod');
36+
const showHierarchicalOptionsCheckbox = document.getElementById('showHierarchicalOptions');
37+
const hierarchicalOptionsDirectionSelect = document.getElementById('direction');
38+
const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod');
39+
const saveAsPngButton = document.getElementById('saveAsPngButton');
40+
const saveSelectionAsPngButton = document.getElementById('saveSelectionAsPngButton');
41+
const copyToClipboardButton = document.getElementById('copyToClipboardButton');
42+
const graphDiv = document.getElementById('network');
43+
const selectionLayer = document.getElementById('selectionLayer');
4744
const helpTextDiv = document.getElementById('helpText');
45+
showHierarchicalOptions();
46+
47+
const vscode = acquireVsCodeApi();
4848
let lastMouseX = lastMouseY = 0;
4949
let mouseX = mouseY = 0;
5050
let selection;
5151
// get the vis.js canvas
52-
var graphDiv = document.getElementById('network');
5352
var visDiv = graphDiv.firstElementChild;
5453
var graphCanvas = visDiv.firstElementChild;
55-
const selectionLayer = document.getElementById('selectionLayer');
5654
const selectionCanvas = selectionLayer.firstElementChild;
5755
let selectionCanvasContext;
5856

5957
// add button event listeners
60-
const saveAsPngButton = document.getElementById('saveAsPngButton');
6158
saveAsPngButton.addEventListener('click', saveAsPng);
62-
const saveSelectionAsPngButton = document.getElementById('saveSelectionAsPngButton');
6359
saveSelectionAsPngButton.addEventListener('click', saveSelectionAsPng);
64-
const copyToClipboardButton = document.getElementById('copyToClipboardButton');
6560
copyToClipboardButton.addEventListener('click', copyToClipboard);
6661
copyToClipboardButton.style['display'] = 'none'; // TODO: Remove when copyToClipboard is implemented
67-
const showHierarchicalOptionsButton = document.getElementById('showHierarchicalOptions');
68-
showHierarchicalOptionsButton.addEventListener('click', showHierarchicalOptions);
69-
const hierarchicalDirectionSelect = document.getElementById('direction');
70-
hierarchicalDirectionSelect.addEventListener('change', setNetworkLayout);
71-
const hierarchicalSortMethodSelect = document.getElementById('sortMethod');
72-
hierarchicalSortMethodSelect.addEventListener('change', setNetworkLayout);
62+
showHierarchicalOptionsCheckbox.addEventListener('click', showHierarchicalOptions);
63+
hierarchicalOptionsDirectionSelect.addEventListener('change', setNetworkLayout);
64+
hierarchicalOptionsSortMethodSelect.addEventListener('change', setNetworkLayout);
7365

7466
function mouseUpEventListener(event) {
7567
// Convert the canvas to image data that can be saved
@@ -151,7 +143,6 @@
151143
}
152144

153145
function saveSelectionAsPng() {
154-
graphDiv = document.getElementById('network');
155146
visDiv = graphDiv.firstElementChild;
156147
graphCanvas = visDiv.firstElementChild;
157148

@@ -174,7 +165,6 @@
174165
}
175166

176167
function saveAsPng() {
177-
graphDiv = document.getElementById('network');
178168
visDiv = graphDiv.firstElementChild;
179169
graphCanvas = visDiv.firstElementChild;
180170

@@ -259,12 +249,11 @@
259249
}
260250

261251
function setDefaultGraphDirection() {
262-
const hierarchicalOptionsDirection = document.getElementById('direction');
263252
let selectedOption = '';
264253
selectedOption = defaultGraphDirection === '' ? 'Fixed' : defaultGraphDirection;
265-
for (var i, j = 0; i = hierarchicalOptionsDirection.options[j]; j++) {
254+
for (var i, j = 0; i = hierarchicalOptionsDirectionSelect.options[j]; j++) {
266255
if (i.value === selectedOption) {
267-
hierarchicalOptionsDirection.selectedIndex = j;
256+
hierarchicalOptionsDirectionSelect.selectedIndex = j;
268257
break;
269258
}
270259
}
@@ -291,42 +280,73 @@
291280
}
292281

293282
function setNetworkLayout() {
294-
const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction');
295-
const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod');
296-
const showHierarchicalOptionsCheckbox = document.getElementById('showHierarchicalOptions');
297283
hierarchicalOptionsDirection.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
298284
hierarchicalOptionsSortMethod.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
299285

300-
options.layout.hierarchical = {
301-
enabled: false
286+
options.layout ={
287+
hierarchical: {
288+
enabled: false
289+
}
302290
};
303291
options.physics = {
304292
enabled: true,
305-
solver: 'repulsion'
293+
barnesHut: {
294+
springConstant: 0,
295+
avoidOverlap: 0.8
296+
}
306297
};
307298
if (showHierarchicalOptionsCheckbox.checked) {
308-
const hierarchicalOptionsDirectionSelect = document.getElementById('direction');
309299
if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Random') {
310300
nodes.getIds().forEach(id => storeCoordinates(nodes.get(id)));
311301
seed = Math.random();
312302
options.layout.randomSeed = seed;
313303
} else if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Fixed') {
314304
nodes.getIds().forEach(id => restoreCoordinates(nodes.get(id)));
315305
} else {
316-
const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod');
317-
options.layout.hierarchical = {
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 = {
318317
enabled: true,
319-
direction: hierarchicalOptionsDirectionSelect.value ? hierarchicalOptionsDirectionSelect.value : defaultGraphDirection,
320-
sortMethod: hierarchicalOptionsSortMethodSelect.value ? hierarchicalOptionsSortMethodSelect.value : 'hubsize'
318+
hierarchicalRepulsion: {
319+
springConstant: 0,
320+
avoidOverlap: 0.2
321+
}
321322
};
322323
}
323324
} else {
324-
nodes.getIds().forEach(id => restoreCoordinates(nodes.get(id)));
325+
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)));
342+
} else {
343+
nodes.getIds().forEach(id => restoreCoordinates(nodes.get(id)));
344+
}
325345
}
326-
network = new vis.Network(container, data, options);
346+
var network = new vis.Network(container, data, options);
327347
network.on("stabilizationIterationsDone", function () {
328348
network.setOptions({
329-
physics: false
349+
physics: { enabled: false }
330350
});
331351
});
332352
}

0 commit comments

Comments
 (0)