forked from ezyang/metromaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
267 lines (232 loc) · 5.85 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!DOCTYPE html>
<!-- Cribbed off of http://mbostock.github.com/d3/talk/20110921/#16 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Force Layouts - Foci</title>
<script src="http://d3js.org/d3.v2.js"></script>
<style type="text/css">
body {
overflow: hidden;
margin: 0;
font: 14px "Helvetica Neue";
}
svg {
width: 1280px;
height: 800px;
}
#chart, #header {
position: absolute;
top: 0;
}
#header {
z-index: 1;
display: block;
}
#header {
top: 80px;
left: 140px;
font: 300 36px "Helvetica Neue";
}
rect {
fill: none;
pointer-events: all;
}
pre {
font-size: 18px;
}
line {
stroke: #000;
stroke-width: 1.5px;
}
.string, .regexp {
color: #f39;
}
.keyword {
color: #00c;
}
.comment {
color: #555;
}
.number {
color: #369;
}
.class, .special {
color: #1181B8;
}
circle {
stroke: #fff;
}
</style>
</head>
<body>
<div id="body">
<div id="chart"></div>
<div id="header">angular forces</div>
</div>
<script type="text/javascript">
var w = 1280,
h = 800,
color = d3.scale.category10();
var force = d3.layout.force()
.gravity(0.1)
.charge(-20)
.size([w, h])
.linkDistance(function(d) {return d.weight * 20})
.linkStrength(1);
var force2 = d3.layout.force()
.gravity(0.1)
.charge(function(d) {
function mkd(i) {return norm([d.x-d.reln[i].x, d.y-d.reln[i].y])}
return -2 * (mkd(0) + mkd(1) + mkd(2));
})
.size([w, h]);
var links = force.links(),
nodes = force.nodes(),
nodes2 = force2.nodes();
var svg = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h);
svg.append("svg:rect")
.attr("width", w)
.attr("height", h);
/*
svg.selectAll("circle")
.data(centers)
.enter().append("svg:circle")
.attr("r", 12)
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.style("fill", fill)
.call(force.drag);
*/
// I cannae believe D3.js has no vector operations
var r2 = 1/Math.sqrt(2);
var angles = [[1,0],[r2,r2],[0,1],[-r2,r2],[-1,0],[-r2,-r2],[0,-1],[r2,-r2]];
function dp(p1, p2) {
return p1[0] * p2[0] + p1[1] * p2[1];
}
function midpoint(p1,p2) {
return [(p1[0]+p2[0])/2, (p1[1]+p2[1])/2];
}
function scale(k, p) {
return [k*p[0], k*p[1]];
}
function norm(p) {
return Math.sqrt(p[0]*p[0]+p[1]*p[1]);
}
function vec(n) {
return [n.x,n.y];
}
function mymax(as, f) {
var m = as[0], fm = f(as[0]);
as.forEach(function(a) {var fa = f(a); if (fa > fm) {m = a; fm = fa}});
return m;
}
var hotcolor = d3.scale.linear().domain([1,4]).range(["black", "#FF6666"]);
force.on("tick", function(e) {
var k = 1; // e.alpha * 1;
links.forEach(function(link) {
// link.source, link.target
var p1 = link.source, p2 = link.target;
var v = [p2.x-p1.x, p2.y-p1.y];
var orthonormal = mymax(angles, function(x) {return dp(x, v);});
var mp = midpoint(vec(p1),vec(p2));
var adjust = scale(norm(v)/2, orthonormal);
p1.x += (mp[0] - adjust[0] - p1.x) * k;
p1.y += (mp[1] - adjust[1] - p1.y) * k;
p2.x += (mp[0] + adjust[0] - p2.x) * k;
p2.y += (mp[1] + adjust[1] - p2.y) * k;
});
var k = 0.3;
nodes2.forEach(function(n) {
var sx = (n.reln[0].x + n.reln[1].x + n.reln[2].x)/3 - n.x;
var sy = (n.reln[0].y + n.reln[1].y + n.reln[2].y)/3 - n.y;
n.reln[0].x -= sx * k;
n.reln[1].x -= sx * k;
n.reln[2].x -= sx * k;
n.reln[0].y -= sy * k;
n.reln[1].y -= sy * k;
n.reln[2].y -= sy * k;
});
svg.selectAll("circle")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", function(d) {
// XXX copy
if (d.type == 3) {
function mkd(i) {return norm([d.x-d.reln[i].x, d.y-d.reln[i].y])}
return 0.1 * (mkd(0) + mkd(1) + mkd(2));
} else {
return 4.5;
}
});
svg.selectAll("line")
.style("stroke", function(d) { return hotcolor(d.weight) } )
.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; });
});
var p0;
var wait = 10000;
svg.on("mousemove", function() {
var p1 = d3.svg.mouse(this),
a = {type: 0, x: p1[0], y: p1[1], px: (p0 || (p0 = p1))[0], py: p0[1]},
b = {type: 1},
c = {type: 2},
d = {type: 3, x: p1[0], y: p1[1], reln: [a,b,c]},
l1 = {source: a, target: b, weight: (Math.random() * 4 | 0) + 1 },
l2 = {source: b, target: c, weight: (Math.random() * 4 | 0) + 1 },
l3 = {source: c, target: a, weight: (Math.random() * 4 | 0) + 1 };
p0 = p1;
svg.selectAll()
.data([a, b, c, d])
.enter().append("svg:circle")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", 4.5)
.style("fill", fill)
.transition()
.delay(wait)
.attr("r", 1e-6)
.remove();
// link identity not maintained...
svg.insert("svg:line", "circle")
.data([l2])
.transition()
.delay(wait)
.remove();
svg.insert("svg:line", "circle")
.data([l3])
.transition()
.delay(wait)
.remove();
svg.insert("svg:line", "circle")
.data([l1])
.transition()
.delay(wait)
.each("end", function() {
nodes.splice(nodes.indexOf(a), 1);
nodes.splice(nodes.indexOf(b), 1);
nodes.splice(nodes.indexOf(c), 1);
nodes2.splice(nodes2.indexOf(d), 1);
links.splice(links.indexOf(l1), 1);
links.splice(links.indexOf(l2), 1);
links.splice(links.indexOf(l3), 1);
force.start();
force2.start();
})
.remove();
nodes.push(a, b, c);
nodes2.push(d);
links.push(l1, l2, l3);
force.start();
force2.start();
});
function fill(d) {
return color(d.type);
}
</script>
</body>
</html>