-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path15.html
49 lines (41 loc) · 1.29 KB
/
15.html
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
<!DOCTYPE html>
<html>
<head>
<title>D3 tree</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
var chart = d3.select("body").append("svg")
.attr("width", 500)
.attr("height", 500)
.append("g")
.attr("transform", "translate(50, 50)");
var tree = d3.layout.tree()
.size([400, 400]);
d3.json("mynesteddata.json", function(data) {
var nodes = tree.nodes(data); // create data nodes suitable for tree structure
var links = tree.links(nodes); // create links to connect source(parent) and target(child) nodes
var nodes = chart.selectAll(".node")
.data(nodes).enter()
.append("g")
.attr("class", "node")
.attr("transform", function(d){ return "translate(" + d.y + "," + d.x + ")"; }); // flip x and y of nodes
nodes.append("circle")
.attr("r", 5)
.attr("fill", "steelblue");
nodes.append("text")
.text(function(d){ return d.name; });
var diagonal = d3.svg.diagonal()
.projection(function(d){ return [d.y, d.x]; }); // flip x and y of links
chart.selectAll(".link")
.data(links).enter()
.append("path")
.attr("class", "link")
.attr("fill", "none").attr("stroke", "#ADADAD")
.attr("d", diagonal);
});
</script>
</body>
</html>