Skip to content

Commit 2798b9b

Browse files
committed
Setting default graph direction. Fixing bug in save selection. Adding Random as an graph direction option.
1 parent 81f8de0 commit 2798b9b

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

src/commands/dgmlViewer.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +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`);
8182
jsContent = jsContent.replace('layout: {} // The layout of the directed graph', `layout: {${this.getDirectedGraphLayoutJs(directedGraph)}} // The layout of the directed graph`);
8283
return jsContent;
8384
}
@@ -114,17 +115,22 @@ export class DgmlViewer {
114115
return text;
115116
}
116117

118+
private convertGraphDirectionToVisLayoutValues(directedGraph: IDirectedGraph): string {
119+
let direction: string = 'LR';
120+
switch(directedGraph.graphDirection.toLowerCase()) {
121+
case 'lefttoright': direction = 'LR'; break;
122+
case 'righttoleft': direction = 'RL'; break;
123+
case 'toptobottom': direction = 'UD'; break;
124+
case 'bottomtotop': direction = 'DU'; break;
125+
default: direction = 'LR'; break;
126+
}
127+
return direction;
128+
}
129+
117130
private getDirectedGraphLayoutJs(directedGraph: IDirectedGraph): string {
118131
if(directedGraph.graphDirection !== undefined) {
119-
let direction: string = 'LR';
120-
switch(directedGraph.graphDirection.toLowerCase()) {
121-
case 'lefttoright': direction = 'LR'; break;
122-
case 'righttoleft': direction = 'RL'; break;
123-
case 'toptobottom': direction = 'UD'; break;
124-
case 'bottomtotop': direction = 'DU'; break;
125-
default: direction = 'LR'; break;
126-
}
127-
return "hierarchical: {enabled: true, direction: '', sortMethod: 'hubsize' }";
132+
let direction: string = this.convertGraphDirectionToVisLayoutValues(directedGraph);
133+
return `hierarchical: {enabled: true, direction: '${direction}', sortMethod: 'hubsize' }`;
128134
}
129135
return '';
130136
}

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="Random">Random</option>
2627
</select>
2728
</div>
2829
<div id="hierarchicalOptions_sortmethod">

