-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
98 lines (82 loc) · 2.16 KB
/
test.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>D3 Test</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js">
</script>
</head>
<style type="text/css">
.node {
stroke: #fff;
stroke-width: 1.5px;
cursor:pointer;
}
.nodetext {
fill: #000;
font-size:12px;
cursor:pointer;
pointer-events:none;
}
</style>
<body>
<script type="text/javascript">
var width = 960;
var height = 600;
//Random choose one color from 20 colors
var color = d3.scale.category20();
//Define svg
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
//Define force property
var force = d3.layout.force()
.charge(-150)
.linkDistance(100)
.size([width, height]);
d3.json("rating.json", function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
//Define link
var link = svg.selectAll(".link")
.data(graph.links)
.enter()
.append("line")
.attr("class", "link")
.attr("stroke","#09F")
.attr("stroke-opacity","0.4")
.style("stroke-width",1);
//Define node
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter()
.append("g")
.call(force.drag);
//Define circle property
node.append("circle")
.attr("class", "node")
.attr("r",function(d){return 10+d.value;})
.style("fill", function(d) { return color(d.value); });
//Define click pending
node.append("title")
.text(function(d) { return d.name; });
//Display name on node
node.append("text")
.attr("dy", ".3em")
.attr("class","nodetext")
.style("text-anchor", "middle")
.text(function(d) { return d.name; });
//Force movement
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d){ return "translate("+d.x+"," + d.y + ")";});
});
});
</script>
</body>
</html>