-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.js
375 lines (311 loc) · 11.8 KB
/
project.js
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
d3.csv("spotify.csv").then(function(data) {
let musicdata = data;
//Select the div
let q3 = d3.select("#viz");
q3.append("svg").attr("width", 800).attr("height", 700).attr("id","chart");
svg = d3.select("svg#chart");
//Set Constant Values
margin = { "top": 20, "right": 50, "bottom": 80, "left":150};
width = svg.attr("width");
height = svg.attr("height");
chartwidth = width - margin["left"] - margin["right"];
chartheight = height - margin["top"]-margin["bottom"];
//Max Values
const eMin = d3.min(musicdata, d => d['energy']);
const eMax = d3.max(musicdata, d => d['energy']);
const duMin = d3.min(musicdata, d => d['duration_ms']);
const duMax = d3.max(musicdata, d => d['duration_ms']);
const liMin = d3.min(musicdata, d => d['liveness']);
const liMax = d3.max(musicdata, d => d['liveness']);
const loMin = d3.min(musicdata, d => d['loudness']);
const loMax = d3.max(musicdata, d => d['loudness']);
d['duration_ms'] = Number(d['duration_ms']);
console.log(duMin)
console.log(duMax)
//Scales for major viz
yScale = d3.scaleLinear().domain([1, 0]).range([0,chartheight]);
xScale = d3.scaleLinear().domain([0, 1000000/360]).range([0, chartwidth]);
rScale = d3.scaleLinear().domain([loMin, loMax]).range([1, 7]);
colorScale = d3.scaleSequential(d3.interpolateCool).domain([0, liMax])
//Legend
svgS = d3.select("#scales");
let initx = 0
let textx = 0
let inity = 25
svgS.append("text")
.attr("x", initx)
.attr("y",40)
.attr("fill","white")
.attr("font-size","20px")
.attr("class","font title")
.text("Spotify Song Data");
initx = initx + 250;
textx = textx + 250;
svgS.append("text")
.attr("x",initx)
.attr("y",inity-3)
.attr("fill","white")
.attr("font-size","14px")
.attr("class","font title")
.text("Song Liveness");
for(k=0; k <= 1;k=k+.2){
svgS.append("rect")
.attr("x", initx)
.attr("y", inity)
.attr("height", 20)
.attr("width", 20)
.attr("fill", colorScale(k));
svgS.append("text")
.attr("x", textx)
.attr("y","55")
.attr("fill","white")
.attr("font-size","10px")
.attr("class","font title")
.text(Math.round(k*10));
initx = initx + 20;
textx = textx + 21;
}
svgS.append("text")
.attr("x", initx+50)
.attr("y",40)
.attr("fill","white")
.attr("font-size","20px")
.attr("class","font title")
.text("X-Scale: Song Duration");
svgS.append("text")
.attr("x", initx+350)
.attr("y",40)
.attr("fill","white")
.attr("font-size","20px")
.attr("class","font title")
.text("Y-Scale: Song Energy");
//Background
svg.append("rect")
.attr("x", margin["left"])
.attr("y", margin["top"])
.attr("height", chartwidth)
.attr("width", chartheight)
.attr("id","viz-background")
.attr("fill", "white");
//Draw all circles
svg.append("g").attr("id","datapoints")
svgb2 = d3.select("g#datapoints");
for(k=0;k < musicdata.length;k++){
if (musicdata[k].duration_ms != 'NaN' && musicdata[k].energy != 'NaN'){
svgb2.append("circle")
.attr("cx",xScale(musicdata[k].duration_ms/360)+margin["left"])
.attr("cy",yScale(musicdata[k].energy)+margin["top"])
.attr("r",rScale(musicdata[k].loudness))
.style("fill", colorScale(musicdata[k].liveness))
.attr("id",musicdata[k].song_title)
.attr("opacity",.7);
}
}
leftAxis = d3.axisLeft(yScale);
svg.append("g")
.attr("stroke-width",1.8)
.attr("transform","translate("+(margin.left)+","+margin.top+")")
.style("font", "12px times")
.attr("class","yscale")
.call(leftAxis.ticks(5));
bottomAxis = d3.axisBottom(xScale)
svg.append("g")
.attr("stroke-width",1.8)
.style("font", "12px times")
.attr("class","xscale")
.attr("transform","translate("+(margin.left)+","+(chartheight + margin.top)+")")
.call(bottomAxis.ticks(5));
//Artist1 Box
svg = d3.select("#DrakeRec");
svg.attr("width", 250).attr("height", 100).attr("id","info");
//Put the artists name here
artists = "Drake"
drake = musicdata.filter( d => d['artist'] == artists);
drake = drake.filter( d => d['duration_ms'] != 'NaN');
//Mean Values of Columns for Drake
const dmeanlo = d3.mean(drake, d => d['loudness']);
const dmeanli = d3.mean(drake, d => d['liveness']);
const dmeandu = d3.mean(drake, d => d['duration_ms']);
const dmeanen = d3.mean(drake, d => d['energy']);
//Artists Name Print along with statistics
svg.append("text")
.attr("x","10")
.attr("y","30")
.attr("fill","crimson")
.attr("font-size","20px")
.attr("class","font title")
.text(artists);
svg.append("text")
.attr("x","10")
.attr("y","55")
.attr("fill","white")
.attr("font-size","14px")
.attr("class","font title")
.text("AVG loudness:" + Math.abs((dmeanlo)));
svg.append("text")
.attr("x","10")
.attr("y","80")
.attr("fill","white")
.attr("font-size","14px")
.attr("class","font title")
.text("AVG energy:" + Math.abs((dmeanen)));
//Smaller Viz of the big viz
svg = d3.select("#drakeballs");
svg.attr("width", 400).attr("height", 250);
//MusicData
musicdata = drake;
//Margins and Chart Attr
margin = { "top": 10, "right": 150, "bottom": 10, "left":10};
width = svg.attr("width");
height = svg.attr("height");
chartwidth = width - margin["left"] - margin["right"];
chartheight = height - margin["top"]-margin["bottom"];
//New Scales for smaller viz
yScale = d3.scaleLinear().domain([1, 0]).range([0,230]);
xScale = d3.scaleLinear().domain([0, 1000000]).range([0, 230]);
rScale = d3.scaleLinear().domain([loMin, loMax]).range([1, 3]);
colorScale = d3.scaleSequential(d3.interpolateCool).domain([0, liMax])
//Append a group with the data points
svg.append("g").attr("id","drakestuffs")
svgb2 = d3.select("g#drakestuffs");
for(k=0;k < musicdata.length;k++){
if (musicdata[k].duration_ms != 'NaN' && musicdata[k].energy != 'NaN'){
svgb2.append("circle")
.attr("cx",xScale(musicdata[k].duration_ms)+margin["left"])
.attr("cy",yScale(musicdata[k].energy)+margin["top"])
.attr("r",rScale(musicdata[k].loudness))
.style("fill", colorScale(musicdata[k].liveness))
.attr("opacity",1);
}
}
leftAxis = d3.axisLeft(yScale);
svg.append("g")
.attr("stroke-width",1.8)
.attr("transform","translate("+(margin.left)+","+margin.top+")")
.style("font", "12px times")
.attr("color","white")
.call(leftAxis.ticks(0));
bottomAxis = d3.axisBottom(xScale)
svg.append("g")
.attr("stroke-width",1.8)
.style("font", "12px times")
.attr("color","white")
.attr("transform","translate("+(margin.left)+","+(chartheight + margin.top)+")")
.call(bottomAxis.ticks(0));
//Floating Balls Next
rScale = d3.scaleLinear().domain([loMin, loMax]).range([1, 15]);
bx = 220
by = 30
for(k=0;k<5;k++){
svg.append("circle")
.attr("cx",bx)
.attr("cy",by)
.attr("r",rScale(musicdata[k].loudness))
.style("fill",colorScale(musicdata[k].liveness))
.attr("opacity",1);
svg.append("text")
.attr("x",bx+20)
.attr("y",by+5)
.attr("fill","white")
.attr("font-size","10px")
.attr("class","font drakeblz")
.text(musicdata[k].song_title);
by = by + 40
}
//FutureViz
svg = d3.select("#KendrickRec");
svg.attr("width", 250).attr("height", 100);
//Artist Name
artists = "Future"
musicdata = data;
future = musicdata.filter( d => d['artist'] == artists);
future = future.filter( d => d['duration_ms'] != 'NaN');
musicdata = future;
//Artist2 Mean Values
const kmeanlo = d3.mean(future, d => d['loudness']);
const kmeanli = d3.mean(future, d => d['liveness']);
const kmeandu = d3.mean(future, d => d['duration_ms']);
const kmeanen = d3.mean(future, d => d['energy']);
//Print Out Artist Info
svg.append("text")
.attr("x","10")
.attr("y","30")
.attr("fill","crimson")
.attr("font-size","20px")
.attr("class","font title")
.text(artists);
svg.append("text")
.attr("x","10")
.attr("y","55")
.attr("fill","white")
.attr("font-size","14px")
.attr("class","font title")
.text("AVG loudness:" + Math.abs((kmeanlo)));
svg.append("text")
.attr("x","10")
.attr("y","80")
.attr("fill","white")
.attr("font-size","14px")
.attr("class","font title")
.text("AVG energy:" + Math.abs((kmeanen)));
//Small Viz
svg = d3.select("#kendrickballs");
svg.attr("width", 400).attr("height", 250);
musicdata = future;
//Margins and Chart Height
margin = { "top": 10, "right": 150, "bottom": 10, "left":10};
width = svg.attr("width");
height = svg.attr("height");
chartwidth = width - margin["left"] - margin["right"];
chartheight = height - margin["top"]-margin["bottom"];
//New Scale
rScale = d3.scaleLinear().domain([loMin, loMax]).range([1, 3]);
//Select Div Element
svg.append("g").attr("id","kstuffs")
svgb2 = d3.select("g#kstuffs");
//Draw Points
for(k=0;k < musicdata.length;k++){
if (musicdata[k].duration_ms != 'NaN' && musicdata[k].energy != 'NaN'){
svgb2.append("circle")
.attr("cx",xScale(musicdata[k].duration_ms)+margin["left"])
.attr("cy",yScale(musicdata[k].energy)+margin["top"])
.attr("r",rScale(musicdata[k].loudness))
.style("fill", colorScale(musicdata[k].liveness))
.attr("opacity",1);
}
}
leftAxis = d3.axisLeft(yScale);
svg.append("g")
.attr("stroke-width",1.8)
.attr("transform","translate("+(margin.left)+","+margin.top+")")
.style("font", "12px times")
.attr("color","white")
.call(leftAxis.ticks(0));
bottomAxis = d3.axisBottom(xScale)
svg.append("g")
.attr("stroke-width",1.8)
.style("font", "12px times")
.attr("color","white")
.attr("transform","translate("+(margin.left)+","+(chartheight + margin.top)+")")
.call(bottomAxis.ticks(0));
//New R Scale for side
rScale = d3.scaleLinear().domain([loMin, loMax]).range([1, 15]);
by = 220
bx = 30
for(k=0;k<5;k++){
svg.append("circle")
.attr("cx",by)
.attr("cy",bx)
.attr("r",rScale(musicdata[k].loudness))
.style("fill",colorScale(musicdata[k].liveness))
.attr("opacity",1);
svg.append("text")
.attr("x",by+20)
.attr("y",bx+5)
.attr("fill","white")
.attr("font-size","10px")
.attr("class","font drakeblz")
.text(musicdata[k].song_title.substr(0, 11));
bx = bx + 40
}
})