@@ -95,22 +95,35 @@ export class DependencyGraphComponent implements OnInit {
9595 generateGraph ( activityName : string ) : void {
9696 let svg = d3 . select ( 'svg' ) ;
9797
98-
9998 // Now that rectWidth is set on each node, set up the simulation
10099 this . simulation = d3
101100 . forceSimulation ( )
102- . force ( 'link' , d3 . forceLink ( ) . id ( function ( d : any ) {
103- return d . id ;
104- } ) . strength ( 0.1 ) )
105- . force ( 'x' , d3 . forceX ( ( d : any ) => {
106- let col : number = 7 ;
107- return d . relativeLevel * Math . ceil ( d . relativeCount / col ) * 300 ;
108- } ) . strength ( 5 ) )
101+ . force (
102+ 'link' ,
103+ d3
104+ . forceLink ( )
105+ . id ( function ( d : any ) {
106+ return d . id ;
107+ } )
108+ . strength ( 0.1 )
109+ )
110+ . force (
111+ 'x' ,
112+ d3
113+ . forceX ( ( d : any ) => {
114+ let col : number = 7 ;
115+ return d . relativeLevel * Math . ceil ( d . relativeCount / col ) * 300 ;
116+ } )
117+ . strength ( 5 )
118+ )
109119 // .force('y', d3.forceY((d: any) => {
110120 // return d.relativeLevel * 30;
111121 // }).strength(10))
112122 . force ( 'charge' , d3 . forceManyBody ( ) . strength ( - 80 ) )
113- . force ( 'collide' , d3 . forceCollide ( ( d : any ) => 30 ) )
123+ . force (
124+ 'collide' ,
125+ d3 . forceCollide ( ( d : any ) => 30 )
126+ )
114127 . force ( 'center' , d3 . forceCenter ( 0 , 0 ) ) ;
115128
116129 svg
@@ -149,22 +162,23 @@ export class DependencyGraphComponent implements OnInit {
149162 . append ( 'g' ) ;
150163 /* eslint-enable */
151164
152-
153-
154165 const rectHeight = 30 ;
155166 const rectRx = 10 ;
156167 const rectRy = 10 ;
157168 const padding = 20 ;
158169
159170 // Append text first so we can measure it
160- node . append ( 'text' )
171+ node
172+ . append ( 'text' )
161173 . attr ( 'dy' , '0.35em' )
162174 . attr ( 'text-anchor' , 'middle' )
163- . text ( function ( d ) { return d . id ; } ) ;
175+ . text ( function ( d ) {
176+ return d . id ;
177+ } ) ;
164178
165179 // Now for each node, measure the text and insert a rect behind it
166180 const self = this ;
167- node . each ( function ( this : SVGGElement , d : any ) {
181+ node . each ( function ( this : SVGGElement , d : any ) {
168182 const textElem = d3 . select ( this ) . select ( 'text' ) . node ( ) as SVGTextElement ;
169183 let textWidth = 60 ; // fallback default
170184 if ( textElem && textElem . getBBox ) {
@@ -189,18 +203,24 @@ export class DependencyGraphComponent implements OnInit {
189203 . attr ( 'stroke-width' , 1.5 ) ;
190204 } ) ;
191205
192- this . simulation . nodes ( this . graphData [ 'nodes' ] ) . on ( 'tick' , ( ) => {
206+ this . simulation . nodes ( this . graphData [ 'nodes' ] ) . on ( 'tick' , ( ) => {
193207 self . rectCollide ( this . graphData [ 'nodes' ] ) ;
194208 ticked ( ) ;
195209 } ) ;
196210
197211 this . simulation . force ( 'link' ) . links ( this . graphData [ 'links' ] ) ;
198212
199213 function ticked ( ) {
200-
201-
202214 // Improved rectangle edge intersection for arrowhead placement
203- function rectEdgeIntersection ( sx : number , sy : number , tx : number , ty : number , rectWidth : number , rectHeight : number , offset : number = 0 ) {
215+ function rectEdgeIntersection (
216+ sx : number ,
217+ sy : number ,
218+ tx : number ,
219+ ty : number ,
220+ rectWidth : number ,
221+ rectHeight : number ,
222+ offset : number = 0
223+ ) {
204224 // Rectangle centered at (tx, ty)
205225 const dx = tx - sx ;
206226 const dy = ty - sy ;
@@ -251,9 +271,13 @@ export class DependencyGraphComponent implements OnInit {
251271 // If target has rectWidth, adjust arrow to edge minus offset
252272 if ( d . target . rectWidth ) {
253273 const pt = rectEdgeIntersection (
254- d . source . x , d . source . y ,
255- d . target . x , d . target . y ,
256- d . target . rectWidth , 30 , 10 // rectHeight, offset
274+ d . source . x ,
275+ d . source . y ,
276+ d . target . x ,
277+ d . target . y ,
278+ d . target . rectWidth ,
279+ 30 ,
280+ 10 // rectHeight, offset
257281 ) ;
258282 return pt . x ;
259283 }
@@ -262,9 +286,13 @@ export class DependencyGraphComponent implements OnInit {
262286 . attr ( 'y2' , function ( d : any ) {
263287 if ( d . target . rectWidth ) {
264288 const pt = rectEdgeIntersection (
265- d . source . x , d . source . y ,
266- d . target . x , d . target . y ,
267- d . target . rectWidth , 30 , 10
289+ d . source . x ,
290+ d . source . y ,
291+ d . target . x ,
292+ d . target . y ,
293+ d . target . rectWidth ,
294+ 30 ,
295+ 10
268296 ) ;
269297 return pt . y ;
270298 }
@@ -286,7 +314,18 @@ export class DependencyGraphComponent implements OnInit {
286314 */
287315 rectCollide ( nodes : any [ ] ) {
288316 // Loop through all pairs of nodes
289- let node , nx1 , nx2 , ny1 , ny2 , other , ox1 , ox2 , oy1 , oy2 , i , n = nodes . length ;
317+ let node ,
318+ nx1 ,
319+ nx2 ,
320+ ny1 ,
321+ ny2 ,
322+ other ,
323+ ox1 ,
324+ ox2 ,
325+ oy1 ,
326+ oy2 ,
327+ i ,
328+ n = nodes . length ;
290329 for ( i = 0 ; i < n ; ++ i ) {
291330 node = nodes [ i ] ;
292331 // Calculate bounding box for node
@@ -304,8 +343,8 @@ export class DependencyGraphComponent implements OnInit {
304343 // Check for overlap between rectangles
305344 if ( nx1 < ox2 && nx2 > ox1 && ny1 < oy2 && ny2 > oy1 ) {
306345 // Overlap detected, push nodes apart along the direction between them
307- let dx = ( node . x - other . x ) || ( Math . random ( ) - 0.5 ) ;
308- let dy = ( node . y - other . y ) || ( Math . random ( ) - 0.5 ) ;
346+ let dx = node . x - other . x || Math . random ( ) - 0.5 ;
347+ let dy = node . y - other . y || Math . random ( ) - 0.5 ;
309348 let l = Math . sqrt ( dx * dx + dy * dy ) ;
310349 let moveX = dx / l || 1 ;
311350 let moveY = dy / l || 1 ;
@@ -318,4 +357,3 @@ export class DependencyGraphComponent implements OnInit {
318357 }
319358 }
320359}
321-
0 commit comments