Skip to content

Commit 48bca8e

Browse files
committed
Added new layout option 'Fixed'
1 parent 829dc94 commit 48bca8e

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/commands/dgmlViewer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class DgmlViewer {
134134
let direction: string = this.convertGraphDirectionToVisLayoutValues(directedGraph);
135135
return `hierarchical: {enabled: true, direction: '${direction}', sortMethod: 'hubsize' }`;
136136
}
137-
return '';
137+
return 'hierarchical: { enabled: false }';
138138
}
139139

140140
private saveAsPng(messageText: string) {

templates/dgmlViewer_Template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<option value="DU">Down-Up</option>
2424
<option value="LR">Left-Right</option>
2525
<option value="RL">Right-Left</option>
26+
<option value="Fixed">Fixed</option>
2627
<option value="Random">Random</option>
2728
</select>
2829
</div>

templates/dgmlViewer_Template.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(function () {
22
var nodes = new vis.DataSet([]);
3+
var nodeCoordinates = [];
34

45
nodes.forEach(nodeId => {
56
nodes.get(nodeId).color = {
@@ -35,8 +36,9 @@
3536
var defaultGraphDirection = ''; // The graph direction from the dgml file itself
3637
setDefaultGraphDirection();
3738
var container = document.getElementById('network');
39+
var seed = Math.random();
40+
options.layout.randomSeed = seed;
3841
var network = new vis.Network(container, data, options);
39-
var seed = network.getSeed();
4042
network.on("stabilizationIterationsDone", function () {
4143
network.setOptions( { physics: false } );
4244
});
@@ -258,14 +260,36 @@
258260

259261
function setDefaultGraphDirection() {
260262
const hierarchicalOptionsDirection = document.getElementById('direction');
263+
let selectedOption = '';
264+
selectedOption = defaultGraphDirection === '' ? 'Fixed' : defaultGraphDirection;
261265
for (var i, j = 0; i = hierarchicalOptionsDirection.options[j]; j++) {
262-
if (i.value === defaultGraphDirection) {
266+
if (i.value === selectedOption) {
263267
hierarchicalOptionsDirection.selectedIndex = j;
264268
break;
265269
}
266270
}
267271
}
268272

273+
function storeCoordinates(node) {
274+
if(node.x !== undefined && node.y !== undefined) {
275+
nodeCoordinates[node.id] = { x: node.x, y: node.y };
276+
}
277+
delete node.x;
278+
delete node.y;
279+
delete node.fixed;
280+
}
281+
282+
function restoreCoordinates(node) {
283+
if (node.id in nodeCoordinates) {
284+
var nodeCoords = nodeCoordinates[node.id];
285+
if (nodeCoords.x !== undefined && nodeCoords.y !== undefined) {
286+
node.x = nodeCoords.x;
287+
node.y = nodeCoords.y;
288+
node.fixed = { x: true, y: true};
289+
}
290+
}
291+
}
292+
269293
function setNetworkLayout() {
270294
const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction');
271295
const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod');
@@ -276,8 +300,12 @@
276300
const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod');
277301
if (showHierarchicalOptionsCheckbox.checked) {
278302
if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Random') {
279-
options.layout = {};
303+
nodes.getIds().forEach(id => storeCoordinates(nodes.get(id) ));
304+
options.layout = { hierarchical: { enabled: false } };
280305
seed = Math.random();
306+
} else if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Fixed') {
307+
options.layout = { hierarchical: { enabled: false } };
308+
nodes.getIds().forEach(id => restoreCoordinates(nodes.get(id)) );
281309
} else {
282310
options.layout = {
283311
hierarchical: {
@@ -289,7 +317,8 @@
289317
}
290318
} else {
291319
if (defaultGraphDirection === '') {
292-
options.layout = {};
320+
options.layout = { hierarchical: { enabled: false } };
321+
nodes.getIds().forEach(id => restoreCoordinates(nodes.get(id)) );
293322
} else {
294323
options.layout = {
295324
hierarchical: {
@@ -298,9 +327,19 @@
298327
sortMethod: 'hubsize'
299328
}
300329
};
330+
nodes.getIds().forEach(id => storeCoordinates(nodes.get(id) ));
301331
}
302332
}
303333
options.layout.randomSeed = seed;
334+
network.setOptions( {
335+
physics: {
336+
enabled: true,
337+
solver: 'repulsion'
338+
}
339+
});
340+
nodes.getIds().forEach(id => {
341+
const node = nodes.get(id);
342+
});
304343
network = new vis.Network(container, data, options);
305344
network.on("stabilizationIterationsDone", function () {
306345
network.setOptions( { physics: false } );

0 commit comments

Comments
 (0)