-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
190 lines (164 loc) · 4.74 KB
/
index.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
<!-- Interactive D3 Alcohol Consumption Bubble Chart
Data used is from fivethiryeight. -->
<html>
<head>
<link href="index.css" rel="stylesheet">
<link href="colorbrewer/colorbrewer.css" rel="stylesheet">
</head>
<body>
<div id="title"> Interactive D3 Alcohol Consumption Bubble Chart </div>
<div id="legend"></div>
<div id="chart"></div>
<script src="d3.min.js"></script>
<script src="colorbrewer/colorbrewer.js"></script>
<script src="jquery-1.11.1.min.js"></script>
<script src="underscore-min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script>
var width = 960,
height = 500;
var clusters = new Array(10);
var force = d3.layout.force()
.size([width, height])
.on("tick", tick);
var drag = force.drag()
.on("dragstart", dragstart);
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height);
var node = svg.selectAll(".node");
var min = 100000, max = 0, nodeX = width/2, nodeY = height/2;
var colorScale = d3.scale.quantize()
.range(colorbrewer.Paired[10]);
d3.csv("data/drinks.csv", function(data) {
var maxRadius = d3.max(_.pluck(data, 'total_litres_of_pure_alcohol')) * 2;
var dataset = [];
var total = 0;
for(var i = 0; i < data.length; i++) {
var totalLitresValue = parseFloat(data[i]["total_litres_of_pure_alcohol"]);
min = totalLitresValue < min ? totalLitresValue : min;
max = totalLitresValue > max ? totalLitresValue : max;
var datum = {
x: nodeX,
y: nodeY,
radius: totalLitresValue * 2,
country: data[i]["country"],
total: totalLitresValue
};
dataset.push(datum);
var r = Math.sqrt((i + 1) / 10 * -Math.log(Math.random())) * maxRadius;
if (!clusters[i] || (r > clusters[i].radius)) clusters[data[i]["country"]] = datum;
}
colorScale.domain([min, max]);
force.nodes(dataset);
node = node.data(dataset)
.enter().append("circle")
.attr("class", "node")
.attr("r", function (d) {
return d.radius;
})
.attr("fill", function (d) {
return colorScale(d.total);
})
.attr("stroke", "none")
.on("dblclick", dblclick)
.on("mouseover", function (d) {
showTooltip.call(this, d);
d3.select(this).attr("stroke", "black");
d3.select(this).attr("stroke-width", 2);
})
.on("mouseout", function (d) {
removeTooltip();
d3.select(this).attr("stroke", "none");
})
.call(drag);
force.start();
var legend = d3.select('#legend')
.append('ul')
.attr('class', 'list-inline');
var keys = legend.selectAll('li.key')
.data(colorScale.range());
keys.enter().append('li')
.attr('class', 'key')
.style('border-top-color', String)
.text(function(d) {
var r = colorScale.invertExtent(d);
return parseFloat(r[0]).toFixed(2) + "L ";
});
});
function tick(e) {
node
.each(cluster(10 * e.alpha * e.alpha))
.each(collide(0.5))
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
function dblclick(d) {
d3.select(this).classed("fixed", d.fixed = false);
}
function dragstart(d) {
d3.select(this).classed("fixed", d.fixed = true);
}
function removeTooltip () {
$('.popover').each(function() {
$(this).remove();
});
}
function showTooltip (d) {
$(this).popover({
placement: 'auto top',
container: 'body',
trigger: 'manual',
html : true,
content: function() {
return "Country: " + d.country + "<br/>Total Litres of Pure Alcohol per Person per Year: " + d.total; }
});
$(this).popover('show')
}
// Move d to be adjacent to the cluster node.
function cluster(alpha) {
return function(d) {
var cluster = clusters[d.country];
if (cluster === d) return;
var x = d.x - cluster.x,
y = d.y - cluster.y,
l = Math.sqrt(x * x + y * y),
r = d.radius + cluster.radius;
if (l != r) {
l = (l - r) / l * alpha;
d.x -= x *= l;
d.y -= y *= l;
cluster.x += x;
cluster.y += y;
}
};
}
function collide(alpha) {
var quadtree = d3.geom.quadtree(node[0]);
return function(d) {
var r = d.radius + 50,
nx1 = d.x - r,
nx2 = d.x + r,
ny1 = d.y - r,
ny2 = d.y + r;
quadtree.visit(function(quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== d)) {
var x = d.x - quad.point.x,
y = d.y - quad.point.y,
l = Math.sqrt(x * x + y * y),
r = d.radius + quad.point.radius + 50;
if (l < r) {
l = (l - r) / l * alpha;
d.x -= x *= l;
d.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
}
}
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
});
};
}
</script>
</body>
</html>