Skip to content

Commit a0f734e

Browse files
committed
Dynamic tree graphs added
1 parent b02d476 commit a0f734e

3 files changed

Lines changed: 65 additions & 64 deletions

File tree

src/app/component/task-description/task-description.component.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,13 @@ <h1>{{currentTask.dimension}} -> {{currentTask.subDimension}}: {{currentTask.tas
126126
</mat-accordion>
127127
</p>
128128
</mat-expansion-panel>
129-
<mat-expansion-panel *ngIf="this.currentTask.dependsOn">
129+
<mat-expansion-panel>
130130
<mat-expansion-panel-header>
131131
<mat-panel-title>
132132
<b>Depends on</b>
133133
</mat-panel-title>
134134
</mat-expansion-panel-header>
135-
<ul *ngFor="let dependency of this.currentTask.dependsOn">
136-
<li>{{dependency}}</li>
137-
<app-tree-map></app-tree-map>
138-
</ul>
135+
<app-tree-map dimension={{currentTask.dimension}} subDimension={{currentTask.subDimension}} taskName={{currentTask.taskName}}></app-tree-map>
139136
</mat-expansion-panel>
140137
</mat-accordion>
141138
</div>
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
<div id="tree">
2-
32
</div>

src/app/component/tree-map/tree-map.component.ts

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,77 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, Input } from '@angular/core';
2+
import { ymlService } from '../../service/yaml-parser/yaml-parser.service';
23
import * as d3 from 'd3';
34

5+
export interface TreeNode {
6+
name: string;
7+
dependsOn:TreeNode[]
8+
}
9+
10+
411
@Component({
512
selector: 'app-tree-map',
613
templateUrl: './tree-map.component.html',
714
styleUrls: ['./tree-map.component.css']
815
})
16+
917
export class TreeMapComponent implements OnInit {
18+
SIZE_OF_NODE:number=10
19+
COLOR_OF_LINK:string="black"
20+
COLOR_OF_NODE:string="#55bc55"
21+
BORDER_COLOR_OF_NODE:string="black"
22+
TREE_DATA:TreeNode={name:"",dependsOn:[]};
23+
YamlObject:any;
24+
@Input() dimension: string= "";
25+
@Input() subDimension: string= "";
26+
@Input() taskName: string= "";
1027

11-
treeData = {
12-
"name": "Eve",
13-
"value": 15,
14-
"children": [
15-
{
16-
"name": "Cain",
17-
"value": 10,
18-
},
19-
{
20-
"name": "Seth",
21-
"value": 10,
22-
"children": [
23-
{
24-
"name": "Enos",
25-
"value": 7.5,
26-
},
27-
{
28-
"name": "Noam",
29-
"value": 7.5,
30-
}
31-
]
32-
},
33-
{
34-
"name": "Abel",
35-
"value": 10,
36-
},
37-
{
38-
"name": "Awan",
39-
"value": 10,
40-
"children": [
41-
{
42-
"name": "Enoch",
43-
"value": 7.5,
44-
}
45-
]
46-
},
47-
{
48-
"name": "Azura",
49-
"value": 10,
50-
}
51-
]
52-
};
53-
54-
constructor() { }
28+
constructor(private yaml:ymlService) { }
5529

5630
ngOnInit(): void {
57-
this.generateTree()
31+
this.setTreeData()
32+
}
33+
34+
setTreeData():void{
35+
this.yaml.setURI('./assets/YAML/generated/generated.yaml');
36+
// Function sets data
37+
this.yaml.getJson().subscribe((data) => {
38+
39+
this.YamlObject = data[this.dimension][this.subDimension];
40+
//console.log(JSON.parse(JSON.stringify(this.TREE_DATA)));
41+
this.TREE_DATA=this.generateTreeNodes(this.taskName)
42+
//console.log(this.TREE_DATA)
43+
this.generateTree()
44+
})
45+
46+
}
47+
48+
generateTreeNodes(currentTask:string):TreeNode{
49+
var tempTreeNode:TreeNode={name:'',dependsOn:[]}
50+
tempTreeNode['name']=currentTask
51+
try{
52+
for(var i=0;i<this.YamlObject[currentTask]["dependsOn"].length;i++){
53+
tempTreeNode['dependsOn'].push(this.generateTreeNodes(this.YamlObject[currentTask]["dependsOn"][i]))
54+
}
55+
}
56+
catch{
57+
return tempTreeNode
58+
}
59+
//console.log(tempTreeNode)
60+
return tempTreeNode
5861
}
5962

6063
generateTree():void{
6164
// set the dimensions and margins of the diagram
62-
const margin = {top: 20, right: 90, bottom: 30, left: 90},
63-
width = 660 - margin.left - margin.right,
65+
const margin = {top: 20, right: 1250, bottom: 30, left: 90},
66+
width = 2000 - margin.left - margin.right,
6467
height = 500 - margin.top - margin.bottom;
6568

6669
// declares a tree layout and assigns the size
6770
const treemap = d3.tree().size([height, width]);
6871

6972
// assigns the data to a hierarchy using parent-child relationships
70-
var nodes:any = d3.hierarchy(this.treeData, (d:any) => d.children);
73+
var nodes:any = d3.hierarchy(this.TREE_DATA, (d:any) => d.dependsOn);
74+
console.log(JSON.parse(JSON.stringify(this.TREE_DATA)));
7175

7276
// maps the node data to the tree layout
7377
nodes = treemap(nodes);
@@ -87,7 +91,7 @@ export class TreeMapComponent implements OnInit {
8791
.data( nodes.descendants().slice(1))
8892
.enter().append("path")
8993
.attr("class", "link")
90-
.style("stroke", "black")
94+
.style("stroke", this.COLOR_OF_LINK)
9195
.style("fill","none")
9296
.attr("d", (d:any) => {
9397
return "M" + d.y + "," + d.x
@@ -100,22 +104,23 @@ export class TreeMapComponent implements OnInit {
100104
const node = g.selectAll(".node")
101105
.data(nodes.descendants())
102106
.enter().append("g")
103-
.attr("class", (d:any) => "node" + (d.children ? " node--internal" : " node--leaf"))
107+
.attr("class", (d:any) => "node" + (d.dependsOn ? " node--internal" : " node--leaf"))
104108
.attr("transform", (d:any) => "translate(" + d.y + "," + d.x + ")");
105109

106110
// adds the circle to the node
107111
node.append("circle")
108-
.attr("r", (d:any) => d.data.value)
109-
.style("stroke", "black")
110-
.style("fill", "green");
112+
.attr("r", this.SIZE_OF_NODE)
113+
.style("stroke", this.BORDER_COLOR_OF_NODE)
114+
.style("fill", this.COLOR_OF_NODE);
111115

112116
// adds the text to the node
113117
node.append("text")
114-
.attr("dy", ".35em")
115-
.attr("x", (d:any) => d.children ? (d.data.value + 5) * -1 : d.data.value + 5)
116-
.attr("y", (d:any) => d.children && d.depth !== 0 ? -(d.data.value + 5) : d)
117-
.style("text-anchor", (d:any) => d.children ? "end" : "start")
118+
.attr("dy", ".25em")
119+
.attr("x", "-20")
120+
.attr("y", "-20")
121+
.style("text-anchor", (d:any) => d.dependsOn ? "end" : "start")
118122
.text((d:any) => d.data.name);
123+
console.log()
119124
}
120125

121126

0 commit comments

Comments
 (0)