-
Notifications
You must be signed in to change notification settings - Fork 1
/
hypem.html
214 lines (191 loc) · 6.69 KB
/
hypem.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>hypem Loved Songs</title>
<link rel="shortcut icon" href="../common/delimited-favicon-v4.ico">
<link href="css/bootstrap.css" rel="stylesheet">
<style type="text/css">
body {
background-color: white;
padding-left: 50px;
}
circle {
stroke: white;
stroke-width: 1px;
opacity: .8;
}
.btn-group {
margin-left: 390px;
}
.label {
fill: white;
font-size: 16px;
}
</style>
</head>
<body>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default active" id="all">
<input type="radio" name="options"> All Songs
</label>
<label class="btn btn-default" id="duration">
<input type="radio" name="options"> Sort By Duration
</label>
</div>
<div id="chart"></div>
<script src="lib/d3.js"></script>
<script src="lib/jquery.js"></script>
<script src="lib/bootstrap.js"></script>
<script src="lib/underscore.js"></script>
TODO: <br/>
make songs play <br/>
change time to minutes and seconds <br/>
<script>
d3.json('http://hypem.com/playlist/popular/3day/json/1/data.js', function (error, data) {
debugger;
data = _.toArray(data);
console.log(data);
//removes version which is stored in last index
data.pop();
// console.log(data)
// var width = 1000, height = 1000;
var width = window.innerWidth, height = window.innerHeight;
//use this for dynamically changing color based on some data
var fill = d3.scale.ordinal()
.domain(["short", "medium", "long"])
.range(["#009999", "#1240ab", "#00CC00"])
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height);
var max_amount = d3.max(data, function (d) { return parseInt(d.loved_count)})
var radius_scale = d3.scale.pow().exponent(0.5).domain([0, max_amount]).range([2, 85])
_.each(data, function (elem) {
if (elem.time < 180) {
elem.duration = 'short';
} else if (elem.time < 300) {
elem.duration = 'medium'
} else {
elem.duration = 'long'
}
elem.radius = radius_scale(elem.loved_count)*.8;
elem.all = 'all';
elem.x = _.random(0, width);
elem.y = _.random(0, height);
})
var padding = 4;
var maxRadius = d3.max(_.pluck(data, 'radius'));
var time_centers = {
"short": {name: "short", x: 150, y: 300},
"medium": {name:"medium", x: 525, y: 300},
"long": {name:"long", x: 900, y: 300}
}
var all_center = { "all": {name:"All Songs", x: 500, y: 300}};
var nodes = svg.selectAll("circle")
.data(data);
nodes.enter().append("circle")
.attr("class", "node")
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; })
.attr("r", 1)
//group color
// .style("fill", function (d) { return fill(d.group); })
.style("fill", function (d) { return fill(d.duration); })
// .style("fill", d3.hsl(Math.random()*360,.5,.5))
.on("mouseover", function (d) { showPopover.call(this, d); })
.on("mouseout", function (d) { removePopovers(); })
//delay for circle radius growth
nodes.transition().delay(100).duration(3000)
.attr("r", function (d) { return d.radius; })
var force = d3.layout.force()
.charge(0)
.gravity(0)
.size([width, height])
draw('all');
$( ".btn" ).click(function() {
draw(this.id);
});
function draw (varname) {
debugger;
var foci = varname === "all" ? all_center : time_centers;
force.on("tick", tick(foci, varname, .55));
labels(foci)
force.start();
}
function tick (foci, varname, k) {
return function (e) {
_.each(data, function (o,i) {
var f = foci[o[varname]];
o.y += (f.y - o.y) * k * e.alpha;
o.x += (f.x - o.x) * k * e.alpha;
});
nodes
.each(collide(.1))
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; });
}
}
function labels (foci) {
svg.selectAll(".label").remove();
svg.selectAll(".label")
.data(_.toArray(foci)).enter().append("text")
.attr("class", "label")
.text(function (d) { return d.name })
.attr("transform", function (d) {
return "translate(" + (d.x - ((d.name.length)*3)) + ", " + (d.y - 275) + ")";
});
}
function removePopovers () {
$('.popover').each(function() {
$(this).remove();
});
}
function showPopover (d) {
$(this).popover({
placement: 'auto bottom',
container: 'body',
trigger: 'manual',
html : true,
content: function() {
return 'Artist: ' + d.artist +
'<br/>Title: ' + d.title +
'<br/>Loved Count: ' + d.loved_count +
'<br/>Time: ' + d.time +
'<br/><img src="' + d.thumb_url_medium + '"/>'
}
});
$(this).popover('show')
}
function collide(alpha) {
var quadtree = d3.geom.quadtree(data);
return function(d) {
var r = d.radius + maxRadius + padding,
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 + padding;
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;
});
};
}
});
// $('body').append('audio controls');
// $('audio controls autoplay loop').attr('src',null);
</script>
</body>
</html>