templates/dgmlViewer_Template.js

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
},
3232
layout: {} // The layout of the directed graph
3333
};
34+
var defaultGraphDirection = ''; // The graph direction from the dgml file itself
35+
setDefaultGraphDirection();
3436
var container = document.getElementById('network');
3537
var network = new vis.Network(container, data, options);
3638
var seed = network.getSeed();
@@ -57,7 +59,7 @@
5759
copyToClipboardButton.addEventListener('click', copyToClipboard);
5860
copyToClipboardButton.style['display'] = 'none'; // TODO: Remove when copyToClipboard is implemented
5961
const showHierarchicalOptionsButton = document.getElementById('showHierarchicalOptions');
60-
showHierarchicalOptionsButton.addEventListener('click', setNetworkLayout);
62+
showHierarchicalOptionsButton.addEventListener('click', showHierarchicalOptions);
6163
const hierarchicalDirectionSelect = document.getElementById('direction');
6264
hierarchicalDirectionSelect.addEventListener('change', setNetworkLayout);
6365
const hierarchicalSortMethodSelect = document.getElementById('sortMethod');
@@ -101,7 +103,7 @@
101103
function drawGuideLine(ctx, mouseX, mouseY) {
102104
ctx.beginPath();
103105
ctx.setLineDash([3, 7]);
104-
if(mouseX > -1) {
106+
if (mouseX > -1) {
105107
ctx.moveTo(mouseX, 0);
106108
ctx.lineTo(mouseX, selectionCanvas.height);
107109
} else if (mouseY > -1) {
@@ -143,6 +145,10 @@
143145
}
144146

145147
function saveSelectionAsPng() {
148+
graphDiv = document.getElementById('network');
149+
visDiv = graphDiv.firstElementChild;
150+
graphCanvas = visDiv.firstElementChild;
151+
146152
// show the help text
147153
helpTextDiv.style['display'] = 'block';
148154

@@ -157,14 +163,15 @@
157163
selection = {};
158164

159165
selectionLayer.addEventListener("mouseup", mouseUpEventListener, true);
160-
selectionLayer.addEventListener("mousedown", mouseDownEventListener , true);
166+
selectionLayer.addEventListener("mousedown", mouseDownEventListener, true);
161167
selectionLayer.addEventListener("mousemove", mouseMoveEventListener, true);
162168
}
163169

164170
function saveAsPng() {
165171
graphDiv = document.getElementById('network');
166172
visDiv = graphDiv.firstElementChild;
167-
graphCanvas = visDiv.firstElementChild;
173+
graphCanvas = visDiv.firstElementChild;
174+
168175
// Calculate the bounding box of all the elements on the canvas
169176
const boundingBox = getBoundingBox();
170177

@@ -173,7 +180,7 @@
173180
finalSelectionCanvas.width = boundingBox.width;
174181
finalSelectionCanvas.height = boundingBox.height;
175182
const finalSelectionCanvasContext = finalSelectionCanvas.getContext('2d');
176-
finalSelectionCanvasContext.drawImage(graphCanvas, boundingBox.top , boundingBox.left , boundingBox.width, boundingBox.height , 0, 0, boundingBox.width, boundingBox.height);
183+
finalSelectionCanvasContext.drawImage(graphCanvas, boundingBox.top, boundingBox.left, boundingBox.width, boundingBox.height, 0, 0, boundingBox.width, boundingBox.height);
177184

178185
// Call back to the extension context to save the image of the graph to the workspace folder.
179186
vscode.postMessage({
@@ -192,7 +199,7 @@
192199
var cWidth = graphCanvas.width * bytesPerPixels;
193200
var cHeight = graphCanvas.height;
194201
var minY = minX = maxY = maxX = -1;
195-
for (var y = cHeight; y > 0 && maxY === -1; y--) {
202+
for (var y = cHeight; y > 0 && maxY === -1; y--) {
196203
for (var x = 0; x < cWidth; x += bytesPerPixels) {
197204
var arrayPos = x + y * cWidth;
198205
if (imgData.data[arrayPos + 3] > 0 && maxY === -1) {
@@ -231,15 +238,30 @@
231238
return {
232239
'top': minX,
233240
'left': minY,
234-
'width': maxX-minX,
235-
'height': maxY-minY
241+
'width': maxX - minX,
242+
'height': maxY - minY
236243
};
237244
}
238245

239246
function copyToClipboard() {
240247
console.log('Not implemented yet...');
241248
}
242-
249+
250+
function showHierarchicalOptions() {
251+
setDefaultGraphDirection();
252+
setNetworkLayout();
253+
}
254+
255+
function setDefaultGraphDirection() {
256+
const hierarchicalOptionsDirection = document.getElementById('direction');
257+
for (var i, j = 0; i = hierarchicalOptionsDirection.options[j]; j++) {
258+
if (i.value === defaultGraphDirection) {
259+
hierarchicalOptionsDirection.selectedIndex = j;
260+
break;
261+
}
262+
}
263+
}
264+
243265
function setNetworkLayout() {
244266
const hierarchicalOptionsDirection = document.getElementById('hierarchicalOptions_direction');
245267
const hierarchicalOptionsSortMethod = document.getElementById('hierarchicalOptions_sortmethod');
@@ -248,19 +270,34 @@
248270
hierarchicalOptionsSortMethod.style['display'] = showHierarchicalOptionsCheckbox.checked ? 'block' : 'none';
249271
const hierarchicalOptionsDirectionSelect = document.getElementById('direction');
250272
const hierarchicalOptionsSortMethodSelect = document.getElementById('sortMethod');
251-
if(showHierarchicalOptionsCheckbox.checked) {
252-
options.layout = {
253-
hierarchical: {
254-
enabled: true,
255-
direction: hierarchicalOptionsDirectionSelect.value ? hierarchicalOptionsDirectionSelect.value : 'LR',
256-
sortMethod: hierarchicalOptionsSortMethodSelect.value ? hierarchicalOptionsSortMethodSelect.value : 'hubsize'
257-
}
258-
};
273+
if (showHierarchicalOptionsCheckbox.checked) {
274+
if (hierarchicalOptionsDirectionSelect.value && hierarchicalOptionsDirectionSelect.value === 'Random') {
275+
options.layout = {};
276+
seed = Math.random();
277+
} else {
278+
options.layout = {
279+
hierarchical: {
280+
enabled: true,
281+
direction: hierarchicalOptionsDirectionSelect.value ? hierarchicalOptionsDirectionSelect.value : defaultGraphDirection,
282+
sortMethod: hierarchicalOptionsSortMethodSelect.value ? hierarchicalOptionsSortMethodSelect.value : 'hubsize'
283+
}
284+
};
285+
}
259286
} else {
260-
options.layout = {};
287+
if (defaultGraphDirection === '') {
288+
options.layout = {};
289+
} else {
290+
options.layout = {
291+
hierarchical: {
292+
enabled: true,
293+
direction: defaultGraphDirection,
294+
sortMethod: 'hubsize'
295+
}
296+
};
297+
}
261298
}
262299
options.layout.randomSeed = seed;
263300
network = new vis.Network(container, data, options);
264301
}
265-
302+
266303
}());

0 commit comments

Comments
 (0)