-
Notifications
You must be signed in to change notification settings - Fork 7
/
graph.html
113 lines (91 loc) · 2.31 KB
/
graph.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background-color:black;
font-family:Sans-Serif;
font-size:10px;
}
circle {
fill: #ff9;
}
text{
fill: #fff;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 1300,
height = 1000;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-100)
//.friction(0.9)
.linkStrength(1)
.linkDistance(80)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("tagGraph2.json", function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke", '#ffa')
.style("stroke-width", function(d) { return d.value; })
.style("stroke-opacity", function(d) { return d.value/5 });
// define the nodes
var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.call(force.drag);
// add the nodes
node.append("circle")
.style("fill-opacity",0)
.attr("r", function(d) { return Math.sqrt(d.size/Math.PI)*5; })
.on("mouseover",function(d){
txts.filter(function(e){return e.name===d.name})
.attr("x", 12)
.attr("y", 0)
})
.on("mouseout",function(d){
txts.filter(function(e){return e.name===d.name})
.attr("x", function(e){
if(e.size>1) return 12;
else return -10000;
})
.attr("y", function(e){
if(e.size>1) return 0;
else return -10000;
})
})
// add the text
var txts=node.append("text")
.attr("x", function(e){
if(e.size>1) return 12;
else return 10000;
})
.attr("y", function(e){
if(e.size>1) return 0;
else return 10000;
})
.style("opacity",0.7)
.text(function(d) { return d.name; });
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>