forked from garethdandrews/LastFmProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualisation.html
335 lines (283 loc) · 11.4 KB
/
visualisation.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Visualisation</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
svg {
border-style: solid;
border-width: 3px;
}
circle {
fill: #5481c9;
opacity: 50%;
}
div.tooltip {
position: absolute;
text-align: left;
width: 150px;
height: 100px;
padding: 5px;
font: 12px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
body {
font-family: "Arial", sans-serif;
}
.bar {
fill: #5481c9;
}
.axis {
font-size: 13px;
}
.axis path,
.axis line {
fill: none;
display: none;
}
.label {
font-size: 13px;
}
</style>
</head>
<body>
<script>
var width = 1000;
var height = 1000;
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("id", "svg");
var xScale = d3.scale.linear().range([0, width - 60]);
var yScale = d3.scale.linear().range([height - 60, 0]);
var data;
function render() {
console.log(data);
// dataa = data;
xScale.domain(d3.extent(data, function (d) { return d.x; }));
yScale.domain(d3.extent(data, function (d) { return d.y; }));
var circles = svg.selectAll("circle")
.data(data);
circles.enter().append("circle");
circles
.attr("cx", function (d) { return xScale(d.x) + 30; })
.attr("cy", function (d) { return yScale(d.y) + 30; })
.attr("r", 5)
.attr("id", function (d) { return d.name })
.style("fill", function (d) {
var t = d.topTag;
if (t === "post-rock" || t === "punk" || t === "post-punk" || t === "classic rock" || t === "rock" || t === "hard rock" || t === "alternative rock" || t === "alternative" || t === "Progressive rock" || t === "indie rock") {
return "#ff0000";
} else if (t === "pop" || t === "indie" || t === "Lo-Fi" || t === "" || t === "funk" || t === "latin" || t === "soul" || t === "k-pop") {
return "#5bbf3b";
} else if (t === "Melodic Death Metal" || t === "Power metal" || t === "black metal" || t === "metalcore" || t === "Progressive metal" || t === "heavy metal" || t === "thrash metal" || t === "death metal") {
return "black";
} else if (t === "industrial" || t === "trance" || t === "electronic" || t === "Drum and bass" || t === "House" || t === "dance" || t === "dubstep" || t === "synthwave") {
return "#ff9000";
} else if (t === "Hip-Hop" || t === "rap") {
return "blue";
} else if (t === "jazz") {
return "#ffe500";
} else if (t === "folk" || t === "country" || t === "world" || t === "italian") {
return "brown";
} else if (t === "Classical" || t === "Soundtrack" || t === "ambient") {
return "#07f7e7";
} else if (t === "reggae" || t === "ska") {
return "#e307f7";
} else {
return "#a9aab2";
}
})
.style("fill-opacity", .8) // set the fill opacity
.style("stroke", "black") // set the line colour
.on("mouseover", function (d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html(
"<b>" + d.name + "</b><br> " + d.topTag + "</br>" +
" <b>1:</b> " + d.artists[0].name + "</br>" +
" <b>2:</b> " + d.artists[1].name + "</br>" +
" <b>3:</b> " + d.artists[2].name + "</br>" +
" <b>4:</b> " + d.artists[3].name + "</br>" +
" <b>5:</b> " + d.artists[4].name + "</br>"
)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function (d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
circles.on("click", function (d) {
var nextColor = this.style.fill == "red" ? "#5481c9" : "red";
// sd3.select(this).style("fill", nextColor);
d3.select(this)
.attr("r", 12)
.style("fill-opacity", 1);
generateBarChart(d.name);
});
circles.exit().remove();
}
function type(d) {
d.x = +d.x;
d.y = +d.y;
return d;
}
d3.json("VisualAnalyticsProject/CosineMDSPointsTopTagsArtists1000.json", function (e, d) {
data = d;
render();
});
// d3.csv("VisualAnalyticsProject/CosineMDSPointsTopTagsArtists1000.csv", type, render);
function clicked() {
var username = $("#username").val();
var circles = svg.selectAll("circle")
.data(dataa.filter(function (d) {
return d.name == username;
}))
.attr("r", 12)
.style("fill-opacity", 1);
generateBarChart(username);
}
function openInNewTab(name) {
var url = "https://www.last.fm/user/" + name + "/library/artists";
var win = window.open(url, '_blank');
win.focus();
}
function generateBarChart(user) {
// console.log(data.filter(function (d) {
// return d.name == user;
// }));
var u = data.filter(function (d) {
return d.name == user;
});
console.log(u[0]);
// // var barchart = document.getElementById("barchart");
// // barchart.parentNode.removeChild(barchart);
alert("yoop yoodel my noodle hang on");
// // setTimeout(function () {
//sort bars based on value
var artists = u[0].artists.sort(function (a, b) {
return d3.ascending(a.playcount, b.playcount);
})
var url = "https://www.last.fm/user/" + user + "/library/artists";
document.getElementById('charts').innerHTML = "<h2><a href=" + url + ">" + user + "</a></h2>"
//set up svg using margin conventions - we'll need plenty of room on the left for labels
var margin = {
top: 15,
right: 25,
bottom: 15,
left: 250
};
var width2 = 1000 - margin.left - margin.right,
height2 = 1000 - margin.top - margin.bottom;
var svg = d3.select("#charts").append("svg")
.attr("width", width2 + margin.left + margin.right)
.attr("height", height2 + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("id", "barchart");
var x = d3.scale.linear()
.range([0, width2])
.domain([0, d3.max(artists, function (d) {
return d.playcount;
})]);
var y = d3.scale.ordinal()
.rangeRoundBands([height2, 0], .1)
.domain(artists.map(function (d) {
return d.name;
}));
//make y axis to show bar names
var yAxis = d3.svg.axis()
.scale(y)
//no tick marks
.tickSize(0)
.orient("left");
var gy = svg.append("g")
.attr("class", "y axis")
.call(yAxis)
var bars = svg.selectAll(".bar")
.data(artists)
.enter()
.append("g")
//append rects
bars.append("rect")
.attr("class", "bar")
.attr("y", function (d) {
return y(d.name);
})
.attr("height", y.rangeBand())
.attr("x", 0)
.attr("width", function (d) {
return x(d.playcount);
})
.on("mouseover", function (d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html(d.playcount)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function (d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
//add a value label to the right of each bar
bars.append("text")
.attr("class", "label")
//y position of the label is halfway down the bar
.attr("y", function (d) {
return y(d.name) + y.rangeBand() / 2 + 4;
})
//x position is 3 pixels to the right of the bar
.attr("x", function (d) {
return x(d.playcount) + 3;
})
.text(function (d) {
return d.playcount;
});
// // }, 1000);
}
function getTopArtists(user) {
var artists = [];
$.ajax({
type: 'POST',
url: 'http://ws.audioscrobbler.com/2.0/',
data: 'method=user.gettopartists&' +
'user=' + user + '&' +
'period=overall&' +
'api_key=57ee3318536b23ee81d6b27e36997cde&' +
'format=json',
dataType: 'jsonp',
success: function (data) {
$.each(data.topartists.artist, function (index, element) {
artists.push({ name: element.name, playcount: element.playcount });
});
},
error: function (code, message) {
console.log(code + message);
}
});
return artists;
}
</script>
<br>
Username: <input id="username" type="text">
<button onclick="clicked()">Highlight User</button><br>
<br>
<div id="charts">
</div>
</body>
</html>