-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetwork.js
More file actions
executable file
·170 lines (143 loc) · 3.83 KB
/
network.js
File metadata and controls
executable file
·170 lines (143 loc) · 3.83 KB
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
160
161
162
163
164
165
166
167
168
169
170
(function($) {
// DOM
var CONTAINER = "#network";
// Container
var WIDTH = $(CONTAINER).width();
var HEIGHT = $(CONTAINER).height();
// Node
var RADIUS = 3000;
// Arrowhead
var MARKER = 8;
var OFFSET = 20;
// Transitions
var TIME = 250;
// Colors
var COLOR = d3.scale.category20b();
// Resizing
d3.select(window).on('resize', resize);
// Elements
var force = d3.layout.force()
.charge(-120)
.linkDistance(200)
.size([WIDTH, HEIGHT]);
var svg = d3.select(CONTAINER).append("svg")
.attr("viewBox", "0 0 " + WIDTH + " " + HEIGHT);
// Search
$("#search").keyup(onKeyUp);
// Network
d3.json("blogs.json", function(error, graph) {
if (error) throw error;
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var def = svg.append("svg:defs");
var marker = def.selectAll("marker")
.data(["end"])
.enter().append("svg:marker")
.attr("id", "end")
.attr("viewBox", "0 -5 10 10")
.attr("refX", OFFSET)
.attr("refY", -1.5)
.attr("markerWidth", MARKER)
.attr("markerHeight", MARKER)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
var link = svg.append("svg:g").selectAll(".link")
.data(force.links())
.enter().append("path")
.attr("class", "link")
.style("stroke-width", function(d) { d.value; })
.attr("marker-end", "url(#end)");
var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.on("click", click)
.call(force.drag);
node.append("circle")
.attr("r", function(d) {
return RADIUS * d.importance;
})
.style("fill", function(d) {
return stringToColor(d.name);
});
force.on("tick", function() {
link.attr("d", function(d) {
var dx = d.target.x - d.source.x;
var dy = d.target.y - d.source.y;
var dr = Math.sqrt(dx * dx + dy * dy);
return "M" +
d.source.x + "," +
d.source.y + "A" +
dr + "," + dr + " 0 0,1 " +
d.target.x + "," +
d.target.y;
});
node.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
});
});
function resize() {
var width = $(window).width();
var height = $(window).height();
svg.attr("width", width).attr("height", height);
}
function stringToColor(value) {
var hash = "";
for (var i = 0; i < value.length; i++) {
hash += value.charCodeAt(i);
}
return COLOR(parseInt(hash));
}
function mouseover(d) {
$("#blog .value").empty().append("<a href='" + d.URL + "'>" + d.name + "</a>")
$("#inlinks .value").text(d.inlinks);
d3.select(this).select("circle").transition()
.duration(TIME)
.attr("r", 1.2 * RADIUS * d.importance);
}
function mouseout(d) {
d3.select(this).select("circle").transition()
.duration(TIME)
.attr("r", RADIUS * d.importance);
}
function click(d) {
if (!d3.event.defaultPrevented) {
window.open(d.URL, "_blank");
}
}
function onKeyUp() {
query = $(this).val();
delay = 1000;
window.setTimeout(function() {
pulse(query);
}, delay);
}
function pulse(query) {
if (query.length > 2) {
d3.selectAll("circle").each(function(d) {
node = d3.select(this);
node.transition().duration(0);
name = d.name.replace(".blogspot", "").replace(".wordpress", "").replace(".popo", "").replace("blogas", "").replace("blog", "").replace(".com", "").replace(".lt", "");
if (name.indexOf(query) > -1) {
for (var i = 0; i < 3; i++) {
console.log(name)
node.transition()
.duration(TIME)
.delay(2 * i * TIME)
.attr("r", 1.3 * RADIUS * d.importance);
node.transition()
.duration(TIME)
.delay((2 * i + 1) * TIME)
.attr("r", RADIUS * d.importance);
}
}
});
}
}
})(jQuery)