|
| 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 | +@Component({ |
| 6 | + selector: 'app-dependency-graph', |
| 7 | + templateUrl: './dependency-graph.component.html', |
| 8 | + styleUrls: ['./dependency-graph.component.css'] |
| 9 | +}) |
| 10 | +export class DependencyGraphComponent implements OnInit { |
| 11 | + SIZE_OF_NODE:number=10 |
| 12 | + COLOR_OF_LINK:string="black" |
| 13 | + COLOR_OF_NODE:string="#55bc55" |
| 14 | + BORDER_COLOR_OF_NODE:string="black" |
| 15 | + simulation:any |
| 16 | + 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 | + }; |
| 38 | + |
| 39 | + @Input() dimension: string= ""; |
| 40 | + @Input() subDimension: string= ""; |
| 41 | + @Input() taskName: string= ""; |
| 42 | + |
| 43 | + constructor(private yaml:ymlService) { } |
| 44 | + |
| 45 | + ngOnInit(): void { |
| 46 | + this.generateGraph() |
| 47 | + } |
| 48 | + |
| 49 | + generateGraph():void{ |
| 50 | + |
| 51 | + let svg = d3.select("svg"), |
| 52 | + width = +svg.attr("width"), |
| 53 | + height = +svg.attr("height"); |
| 54 | + |
| 55 | + this.simulation = d3.forceSimulation() |
| 56 | + .force("link", d3.forceLink().id(function(d:any) { return d.id; })) |
| 57 | + .force("charge", d3.forceManyBody().strength(-11215)) |
| 58 | + .force("center", d3.forceCenter(width / 2, height / 2)); |
| 59 | + |
| 60 | + svg.append('defs').append('marker') |
| 61 | + .attr('id','arrowhead') |
| 62 | + .attr('viewBox','-0 -5 10 10') |
| 63 | + .attr('refX',20) |
| 64 | + .attr('refY',0) |
| 65 | + .attr('orient','auto') |
| 66 | + .attr('markerWidth',13) |
| 67 | + .attr('markerHeight',13) |
| 68 | + .attr('xoverflow','visible') |
| 69 | + .append('svg:path') |
| 70 | + .attr('d', 'M 0,-5 L 10 ,0 L 0,5') |
| 71 | + .attr('fill', this.COLOR_OF_LINK) |
| 72 | + .style('stroke','none'); |
| 73 | + |
| 74 | + let link = svg.append("g") |
| 75 | + .attr("class", "links") |
| 76 | + .selectAll("line") |
| 77 | + .data(this.graph.links) |
| 78 | + .enter().append("line") |
| 79 | + .style("stroke", this.COLOR_OF_LINK) |
| 80 | + .attr('marker-end','url(#arrowhead)'); |
| 81 | + |
| 82 | + let node = svg.append("g") |
| 83 | + .attr("class", "nodes") |
| 84 | + .selectAll("g") |
| 85 | + .data(this.graph.nodes) |
| 86 | + .enter().append("g") |
| 87 | + |
| 88 | + |
| 89 | + node.append("circle") |
| 90 | + .attr("r", 15) |
| 91 | + .attr("fill",this.COLOR_OF_NODE) |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + node.append("text") |
| 96 | + .attr("dy", ".35em") |
| 97 | + .attr("text-anchor","middle") |
| 98 | + .text(function(d) { return d.id }); |
| 99 | + |
| 100 | + this.simulation |
| 101 | + .nodes(this.graph.nodes) |
| 102 | + .on("tick", ticked); |
| 103 | + |
| 104 | + this.simulation.force("link") |
| 105 | + .links(this.graph.links); |
| 106 | + |
| 107 | + function ticked() { |
| 108 | + link |
| 109 | + .attr("x1", function(d:any) { return d.source.x; }) |
| 110 | + .attr("y1", function(d:any) { return d.source.y; }) |
| 111 | + .attr("x2", function(d:any) { return d.target.x; }) |
| 112 | + .attr("y2", function(d:any) { return d.target.y; }); |
| 113 | + |
| 114 | + node |
| 115 | + .attr("transform", function(d:any) { |
| 116 | + return "translate(" + d.x + "," + d.y + ")"; |
| 117 | + }) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments