-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbles.html
319 lines (262 loc) · 9.36 KB
/
bubbles.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script type='text/javascript' src='http://d3js.org/d3.v3.min.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type='text/css'>
g.axis path, g.axis line {
fill:none;
stroke:royalblue;
}
g.axis text, g.bubbles text {
fill:royalblue;
font-family:sans-serif;
}
g.bubbles line {
stroke-width:5;
stroke:royalblue;
}
g.bubbles circle {
fill:rgba(255,0,64,0.5);
stroke:rgb(255,0,64);
stroke-width:3;
}
g.bubbles text {
text-anchor:middle;
alignment-baseline:middle;
opacity:0;
pointer-events:all;
transition:1s;
}
g.bubbles text:hover {
opacity:1;
}
g.threads line {
fill:none;
stroke:navy;
opacity:0.5;
}
</style>
<script type='text/javascript'>//<![CDATA[
window.onload=function(){
(function() {
//D3 program to fit circles of different sizes along a
//horizontal dimension, shifting them up and down
//vertically only as much as is necessary to make
//them all fit without overlap.
//By Amelia Bellamy-Royds, in response to
//http://stackoverflow.com/questions/20912081/d3-js-circle-packing-along-a-line
//inspired by
//http://www.nytimes.com/interactive/2013/05/25/sunday-review/corporate-taxes.html
//Freely released for any purpose under Creative Commons Attribution licence: http://creativecommons.org/licenses/by/3.0/
//Author name and link to this page is sufficient attribution.
//create data array//
var data = [];
var N = 25, i = N;
var randNorm = d3.random.normal(0.5,0.2)
while(i--)data.push({
x:randNorm(),
r:Math.random()});
//x for x-position
//r for radius; value will be proportional to area
//________________//
//Set up SVG and axis//
var svg = d3.select("svg");
var digits = /(\d*)/;
var margin = 50; //space in pixels from edges of SVG
var padding = 4; //space in pixels between circles
var maxRadius = 25;
var biggestFirst = true; //should largest circles be added first?
var width = window.getComputedStyle(svg[0][0])["width"];
width = digits.exec(width)[0];
var height = window.getComputedStyle(svg[0][0])["height"];
height = Math.min(digits.exec(height)[0], width);
var baselineHeight = (margin + height)/2;
var xScale = d3.scale.linear()
.domain([0,1])
.range([margin,width-margin]);
var rScale = d3.scale.sqrt()
//make radius proportional to square root of data r
.domain([0,1])
.range([1,maxRadius]);
var formatPercent = d3.format(".0%");
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("top")
.ticks(5)
.tickFormat(formatPercent);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + margin + ")")
.call(xAxis);
var threads = svg.append("g")
.attr("class", "threads");
var bubbleLine = svg.append("g")
.attr("class", "bubbles")
.attr("transform",
"translate(0," + baselineHeight + ")");
bubbleLine.append("line")
.attr("x1", xScale.range()[0])
.attr("x2", xScale.range()[1]);
//________________//
//Create Quadtree to manage data conflicts & define functions//
var quadtree = d3.geom.quadtree()
.x(function(d) { return xScale(d.x); })
.y(0) //constant, they are all on the same line
.extent([[xScale(-1),0],[xScale(2),0]]);
//extent sets the domain for the tree
//using the format [[minX,minY],[maxX, maxY]]
//optional if you're adding all the data at once
var quadroot = quadtree([]);
//create an empty adjacency tree;
//the function returns the root node.
// Find the all nodes in the tree that overlap a given circle.
// quadroot is the root node of the tree, scaledX and scaledR
//are the position and dimensions of the circle on screen
//maxR is the (scaled) maximum radius of dots that have
//already been positioned.
//This will be most efficient if you add the circles
//starting with the smallest.
function findNeighbours(root, scaledX, scaledR, maxR) {
var neighbours = [];
//console.log("Neighbours of " + scaledX + ", radius " + scaledR);
root.visit(function(node, x1, y1, x2, y2) {
//console.log("visiting (" + x1 + "," +x2+")");
var p = node.point;
if (p) { //this node stores a data point value
var overlap, x2=xScale(p.x), r2=rScale(p.r);
if (x2 < scaledX) {
//the point is to the left of x
overlap = (x2+r2 + padding >= scaledX-scaledR);
/*console.log("left:" + x2 + ", radius " + r2
+ (overlap?" overlap": " clear"));//*/
}
else {
//the point is to the right
overlap = (scaledX + scaledR + padding >= x2-r2);
/*console.log("right:" + x2 + ", radius " + r2
+ (overlap?" overlap": " clear"));//*/
}
if (overlap) neighbours.push(p);
}
return (x1-maxR > scaledX + scaledR + padding)
&& (x2+maxR < scaledX - scaledR - padding) ;
//Returns true if none of the points in this
//section of the tree can overlap the point being
//compared; a true return value tells the `visit()` method
//not to bother searching the child sections of this tree
});
return neighbours;
}
function calculateOffset(maxR){
return function(d) {
neighbours = findNeighbours(quadroot,
xScale(d.x),
rScale(d.r),
maxR);
var n=neighbours.length;
//console.log(j + " neighbours");
var upperEnd = 0, lowerEnd = 0;
if (n){
//for every circle in the neighbour array
// calculate how much farther above
//or below this one has to be to not overlap;
//keep track of the max values
var j=n, occupied=new Array(n);
while (j--) {
var p = neighbours[j];
var hypoteneuse = rScale(d.r)+rScale(p.r)+padding;
//length of line between center points, if only
// "padding" space in between circles
var base = xScale(d.x) - xScale(p.x);
// horizontal offset between centres
var vertical = Math.sqrt(Math.pow(hypoteneuse,2) -
Math.pow(base, 2));
//Pythagorean theorem
occupied[j]=[p.offset+vertical,
p.offset-vertical];
//max and min of the zone occupied
//by this circle at x=xScale(d.x)
}
occupied = occupied.sort(
function(a,b){
return a[0] - b[0];
});
//sort by the max value of the occupied block
//console.log(occupied);
lowerEnd = upperEnd = 1/0;//infinity
j=n;
while (j--){
//working from the end of the "occupied" array,
//i.e. the circle with highest positive blocking
//value:
if (lowerEnd > occupied[j][0]) {
//then there is space beyond this neighbour
//inside of all previous compared neighbours
upperEnd = Math.min(lowerEnd,
occupied[j][0]);
lowerEnd = occupied[j][1];
}
else {
lowerEnd = Math.min(lowerEnd,
occupied[j][1]);
}
//console.log("at " + formatPercent(d.x) + ": "
// + upperEnd + "," + lowerEnd);
}
}
//assign this circle the offset that is smaller
//in magnitude:
return d.offset =
(Math.abs(upperEnd)<Math.abs(lowerEnd))?
upperEnd : lowerEnd;
};
}
//Create circles!//
var maxR = 0;
bubbleLine.selectAll("circle")
.data(data.sort(
biggestFirst?
function(a,b){return b.r - a.r;} :
function(a,b){return a.r - b.r;}
))
.enter()
.append("circle")
.attr("r", function(d){
var r=rScale(d.r);
maxR = Math.max(r,maxR);
return r;})
.each(function(d, i) {
//for each circle, calculate it's position
//then add it to the quadtree
//so the following circles will avoid it.
//console.log("Bubble " + i);
var scaledX = xScale(d.x);
d3.select(this)
.attr("cx", scaledX)
.attr("cy", -baselineHeight + margin)
.transition().delay(300*i).duration(250)
.attr("cy", calculateOffset(maxR));
quadroot.add(d);
bubbleLine.append("text")
.attr("x", scaledX)
.attr("y", d.offset)
.text(i);
//add a drop line from the centre of this
//circle to the axis
threads.append("line").datum(d)
.attr({x1:scaledX, x2:scaledX, y2:margin})
.attr("y1", margin)
.transition().delay(300*i).duration(250)
.attr("y1", (baselineHeight+d.offset));
});
})();
}//]]>
</script>
</head>
<body>
<svg></svg>
</body>
</html>