|
| 1 | +import { Component, OnInit, Input } from '@angular/core'; |
| 2 | +import * as d3 from 'd3' |
| 3 | +import { ymlService } from '../service/yaml-parser/yaml-parser.service'; |
| 4 | + |
| 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 | + |
| 19 | +@Component({ |
| 20 | + selector: 'app-dependency-graph', |
| 21 | + templateUrl: './dependency-graph.component.html', |
| 22 | + styleUrls: ['./dependency-graph.component.css'] |
| 23 | +}) |
| 24 | +export class DependencyGraphComponent implements OnInit { |
| 25 | + SIZE_OF_NODE:number=10 |
| 26 | + COLOR_OF_LINK:string="black" |
| 27 | + COLOR_OF_NODE:string="#55bc55" |
| 28 | + BORDER_COLOR_OF_NODE:string="black" |
| 29 | + simulation:any |
| 30 | + YamlObject:any; |
| 31 | + graphData:graph={nodes:[],links:[]}; |
| 32 | + visited:string[]=[] |
| 33 | + |
| 34 | + @Input() dimension: string= ""; |
| 35 | + @Input() subDimension: string= ""; |
| 36 | + @Input() taskName: string= ""; |
| 37 | + |
| 38 | + constructor(private yaml:ymlService) { } |
| 39 | + |
| 40 | + ngOnInit(): void { |
| 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 | + |
| 55 | + } |
| 56 | + |
| 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 | + } |
| 86 | + |
| 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 | + |
| 99 | + let svg = d3.select("svg"), |
| 100 | + width = +svg.attr("width"), |
| 101 | + height = +svg.attr("height"); |
| 102 | + |
| 103 | + this.simulation = d3.forceSimulation() |
| 104 | + .force("link", d3.forceLink().id(function(d:any) { return d.id; })) |
| 105 | + .force("charge", d3.forceManyBody().strength(-2000)) |
| 106 | + .force("center", d3.forceCenter(width / 2, height / 2)); |
| 107 | + |
| 108 | + svg.append('defs').append('marker') |
| 109 | + .attr('id','arrowhead') |
| 110 | + .attr('viewBox','-0 -5 10 10') |
| 111 | + .attr('refX',18) |
| 112 | + .attr('refY',0) |
| 113 | + .attr('orient','auto') |
| 114 | + .attr('markerWidth',13) |
| 115 | + .attr('markerHeight',13) |
| 116 | + .attr('xoverflow','visible') |
| 117 | + .append('svg:path') |
| 118 | + .attr('d', 'M 0,-5 L 10 ,0 L 0,5') |
| 119 | + .attr('fill', this.COLOR_OF_LINK) |
| 120 | + .style('stroke','none'); |
| 121 | + |
| 122 | + let link = svg.append("g") |
| 123 | + .attr("class", "links") |
| 124 | + .selectAll("line") |
| 125 | + .data(this.graphData['links']) |
| 126 | + .enter().append("line") |
| 127 | + .style("stroke", this.COLOR_OF_LINK) |
| 128 | + .attr('marker-end','url(#arrowhead)'); |
| 129 | + |
| 130 | + let node = svg.append("g") |
| 131 | + .attr("class", "nodes") |
| 132 | + .selectAll("g") |
| 133 | + .data(this.graphData['nodes']) |
| 134 | + .enter().append("g") |
| 135 | + |
| 136 | + var defaultNodeColor=this.COLOR_OF_NODE |
| 137 | + node.append("circle") |
| 138 | + .attr("r", 10) |
| 139 | + .attr("fill",function(d) { |
| 140 | + if(d.id==task) |
| 141 | + return "yellow" |
| 142 | + else |
| 143 | + return defaultNodeColor}) |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + node.append("text") |
| 148 | + .attr("dy", ".35em") |
| 149 | + .attr("text-anchor","middle") |
| 150 | + .text(function(d) { return d.id }); |
| 151 | + |
| 152 | + this.simulation |
| 153 | + .nodes(this.graphData['nodes']) |
| 154 | + .on("tick", ticked); |
| 155 | + |
| 156 | + this.simulation.force("link") |
| 157 | + .links(this.graphData['links']); |
| 158 | + |
| 159 | + function ticked() { |
| 160 | + link |
| 161 | + .attr("x1", function(d:any) { return d.source.x; }) |
| 162 | + .attr("y1", function(d:any) { return d.source.y; }) |
| 163 | + .attr("x2", function(d:any) { return d.target.x; }) |
| 164 | + .attr("y2", function(d:any) { return d.target.y; }); |
| 165 | + |
| 166 | + node |
| 167 | + .attr("transform", function(d:any) { |
| 168 | + return "translate(" + d.x + "," + d.y + ")"; |
| 169 | + }) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments