-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vis_ForceDirected.html
159 lines (127 loc) · 3.87 KB
/
Vis_ForceDirected.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<html>
<head>
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
stroke-width: 1.5px;
}
.graph-svg-component {
background-color: #ff0000;
}
</style>
</head>
<body>
<script src="d3.js"></script>
<script>
d3.json("Data_Forcedirected.json", function(data) {
var links = data.links;
var nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source,group: link.sourcegroup});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target,group: link.targetgroup});
});
var radius = 10;
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.value; });
var arc = d3.svg.arc()
.outerRadius(radius)
.innerRadius(0);
var color = d3.scale.ordinal().range(["#B0BF1A","#00308F","#E52B50","#665D1E","#A52A2A","#FF91A7","#848482","#FBCEB1","#F5F5DC","#BF4F51",
"#A57164","#8A2BE2","#CC0000","#1DACD6","#D19FEB","#964B00","#480601","#ED872D","#A3C1AD","#ACE1AF",
"#B9F2FF","#BDB76B","#58427C","#C4D8E2","#DFFF00"]);
var width = 10000,
height = 10000;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(60)
.charge(-300)
.on("tick", tick)
.start();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "graph-svg-component");
var link = svg.selectAll(".link")
.data(force.links())
.enter().append("line")
.attr("class", "link")
.style("stroke", function(d) {return color(d.group);});
var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.attr("group",function(d) {return d.group;})
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.call(force.drag);
/*
node.append("circle")
.attr("r", 8)
.style("fill", function(d) {return color(d.group[0]); });
*/
node.append("text")
.attr("x", 12)
.attr("dy", ".35em")
.attr("color","red")
.text(function(d) { return d.name; });
node.selectAll("path")
.data(function(d, i) {
var data = [];
for(var i=0; i< d.group.length; i++){
data.push({"group": d.group[i], "value": 1});
}
return pie(data);
})
.enter()
.append("svg:path")
.attr("d", arc)
.attr("fill", function(d, i) { return color(d.data.group); });;
function tick() {
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 + ")"; });
}
function mouseover() {
groupID = d3.select(this).attr("group");
console.log(groupID);
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 16);
d3.selectAll(".node").filter( function(d) {
var haveCommon = false;
for(var i=0; i < groupID.length; i++){
for(var j=0; j < d.group.length; j++){
if(groupID[i] == d.group[j])
haveCommon = true;
}
}
return !haveCommon;
})
.each(function(d){ d3.select(this).style("opacity",0.1); });
d3.selectAll(".link").style("opacity",0.1);
}
function mouseout() {
groupID = d3.select(this).attr("group");
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 8);
d3.selectAll(".node").each(function(d){ d3.select(this).style("opacity",1); });
d3.selectAll(".link").style("opacity",1);
}
});
</script>
</body bgcolor="#E6E6FA">
</html>