@@ -8,15 +8,23 @@ import cytoscape, {
88 LayoutOptions ,
99 NodeDefinition ,
1010} from 'cytoscape' ;
11+
1112import cola from 'cytoscape-cola' ;
1213import qtip from 'cytoscape-qtip' ;
14+ import dagre from 'cytoscape-dagre' ;
1315
1416import {
1517 MatCheckboxChange ,
1618 MatCheckboxModule ,
1719} from '@angular/material/checkbox' ;
1820import { FormsModule } from '@angular/forms' ;
1921
22+
23+ cytoscape . use ( dagre ) ;
24+ cytoscape . use ( cola ) ;
25+ cytoscape . use ( qtip ) ;
26+
27+
2028interface CustomNodeDefinition extends NodeDefinition {
2129 data : {
2230 id : string ;
@@ -200,26 +208,22 @@ function getMinMaxWeight(cy: cytoscape.Core): [number, number] {
200208}
201209
202210function 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
413443function 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