@@ -2,6 +2,20 @@ import { Component, OnInit, Input } from '@angular/core';
22import * as d3 from 'd3'
33import { ymlService } from '../service/yaml-parser/yaml-parser.service' ;
44
5+ export interface graphNodes {
6+ id :string
7+ }
8+
9+ export interface graphLinks {
10+ source : string
11+ target : string
12+ }
13+
14+ export interface graph {
15+ nodes :graphNodes [ ]
16+ links :graphLinks [ ]
17+ }
18+
519@Component ( {
620 selector : 'app-dependency-graph' ,
721 templateUrl : './dependency-graph.component.html' ,
@@ -14,27 +28,8 @@ export class DependencyGraphComponent implements OnInit {
1428 BORDER_COLOR_OF_NODE :string = "black"
1529 simulation :any
1630 YamlObject :any ;
17- graph = {
18- nodes : [ {
19- id : "Alice"
20- } ,
21- {
22- id : "Bob"
23- } ,
24- {
25- id : "George"
26- }
27- ] ,
28- links : [ {
29- source : "Alice" ,
30- target : "George"
31- } ,
32- {
33- source : "George" ,
34- target : "Bob"
35- }
36- ]
37- } ;
31+ graphData :graph = { nodes :[ ] , links :[ ] } ;
32+ visited :string [ ] = [ ]
3833
3934 @Input ( ) dimension : string = "" ;
4035 @Input ( ) subDimension : string = "" ;
@@ -43,24 +38,77 @@ export class DependencyGraphComponent implements OnInit {
4338 constructor ( private yaml :ymlService ) { }
4439
4540 ngOnInit ( ) : void {
46- this . generateGraph ( )
41+ this . yaml . setURI ( './assets/YAML/generated/generated.yaml' ) ;
42+ // Function sets data
43+ this . yaml . getJson ( ) . subscribe ( ( data ) => {
44+ this . graphData = { nodes :[ ] , links :[ ] } ;
45+ this . YamlObject = data [ this . dimension ] [ this . subDimension ] ;
46+ this . populateGraphWithActivitiesCurrentTaskDependsOn ( this . taskName )
47+ this . populateGraphWithActivitiesThatDependsOnCurrentTask ( this . taskName )
48+ //console.log({...this.graphData['nodes']})
49+
50+
51+ console . log ( { ...this . graphData } )
52+ this . generateGraph ( this . taskName )
53+ } )
54+
4755 }
4856
49- generateGraph ( ) :void {
57+ populateGraphWithActivitiesCurrentTaskDependsOn ( task :string ) :void {
58+ this . checkIfNodeHasBeenGenerated ( task )
59+ try {
60+ var tasksThatCurrenTaskIsDependentOn = this . YamlObject [ task ] [ 'dependsOn' ]
61+ for ( var j = 0 ; j < tasksThatCurrenTaskIsDependentOn . length ; j ++ ) {
62+ this . checkIfNodeHasBeenGenerated ( tasksThatCurrenTaskIsDependentOn [ j ] )
63+ this . graphData [ 'links' ] . push ( { 'source' :tasksThatCurrenTaskIsDependentOn [ j ] , 'target' :task } )
64+ this . populateGraphWithActivitiesCurrentTaskDependsOn ( tasksThatCurrenTaskIsDependentOn [ j ] )
65+ }
66+ }
67+ catch ( e ) {
68+ console . log ( e )
69+ }
70+ //console.log({...this.graphData['nodes']})
71+
72+ }
73+ populateGraphWithActivitiesThatDependsOnCurrentTask ( task :string ) {
74+ var allTasks = Object . keys ( this . YamlObject )
75+ for ( var i = 0 ; i < allTasks . length ; i ++ ) {
76+ try {
77+ if ( this . YamlObject [ allTasks [ i ] ] [ 'dependsOn' ] . includes ( task ) ) {
78+ this . checkIfNodeHasBeenGenerated ( allTasks [ i ] )
79+ this . graphData [ 'links' ] . push ( { 'source' :task , 'target' :allTasks [ i ] } )
80+ }
81+ }
82+ catch {
83+ continue
84+ }
85+ }
5086
87+ }
88+
89+ checkIfNodeHasBeenGenerated ( task :string ) {
90+ if ( ! this . visited . includes ( task ) ) {
91+ this . graphData [ 'nodes' ] . push ( { 'id' :task } )
92+ this . visited . push ( task )
93+ }
94+ }
95+
96+
97+ generateGraph ( task :string ) :void {
98+
5199 let svg = d3 . select ( "svg" ) ,
52100 width = + svg . attr ( "width" ) ,
53101 height = + svg . attr ( "height" ) ;
54102
55103 this . simulation = d3 . forceSimulation ( )
56104 . force ( "link" , d3 . forceLink ( ) . id ( function ( d :any ) { return d . id ; } ) )
57- . force ( "charge" , d3 . forceManyBody ( ) . strength ( - 11215 ) )
105+ . force ( "charge" , d3 . forceManyBody ( ) . strength ( - 2000 ) )
58106 . force ( "center" , d3 . forceCenter ( width / 2 , height / 2 ) ) ;
59107
60108 svg . append ( 'defs' ) . append ( 'marker' )
61109 . attr ( 'id' , 'arrowhead' )
62110 . attr ( 'viewBox' , '-0 -5 10 10' )
63- . attr ( 'refX' , 20 )
111+ . attr ( 'refX' , 18 )
64112 . attr ( 'refY' , 0 )
65113 . attr ( 'orient' , 'auto' )
66114 . attr ( 'markerWidth' , 13 )
@@ -74,21 +122,25 @@ export class DependencyGraphComponent implements OnInit {
74122 let link = svg . append ( "g" )
75123 . attr ( "class" , "links" )
76124 . selectAll ( "line" )
77- . data ( this . graph . links )
125+ . data ( this . graphData [ ' links' ] )
78126 . enter ( ) . append ( "line" )
79127 . style ( "stroke" , this . COLOR_OF_LINK )
80128 . attr ( 'marker-end' , 'url(#arrowhead)' ) ;
81129
82130 let node = svg . append ( "g" )
83131 . attr ( "class" , "nodes" )
84132 . selectAll ( "g" )
85- . data ( this . graph . nodes )
133+ . data ( this . graphData [ ' nodes' ] )
86134 . enter ( ) . append ( "g" )
87135
88-
136+ var defaultNodeColor = this . COLOR_OF_NODE
89137 node . append ( "circle" )
90- . attr ( "r" , 15 )
91- . attr ( "fill" , this . COLOR_OF_NODE )
138+ . attr ( "r" , 10 )
139+ . attr ( "fill" , function ( d ) {
140+ if ( d . id == task )
141+ return "yellow"
142+ else
143+ return defaultNodeColor } )
92144
93145
94146
@@ -98,11 +150,11 @@ export class DependencyGraphComponent implements OnInit {
98150 . text ( function ( d ) { return d . id } ) ;
99151
100152 this . simulation
101- . nodes ( this . graph . nodes )
153+ . nodes ( this . graphData [ ' nodes' ] )
102154 . on ( "tick" , ticked ) ;
103155
104156 this . simulation . force ( "link" )
105- . links ( this . graph . links ) ;
157+ . links ( this . graphData [ ' links' ] ) ;
106158
107159 function ticked ( ) {
108160 link
0 commit comments