forked from fengyanzhang/group-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo4-v4-treeAndCluster.html
72 lines (68 loc) · 2.38 KB
/
demo4-v4-treeAndCluster.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script type="text/javascript" src="js/plugins/d3/d3.v4.js"></script>
<script type="text/javascript" src="js/plugins/jquery/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="css/demo.css"/>
<style>
.node {
font: 10px sans-serif;
}
.link {
stroke: steelblue;
stroke-opacity: 0.5;
fill: none;
pointer-events: none;
}
</style>
</head>
<body>
<svg width="960" height="500"></svg>
<script type="text/javascript">
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g").attr("transform", "translate(40,0)");
var tree = d3.cluster().size([height, width - 160]); //集群图
//var tree = d3.tree().size([height, width - 160]); 树形图
d3.json("js/data/cluster.json", function (error, data) {
if (error) throw error;
var root = d3.hierarchy(data);
tree(root);
var link = g.selectAll(".link")
.data(root.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("d", function (d) {
return "M" + d.y + "," + d.x
+ "C" + (d.parent.y + 100) + "," + d.x
+ " " + (d.parent.y + 100) + "," + d.parent.x
+ " " + d.parent.y + "," + d.parent.x;
});
var node = g.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", function (d) {
return "node" + (d.children ? " node--internal" : " node--leaf");
})
.attr("transform", function (d) {
return "translate(" + d.y + "," + d.x + ")";
});
node.append("circle")
.attr("r", 2.5);
node.append("text")
.attr("dy", 3)
.attr("x", function (d) {
return d.children ? -8 : 8;
})
.style("text-anchor", function (d) {
return d.children ? "end" : "start";
})
.text(function (d) {
return d.data.name
});
});
</script>
</body>
</html>