-
Notifications
You must be signed in to change notification settings - Fork 0
/
createTree.js
93 lines (74 loc) · 2.65 KB
/
createTree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
async function createTree() {
// Import our data
let data = await d3.json('./data.js')
// Convert it into a D3 friendly data structure
let root = d3.hierarchy(data);
// Tell D3 we want a tree of 1000px width and 1000px height
let treeLayout = d3.tree().size([1000, 1000]);
// Add the coordinates to our data so it makes a tree
treeLayout(root);
// Add the circle nodes
function addNodes(){
// Grab the g with class of nodes (to store our nodes)
d3.select('svg g.nodes')
.selectAll('circle.node') // select ALL circle objects with 'nodes' as class (there are none)
.data(root.descendants()) // attach the data to this 'selection'
.enter() // as there are no nodes we will make them using enter (and attach the data)
.append('circle') // for each data point, create a circle object
.classed('node', true) // add class of 'node' to each of them
.attr('cx', function (d) { // set its x coordinate (horizontal axis)
return d.x;
})
.attr('cy', function (d) { // set its y coordinate (verticle)
return d.y;
})
.attr('r', 7) // set radius of the circle (it's size)
}
addNodes()
// Add the links between nodes (source ---> target)
function addLinks () {
d3.select('svg g.links') // select the g object with class 'links'
.selectAll('line.link') // select all the line objects with class link (there aren't any)
.data(root.links()) // attach the links data
.enter() // as there are no links we will make them using enter (and attach the data)
.append('line') // create a line object for each data point
.classed('link', true) // set the class to 'link'
.attr('x1', function (d) { // set the 'source' x and y coordinates
return d.source.x;
})
.attr('y1', function (d) {
return d.source.y;
})
.attr('x2', function (d) { // set the 'target' x and y coordinates
return d.target.x;
})
.attr('y2', function (d) {
return d.target.y;
});
}
addLinks()
// Add text labels to nodes
function addTextLabels(){
d3.selectAll('svg g.labels')
.selectAll('text.label')
.data(root.descendants())
.enter()
.append('text')
.classed('label', true)
.style('text-anchor', 'middle')
.style('fill', 'white')
.text((d) => d.data.name)
.attr('x', (d) => d.x)
.attr('y', (d) => d.y - 10);
}
addTextLabels()
// Do something when clicking a node
function addClickListeners(){
d3.selectAll('circle.node')
.on('click', function(d){
alert(d.data.name)
})
}
addClickListeners()
}
createTree()