|
| 1 | +import { Component, OnInit } from '@angular/core'; |
| 2 | +import * as d3 from 'd3'; |
| 3 | + |
| 4 | +@Component({ |
| 5 | + selector: 'app-tree-map', |
| 6 | + templateUrl: './tree-map.component.html', |
| 7 | + styleUrls: ['./tree-map.component.css'] |
| 8 | +}) |
| 9 | +export class TreeMapComponent implements OnInit { |
| 10 | + |
| 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() { } |
| 55 | + |
| 56 | + ngOnInit(): void { |
| 57 | + this.generateTree() |
| 58 | + } |
| 59 | + |
| 60 | + generateTree():void{ |
| 61 | + // 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, |
| 64 | + height = 500 - margin.top - margin.bottom; |
| 65 | + |
| 66 | + // declares a tree layout and assigns the size |
| 67 | + const treemap = d3.tree().size([height, width]); |
| 68 | + |
| 69 | + // assigns the data to a hierarchy using parent-child relationships |
| 70 | + var nodes:any = d3.hierarchy(this.treeData, (d:any) => d.children); |
| 71 | + |
| 72 | + // maps the node data to the tree layout |
| 73 | + nodes = treemap(nodes); |
| 74 | + |
| 75 | + // append the svg object to the body of the page |
| 76 | + // appends a 'group' element to 'svg' |
| 77 | + // moves the 'group' element to the top left margin |
| 78 | + const svg = d3.select("#tree").append("svg") |
| 79 | + .attr("width", width + margin.left + margin.right) |
| 80 | + .attr("height", height + margin.top + margin.bottom), |
| 81 | + g = svg.append("g") |
| 82 | + .attr("transform", |
| 83 | + "translate(" + margin.left + "," + margin.top + ")"); |
| 84 | + |
| 85 | + // adds the links between the nodes |
| 86 | + const link = g.selectAll(".link") |
| 87 | + .data( nodes.descendants().slice(1)) |
| 88 | + .enter().append("path") |
| 89 | + .attr("class", "link") |
| 90 | + .style("stroke", "black") |
| 91 | + .style("fill","none") |
| 92 | + .attr("d", (d:any) => { |
| 93 | + return "M" + d.y + "," + d.x |
| 94 | + + "C" + (d.y + d.parent.y) / 2 + "," + d.x |
| 95 | + + " " + (d.y + d.parent.y) / 2 + "," + d.parent.x |
| 96 | + + " " + d.parent.y + "," + d.parent.x; |
| 97 | + }); |
| 98 | + |
| 99 | + // adds each node as a group |
| 100 | + const node = g.selectAll(".node") |
| 101 | + .data(nodes.descendants()) |
| 102 | + .enter().append("g") |
| 103 | + .attr("class", (d:any) => "node" + (d.children ? " node--internal" : " node--leaf")) |
| 104 | + .attr("transform", (d:any) => "translate(" + d.y + "," + d.x + ")"); |
| 105 | + |
| 106 | + // adds the circle to the node |
| 107 | + node.append("circle") |
| 108 | + .attr("r", (d:any) => d.data.value) |
| 109 | + .style("stroke", "black") |
| 110 | + .style("fill", "green"); |
| 111 | + |
| 112 | + // adds the text to the node |
| 113 | + 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 | + .text((d:any) => d.data.name); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +} |
0 commit comments