Skip to content

Commit 653797f

Browse files
committed
feat: use dagre layout i/o cola
1 parent 050c5a5 commit 653797f

3 files changed

Lines changed: 149 additions & 31 deletions

File tree

frontend/package-lock.json

Lines changed: 83 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
"chart.js": "^4.4.4",
2424
"cytoscape": "^3.30.2",
2525
"cytoscape-cola": "^2.5.1",
26+
"cytoscape-dagre": "^2.5.0",
27+
"cytoscape-euler": "^1.2.3",
28+
"cytoscape-klay": "^3.1.4",
2629
"cytoscape-qtip": "^2.8.0",
30+
"cytoscape-spread": "^3.0.0",
2731
"d3": "^7.9.0",
2832
"qtip2": "^3.0.3",
2933
"rxjs": "~7.8.0",

frontend/src/app/coupling/graph/graph.component.ts

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@ import cytoscape, {
88
LayoutOptions,
99
NodeDefinition,
1010
} from 'cytoscape';
11+
1112
import cola from 'cytoscape-cola';
1213
import qtip from 'cytoscape-qtip';
14+
import dagre from 'cytoscape-dagre';
1315

1416
import {
1517
MatCheckboxChange,
1618
MatCheckboxModule,
1719
} from '@angular/material/checkbox';
1820
import { FormsModule } from '@angular/forms';
1921

22+
23+
cytoscape.use(dagre);
24+
cytoscape.use(cola);
25+
cytoscape.use(qtip);
26+
27+
2028
interface CustomNodeDefinition extends NodeDefinition {
2129
data: {
2230
id: string;
@@ -200,26 +208,22 @@ function getMinMaxWeight(cy: cytoscape.Core): [number, number] {
200208
}
201209

202210
function drawGraph(nodes: NodeDefinition[], edges: EdgeDefinition[], showGroups = false) {
203-
cytoscape.use(cola);
204-
cytoscape.use(qtip);
205211

206212
console.log('showGrups', showGroups);
207-
const axis = showGroups ? 'xy' : 'y';
213+
// const axis = showGroups ? 'xy' : 'x';
208214

209215
// Erstelle eine Instanz von Cytoscape
210216
const cy: Core = cytoscape({
211217
container: document.getElementById('cy'), // Hier muss das HTML-Element angegeben werden, in dem der Graph dargestellt wird.
212218

213219
layout: {
214-
name: 'cola',
220+
name: 'dagre',
215221
padding: 30,
216-
nodeSpacing: 50,
222+
nodeSpacing: 40,
217223
avoidOverlap: true,
218-
flow: { axis, minSeparation: 50 },
224+
flow: { axis: 'x', minSeparation: 50 },
219225
fit: false,
220226
animate: false,
221-
222-
223227
} as LayoutOptions,
224228

225229
style: [
@@ -305,7 +309,13 @@ function drawGraph(nodes: NodeDefinition[], edges: EdgeDefinition[], showGroups
305309
userPanningEnabled: true,
306310
} as any);
307311

312+
313+
314+
315+
308316
cy.ready(() => {
317+
318+
309319
cy.nodes().forEach((node) => {
310320
const label = node.data('label');
311321
node.style('width', `${label.length * 10}px`); // Breite basierend auf der Beschriftung
@@ -327,26 +337,26 @@ function drawGraph(nodes: NodeDefinition[], edges: EdgeDefinition[], showGroups
327337
})
328338
.update();
329339

330-
var minY = Infinity;
340+
// var minY = Infinity;
331341

332-
var minY = Infinity;
342+
// var minY = Infinity;
333343

334-
// Durchlaufen Sie alle Knoten und Compound Nodes (Gruppen)
335-
cy.nodes().forEach(function (node) {
336-
var position = node.boundingBox({
337-
includeNodes: true,
338-
includeEdges: false,
339-
includeLabels: false,
340-
});
344+
// // Durchlaufen Sie alle Knoten und Compound Nodes (Gruppen)
345+
// cy.nodes().forEach(function (node) {
346+
// var position = node.boundingBox({
347+
// includeNodes: true,
348+
// includeEdges: false,
349+
// includeLabels: false,
350+
// });
341351

342-
// Überprüfen Sie die obere Grenze (y1) der Bounding-Box
343-
if (position.y1 < minY) {
344-
minY = position.y1;
345-
}
346-
});
352+
// // Überprüfen Sie die obere Grenze (y1) der Bounding-Box
353+
// if (position.y1 < minY) {
354+
// minY = position.y1;
355+
// }
356+
// });
347357

348-
// Den Graphen nach oben verschieben, sodass der obere Rand sichtbar ist
349-
cy.pan({ x: 0, y: -minY + 20 }); // Verschiebt den Graphen nach oben, +20 Pixel für zusätzlichen Puffer
358+
// // Den Graphen nach oben verschieben, sodass der obere Rand sichtbar ist
359+
// cy.pan({ x: 0, y: -minY + 20 }); // Verschiebt den Graphen nach oben, +20 Pixel für zusätzlichen Puffer
350360
});
351361

352362
cy.nodes().forEach((node: any) => {
@@ -401,13 +411,33 @@ function drawGraph(nodes: NodeDefinition[], edges: EdgeDefinition[], showGroups
401411
event: 'mouseout',
402412
},
403413
});
414+
404415
});
405416

406-
// cy.on('click', 'node', (event) => {
407-
// const node = event.target;
408-
// console.log(`Clicked on node with id: ${node.id()}`);
409-
// alert(`You clicked on ${node.data('label')}`);
410-
// });
417+
centerAllNodes(cy);
418+
419+
}
420+
421+
422+
function centerAllNodes(cy) {
423+
// Berechne die Bounding Box des gesamten Graphen
424+
const boundingBox = cy.elements().boundingBox();
425+
426+
// Berechne die Mitte des Containers
427+
const container = cy.container();
428+
const containerCenterX = container.clientWidth / 2;
429+
const containerCenterY = container.clientHeight / 2;
430+
431+
// Berechne die Mitte des gesamten Graphen
432+
const graphCenterX = (boundingBox.x1 + boundingBox.x2) / 2;
433+
const graphCenterY = (boundingBox.y1 + boundingBox.y2) / 2;
434+
435+
// Berechne die Verschiebung in X- und Y-Richtung
436+
const shiftX = containerCenterX - graphCenterX;
437+
const shiftY = containerCenterY - graphCenterY;
438+
439+
// Verwende cy.panBy, um den Graphen nach links/rechts und oben/unten zu verschieben
440+
cy.panBy({ x: shiftX, y: shiftY - 200 });
411441
}
412442

413443
function sumRow(matrix: number[][], nodeIndex: number): number {
@@ -428,4 +458,6 @@ function sumCol(matrix: number[][], nodeIndex: number): number {
428458
}
429459
}
430460
return sum;
431-
}
461+
}
462+
463+

0 commit comments

Comments
 (0)