Skip to content

Commit 6829134

Browse files
committed
Bugfix: The GraphDirection attribute is now used correctly when rendering the graph.
Bugfix: It is now possible to moved nodes when Fixed direction is used.
1 parent 9fcc809 commit 6829134

File tree

4 files changed

+70
-55
lines changed

4 files changed

+70
-55
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## Version 1.2.1
4+
5+
- Bugfix: The GraphDirection attribute is now used correctly when rendering the graph.
6+
- Bugfix: It is now possible to moved nodes when Fixed direction is used.
7+
38
## Version 1.2.0
49

510
- The width and height from the bounds-attribute on the nodes are now used if the attribute is present.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"displayName": "DGMLViewer",
55
"description": "DGMLViewer is viewer for dgml (Directed Graph Markup Language) files",
66
"icon": "icon.png",
7-
"version": "1.2.0",
7+
"version": "1.2.1",
88
"repository": "https://github.com/CoderAllan/vscode-dgmlviewer",
99
"engines": {
1010
"vscode": "^1.54.0"

src/commands/dgmlViewer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class DgmlViewer {
7878
jsContent = jsContent.replace('ctx.lineWidth = 1; // graph selection guideline width', `ctx.lineWidth = ${this.config.graphSelectionGuidelineWidth}; // graph selection guideline width`);
7979
jsContent = jsContent.replace('selectionCanvasContext.strokeStyle = \'red\';', `selectionCanvasContext.strokeStyle = '${this.config.graphSelectionColor}';`);
8080
jsContent = jsContent.replace('selectionCanvasContext.lineWidth = 2;', `selectionCanvasContext.lineWidth = ${this.config.graphSelectionWidth};`);
81-
jsContent = jsContent.replace("var defaultGraphDirection = ''; // The graph direction from the dgml file itself", `var defaultGraphDirection = '${this.convertGraphDirectionToVisLayoutValues(directedGraph)}'; // The graph direction from the dgml file itself`);
81+
jsContent = jsContent.replace("const defaultGraphDirection = ''; // The graph direction from the dgml file itself", `const defaultGraphDirection = '${this.convertGraphDirectionToVisLayoutValues(directedGraph)}'; // The graph direction from the dgml file itself`);
8282
jsContent = jsContent.replace('layout: {} // The layout of the directed graph', `layout: {${this.getDirectedGraphLayoutJs(directedGraph)}} // The layout of the directed graph`);
8383
return jsContent;
8484
}

templates/dgmlViewer_Template.js

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
},
3030
layout: {} // The layout of the directed graph
3131
};
32-
let defaultGraphDirection = ''; // The graph direction from the dgml file itself
32+
const defaultGraphDirection = ''; // The graph direction from the dgml file itself
3333
const container = document.getElementById('network');
3434
const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction');
3535
const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod');
@@ -259,24 +259,48 @@
259259
}
260260
}
261261

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+
});
269271
}
270272

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+
});
278283
}
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+
};
280304
}
281305

282306
function setNetworkLayout() {
@@ -295,59 +319,45 @@
295319
avoidOverlap: 0.8
296320
}
297321
};
322+
var unfixNodes = false;
298323
if (showHierarchicalOptionsCheckbox.checked) {
299324
if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Random') {
300-
nodes.getIds().forEach(id => storeCoordinates(nodes.get(id)));
325+
storeCoordinates();
301326
seed = Math.random();
302327
options.layout.randomSeed = seed;
303328
} 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;
305332
} 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);
323337
}
324338
} else {
325339
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');
342342
} else {
343-
nodes.getIds().forEach(id => restoreCoordinates(nodes.get(id)));
343+
restoreCoordinates();
344+
unfixNodes = false;
344345
}
345346
}
347+
console.log(options);
346348
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+
}
347354
network.on("stabilizationIterationsDone", function () {
348355
network.setOptions({
349356
physics: { enabled: false }
350357
});
351-
});
358+
nodes.forEach(function(node) {
359+
nodes.update({id: node.id, fixed: false});
360+
});
361+
});
352362
}
353363
}());

0 commit comments

Comments
 (0)