-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoutput.html
1645 lines (1399 loc) · 69.1 KB
/
output.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html>
<head>
<meta Content-Type:text/html; charset="UTF-8">
<title>SnuInfoVis Term Project - GPS</title>
<link rel="stylesheet" href="stylesheets/radar-chart.css">
<script type="text/javascript" src="http://d3js.org/d3.v6.min.js"></script>
<script type="text/javascript" src="javascripts/radar-chart.js"></script>
</head>
<style>
#tooltip-table {
position: absolute;
border: solid;
border-width: 2px;
border-radius: 4px 4px 4px 4px;
background-color: white;
padding: 5px;
text-align: left;
font-size: 11px;
top: 10%;
left: 10%;
width: 80%;
height: 80%;
padding-bottom: 40px;
}
#tooltip-table-close-button {
float: right;
display: block;
width: 30px;
height: 30px;
}
#tooltip-table-title {
font-size: xx-large;
display: block;
text-align: center;
padding-top: 20px;
padding-bottom: 20px;
}
#tooltip-table-div {
height: 80%;
overflow-y: scroll;
}
/* #tooltip-table-div::-webkit-scrollbar {
display: none;
} */
#tooltip-table-main {
font-size: x-large;
display: block;
margin: auto;
width: 90%;
}
.tooltip-table-main-row {
font-size: medium;
}
.tooltip-table-button {
font-size: large;
height: 50px;
width: 200px;
margin-bottom: 20px;
margin-left: 30px;
margin-right: 30px;
}
#tooltip-cards-div {
height: 80%;
overflow-y: scroll;
}
#tooltip-cards-main {
display: block;
margin: auto;
width: 90%;
}
#tooltip-radar-chart {
border: solid;
border-width: 2px;
border-radius: 4px 4px 4px 4px;
background-color: white;
}
</style>
<script>
let SVG_AREA_WIDTH = 500;
let SVG_AREA_WIDTH_LINE = 1000;
let SVG_AREA_HEIGHT = 500;
let AREA_MARGINS = {TOP:20, RIGHT:20, BOTTOM:30, LEFT:40};
let AREA_WIDTH = SVG_AREA_WIDTH - AREA_MARGINS.LEFT - AREA_MARGINS.RIGHT;
let AREA_WIDTH_LINE = SVG_AREA_WIDTH_LINE - AREA_MARGINS.LEFT - AREA_MARGINS.RIGHT;
let AREA_HEIGHT = SVG_AREA_HEIGHT - AREA_MARGINS.TOP - AREA_MARGINS.BOTTOM;
let GENRE_CATEGORY = ['k-pop', 'hiphop', 'classic', 'rock', 'r&b', 'jazz', 'country', 'pop']
let mData; // load json
let mMinYear = 1970; // fixed
let mMaxYear = 0;
let mSelectedYear = 1991;
let mSelectedGenre = GENRE_CATEGORY[3];
let mSelectedIndex = "danceability";
let mSelectedPopularity = 72;
let mAreaColorToRestore;
let mAreaSelected;
let initViolinPlot =1;
let linetototo;
let lineX;
let lineY;
let lineXAxis;
let lineYAxis;
d3.json("data.json").then(function(data) {
mData = data;
init();
});
function init() {
console.log("mData size : " + mData.length);
d3.select("h1").text("GPS - Guidance to Popular Songs!");
// update year range from data.
for (let i = 0 ; i < mData.length ; i++) {
let year = mData[i].date.substring(0, 4);
// mMinYear = Math.min(mMinYear, parseInt(year)); // mMinYear needs to be fixed.
mMaxYear = Math.max(mMaxYear, parseInt(year));
}
drawAreaChart();
drawViolinPlot();
initdrawLineChart();
drawTable();
}
// -------------------- UTIL apis --------------------
function translate(x, y) {
return 'translate(' + x + ', ' + y + ')';
}
function getSongById(id) {
for (let i = 0 ; i < mData.length ; i++) {
if (id == mData[i].id) {
return mData[i];
}
}
}
function getSongsByYear(year) {
let ret = [];
for (let i = 0 ; i < mData.length ; i++) {
if (mData[i].date.substring(0, 4) == year) {
ret.push(mData[i]);
}
}
return ret;
}
let colors = ["DarkSalmon" , "DarkKhaki", "DarkSeaGreen" , "#7DCEA0", "#48C9B0", "#5DADE2","#BB8FCE", "Plum"]
function getColorByGenre(genre) {
for (let i = 0 ; i < GENRE_CATEGORY.length ; i++) {
if (genre == GENRE_CATEGORY[i]) {
//return d3.schemeCategory10[i];
return colors[i];
}
}
}
function getLineData(mSelectedYear,mSelectedGenre)
{
// console.log("getLineData in");
// console.log(mData);
let newData;
let newData2 = [];
let newData3 = [];
newData = getSongsByYear(mSelectedYear);
for (let i=0; i< newData.length; i++){
if(newData[i].genre==mSelectedGenre)
{
newData2.push(newData[i]);
}
}
return newData2;
}
function getLineDataIndex(linedata,mSelectedIndex)
{
// console.log("getLineDataIndex in");
// console.log(linedata);
if(mSelectedIndex=="tempo")
{
let ret=[];
let minpop = d3.min(linedata,d=>d.popularity);
let maxpop = d3.max(linedata,d=>d.popularity);
console.log(minpop);
console.log(maxpop);
linedata.forEach(function(d){
d.tempo = parseFloat(d.tempo);
})
let minn= d3.min(linedata,d=>d.tempo);
let maxx = d3.max(linedata,d=>d.tempo);
console.log(linedata);
console.log(minn);
console.log(maxx);
for(let i=minpop; i<=maxpop; i++)
{
let temp=[];
let mean=0.0;
for(let j=0; j<linedata.length;j++)
{
if(linedata[j].popularity==i)
{
temp.push(linedata[j]);
}
}
for(let k=0;k<temp.length;k++)
{
mean= (mean+parseFloat(temp[k][mSelectedIndex])-minn)/maxx;
}
if(temp.length>0)
{
ret.push({"id":mSelectedYear+mSelectedGenre+mSelectedIndex+i.toString(),"popularity":parseInt(i),"mean":Math.round(mean/temp.length*10000)/10000});
}
}
// console.log(ret);
return ret;
}
else
{
let ret=[];
let minpop = d3.min(linedata,d=>d.popularity);
let maxpop = d3.max(linedata,d=>d.popularity);
// console.log(minpop);
// console.log(maxpop);
for(let i=minpop; i<=maxpop; i++)
{
let temp=[];
let mean=0.0;
for(let j=0; j<linedata.length;j++)
{
if(linedata[j].popularity==i)
{
temp.push(linedata[j]);
}
}
for(let k=0;k<temp.length;k++)
{
mean= mean+parseFloat(temp[k][mSelectedIndex]);
// console.log("i is "+ i.toString())
// console.log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
// console.log(mean);
}
if(temp.length>0)
{
ret.push({"id":mSelectedYear+mSelectedGenre+mSelectedIndex+i.toString(),"popularity":parseInt(i),"mean":Math.round(mean/temp.length*10000)/10000});
}
}
// console.log(ret);
return ret;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// -------------------- DRAWING APIs --------------------
function drawAreaChart() {
let areaChartSvg = d3.select('#area');
// axis
let areaX = d3.scaleLinear().domain([mMinYear, mMaxYear + 1 /* +1 for 2020 data */ ])
.range([0, AREA_WIDTH]);
let areaXAxis = d3.axisBottom(areaX).tickFormat(d3.format("d"));
areaChartSvg.append("g").attr('transform', translate(AREA_MARGINS.LEFT,
AREA_HEIGHT + AREA_MARGINS.TOP)).call(areaXAxis);
let areaY = d3.scaleLinear().domain([100, 0]).range([0, AREA_HEIGHT]);
let areaYAxis = d3.axisLeft(areaY);
areaChartSvg.append("g").attr('transform', translate(AREA_MARGINS.LEFT, AREA_MARGINS.TOP))
.call(areaYAxis);
let xUnit = AREA_WIDTH / (mMaxYear - mMinYear + 1);
let yUnit = AREA_HEIGHT / 100;
let yBase = AREA_HEIGHT + AREA_MARGINS.TOP;
// prepare prev values
let prevSongs = getSongsByYear(mMinYear - 1);
let prevData = {};
// generate keys
for (let i = 0 ; i < GENRE_CATEGORY.length ; i++) {
prevData[ GENRE_CATEGORY[ i ] ] = 0;
}
// area tooltips
let areaTooltip = d3.select('body').append('div').style('position', 'absolute')
.style('visibility', 'hidden').style('color', 'gray');
let areaTooltipFixed = d3.select('body').append("div").style('position', 'absolute')
.style("top", 350 + "px").style("left", 210 + "px").style("visibility", "hidden")
.text(mSelectedYear + ', ' + mSelectedGenre);
// get counts per key(genre)
for (let i = 0 ; i < prevSongs.length ; i++) {
for (let j = 0 ; j < GENRE_CATEGORY.length ; j++) {
if (prevSongs[i].genre == GENRE_CATEGORY[j]) {
prevData[ GENRE_CATEGORY[j] ]++;
}
}
}
// normalize and calculate height values
for (let i in prevData) {
prevData[i] = ((prevData[i] / prevSongs.length) * 100) * yUnit;
}
for (let i = mMinYear ; i <= mMaxYear ; i++) {
// prepare cur values
let curSongs = getSongsByYear(i);
let curData = {};
// generate keys
for (let j = 0 ; j < GENRE_CATEGORY.length ; j++) {
curData[ GENRE_CATEGORY[j] ] = 0;
}
// get counts
for (let j = 0 ; j < curSongs.length ; j++) {
for (let k = 0 ; k < GENRE_CATEGORY.length ; k++) {
if (curSongs[j].genre == GENRE_CATEGORY[k]) {
curData[ GENRE_CATEGORY[k] ]++;
}
}
}
// normalize and calculate height values
for (let j = 0 ; j < GENRE_CATEGORY.length ; j++) {
let counts = curData[ GENRE_CATEGORY[j] ];
curData[ GENRE_CATEGORY[j] ] = ((counts / curSongs.length) * 100) * yUnit;
}
let prevX = AREA_MARGINS.LEFT + 1/* for y axis line */ + (xUnit * (i - mMinYear));
let curX = prevX + xUnit;
let accPrevY = 0;
let accCurY = 0;
// line with fill from prev to cur
for (let j = 0 ; j < GENRE_CATEGORY.length ; j++) {
let curGenre = GENRE_CATEGORY[j];
let curColor = getColorByGenre(curGenre);
let dStr = 'M ' + prevX + ' ' + (yBase - accPrevY) +
' V ' + (yBase - accPrevY - (prevData[ curGenre ])) +
' L ' + curX + ' ' + (yBase - accCurY - (curData[ curGenre ])) +
' V ' + (yBase - accCurY);
areaChartSvg.append('path').attr('fill', curColor).attr('id', 'genre_' + j)
.attr('stroke', curColor).attr('d', dStr).style('opacity', 0)
.on('mouseenter', function() {
if (d3.select(this).attr('class') != 'selected') {
d3.select(this).attr('fill', d3.rgb(curColor).darker(1));
}
return areaTooltip.style("visibility", "visible").text(i + ', ' + curGenre);
}).on('mousemove', function() {
return areaTooltip.style("top", (event.pageY - 10) + "px")
.style("left", ( event.pageX + 10 ) + "px")
.text(i + ', ' + curGenre);
}).on('mouseleave', function() {
if (d3.select(this).attr('class') != 'selected') {
d3.select(this).attr('fill', curColor);
}
return areaTooltip.style("visibility", "hidden");
}).on('click', function() {
console.log('mouse click(year : ' + i + ', genre : ' + curGenre + ')');
// restore the prev
mAreaSelected.attr('fill', mAreaColorToRestore);
mAreaSelected.attr('class', '');
// set new one to be selected
mAreaSelected = d3.select(this);
mAreaColorToRestore = curColor;
mAreaSelected.attr('class', 'selected');
mAreaSelected.attr('fill', d3.rgb(curColor).darker(2));
mSelectedYear = i;
mSelectedGenre = curGenre;
drawViolinPlot();
return areaTooltipFixed.style("top", (event.pageY - 10) + "px")
.style("left", ( event.pageX + 10 ) + "px")
.style("visibility", "visible").text(i + ', ' + curGenre);
}).each(function(){
if (mSelectedYear == i && mSelectedGenre == curGenre) {
mAreaSelected = d3.select(this);
mAreaSelected.attr('class', 'selected');
mAreaColorToRestore = curColor;
}
});
let border = 'M ' + prevX + ' ' + (yBase - accPrevY - (prevData[ curGenre ])) +
' L ' + curX + ' ' + (yBase - accCurY - (curData[ curGenre ]));
areaChartSvg.append('path').attr('id', 'genre_' + j).style('opacity', 0)
.attr('stroke', d3.rgb(curColor).darker(2)).attr('d', border);
accPrevY += prevData[ curGenre ];
accCurY += curData[ curGenre ];
}
prevSongs = curSongs;
prevData = curData;
}
// animation
for (let i = 0 ; i < GENRE_CATEGORY.length ; i++) {
d3.selectAll('#genre_' + i).transition().delay((i * 100)).duration(100)
.style('opacity', 1);
}
// show default selection and fixed tooltip
mAreaSelected.transition().delay(GENRE_CATEGORY.length * 100).duration(100)
.attr('fill', d3.rgb(mAreaColorToRestore).darker(2));
areaTooltipFixed.transition().delay(GENRE_CATEGORY.length * 100).duration(100)
.style("visibility", "visible");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function drawViolinPlot() {
//disappear with animation
d3.selectAll("rect")
.attr("fill-opacity", 1)
.attr("stroke-opacity", 1)
.transition()
.duration(300)
//change fill and stroke opacity to avoid CSS conflicts
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0).remove()
d3.selectAll(".whisker_line")
.attr("fill-opacity", 1)
.attr("stroke-opacity", 1)
.transition()
.duration(300)
//change fill and stroke opacity to avoid CSS conflicts
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0).remove()
d3.selectAll(".box_line")
.attr("fill-opacity", 1)
.attr("stroke-opacity", 1)
.transition()
.duration(300)
//change fill and stroke opacity to avoid CSS conflicts
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0).remove()
d3.selectAll("toto")
.attr("fill-opacity", 1)
.attr("stroke-opacity", 1)
.transition()
//change fill and stroke opacity to avoid CSS conflicts
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0).remove()
d3.selectAll(".box_axis")
.attr("fill-opacity", 1)
.attr("stroke-opacity", 1)
.transition()
.duration(300)
//change fill and stroke opacity to avoid CSS conflicts
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0).remove()
d3.selectAll(".stooltip")
.attr("fill-opacity", 1)
.attr("stroke-opacity", 1)
.transition()
.duration(300)
//change fill and stroke opacity to avoid CSS conflicts
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0).remove()
//svg.transition().duration(1200).style('opacity', 1);
// prepare values
let prevSongs = getSongsByYear(mSelectedYear);
//prepare attributes
let valence = []
let energy = []
let acousticness = []
let danceability = []
function getNormalized(array) {
let min = Math.min(...array),
max = Math.max(...array);
return array.map(v => (v - min) / (max - min));
}
let temp = []
for (let i = 0 ; i < prevSongs.length ; i++) {
if (prevSongs[i].genre == mSelectedGenre) {
console.log(mSelectedGenre)
valence.push(prevSongs[i].valence)
energy.push(prevSongs[i].energy)
acousticness.push(prevSongs[i].acousticness)
danceability.push(prevSongs[i].danceability)
temp.push(prevSongs[i].tempo)
}
}
let tempo = getNormalized(temp)
//console.log(valence)
//console.log(energy)
//console.log(acousticness)
//console.log(danceability)
//console.log((tempo))
//valence
let sorted_valence = valence.sort(d3.ascending)
let v_q1 = d3.quantile(sorted_valence, .25)
let v_median = d3.quantile(sorted_valence, .5)
let v_q3 = d3.quantile(sorted_valence, .75)
let v_interQuantileRange = v_q3 - v_q1
let v_min = v_q1 - (1.5 * v_interQuantileRange)
let v_max = v_q1 + (1.5 * v_interQuantileRange)
if (v_min <0){
v_min =0
}
if (v_max >1){
v_max =1
}
//energy
let sorted_energy = energy.sort(d3.ascending)
let e_q1 = d3.quantile(sorted_energy, .25)
let e_median = d3.quantile(sorted_energy, .5)
let e_q3 = d3.quantile(sorted_energy, .75)
let e_interQuantileRange = e_q3 - e_q1
let e_min = e_q1 - 1.5 * e_interQuantileRange
let e_max = e_q1 + 1.5 * e_interQuantileRange
if (e_min <0){
e_min =0
}
if (e_max >1){
e_max =1
}
//acousticness
let sorted_acousticness = acousticness.sort(d3.ascending)
let a_q1 = d3.quantile(sorted_acousticness, .25)
let a_median = d3.quantile(sorted_acousticness, .5)
let a_q3 = d3.quantile(sorted_acousticness, .75)
let a_interQuantileRange = a_q3 - a_q1
let a_min = a_q1 - 1.5 * a_interQuantileRange
let a_max = a_q1 + 1.5 * a_interQuantileRange
if (a_min <0){
a_min =0
}
if (a_max >1){
a_max =1
}
//danceability
let sorted_danceability = danceability.sort(d3.ascending)
let d_q1 = d3.quantile(sorted_danceability, .25)
let d_median = d3.quantile(sorted_danceability, .5)
let d_q3 = d3.quantile(sorted_danceability, .75)
let d_interQuantileRange = d_q3 - d_q1
let d_min = d_q1 - 1.5 * d_interQuantileRange
let d_max = d_q1 + 1.5 * d_interQuantileRange
if (d_min <0){
d_min =0
}
if (d_max >1){
d_max =1
}
//tempo
let sorted_tempo = tempo.sort(d3.ascending)
let t_q1 = d3.quantile(sorted_tempo, .25)
let t_median = d3.quantile(sorted_tempo, .5)
let t_q3 = d3.quantile(sorted_tempo, .75)
let t_interQuantileRange = t_q3 - t_q1
let t_min = t_q1 - 1.5 * t_interQuantileRange
let t_max = t_q1 + 1.5 * t_interQuantileRange
if (t_min <0){
t_min =0
}
if (t_max >1){
t_max =1
}
// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 500 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// append the svg object to the body of the page
let svg = d3.select("#violin")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr('class','box_axis')
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Show the X scale
let x = d3.scaleBand()
.range([ 0, width ])
.domain(["Valence", "Energy", "Acousticness","Danceability","Tempo"])
.paddingInner(1)
.paddingOuter(.5)
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
// Show the Y scale
let y = d3.scaleLinear()
.domain([0,1])
.range([height, 0])
svg.append("g").call(d3.axisLeft(y))
let histogram = d3.histogram()
.domain(y.domain())
.thresholds(y.ticks(10)) // Important: how many bins approx are going to be made? It is the 'resolution' of the violin plot
.value(d => d)
// gridlines in y axis function
/* function make_y_gridlines() {
return d3.axisLeft(y)
.ticks(10)
}
// add the Y gridlines
svg.append("g")
.attr("class", "grid")
.style("stroke", "none")
.call(make_y_gridlines()
.tickSize(-width)
.tickFormat("")
) */
let vyScale = d3.scaleLinear().domain([v_min,v_max]).range([y(v_min), y(v_max)])
let vyAxis = d3.axisRight().scale(vyScale).tickSize(200)
//////valence
let areaV = d3.area()
.x0(d => -d.length/3 +4.5)
.x1(d => d.length/3 +4.5)
.y(d => vyScale(d.x0))
.curve(d3.curveCatmullRom)
svg
.selectAll("myViolin")
.data([valence]).enter() // So now we are working group per group
.append("g")
.attr("transform", (d)=>"translate( 40)") // Translation on the right to be at the group position
.append("path")
.style("stroke", "none")
.style("fill","pink")
.style("opacity","1")
.attr("d", d => areaV(histogram(d))) // This makes the line smoother to give the violin appearance. Try d3.curveStep to see the difference
var v_center = 40
var v_width = 50
// Show the vertical line
svg
.append("line")
.attr('class','whisker_line')
.attr("x1", v_center+4.5)
.attr("x2", v_center+4.5)
.attr("y1", y(v_min) )
.attr("y2", y(v_max) )
.attr("stroke", "black")
.attr("stroke-width",1)
// Show the box
svg
.append("rect")
.attr('id', 'valence')
.attr("min", v_min )
.attr("q1", v_q1 )
.attr("median", v_median )
.attr("q3", v_q3)
.attr("IQR", v_interQuantileRange)
.attr("max", v_max)
.attr("x", v_center - v_width/4+4.5)
.attr("y", y(v_q3) )
.attr("height", (y(v_q1)-y(v_q3)) )
.attr("width", v_width/2 )
.attr("stroke", "black")
.style("fill", "#ff89da")
.style("opacity","0.6")
.attr("stroke-width",0.8)
// show median, min and max horizontal lines
svg
.selectAll("toto")
.data([v_min, v_median, v_max])
.enter()
.append("line")
.attr('class','box_line')
.attr("x1", v_center-v_width/4 +4.5)
.attr("x2", v_center+v_width/4 +4.5)
.attr("y1", function(d){ return(y(d))} )
.attr("y2", function(d){ return(y(d))} )
.attr("stroke", "black")
.attr("stroke-width",0.8)
//////energy
let eyScale = d3.scaleLinear().domain([e_min,e_max]).range([y(e_min), y(e_max)])
let eyAxis = d3.axisRight().scale(eyScale).tickSize(200)
let areaE = d3.area()
.x0(d => -d.length/3 +3)
.x1(d => d.length/3+3)
.y(d => eyScale(d.x0))
.curve(d3.curveCatmullRom)
svg
.selectAll("myViolin")
.data([energy]).enter() // So now we are working group per group
.append("g")
.attr("transform", (d)=>"translate( 130)") // Translation on the right to be at the group position
.append("path")
.style("stroke", "none")
.style("fill","lightgreen")
.style("opacity","0.8")
.attr("d", d => areaE(histogram(d))) // This makes the line smoother to give the violin appearance. Try d3.curveStep to see the difference
var e_center = 130
var e_width = 50
// Show the vertical line
svg
.append("line")
.attr('class','whisker_line')
.attr("x1", e_center +3)
.attr("x2", e_center+3)
.attr("y1", y(e_min) )
.attr("y2", y(e_max) )
.attr("stroke", "black")
.attr("stroke-width",1)
// Show the box
svg
.append("rect")
.attr('id', 'energy')
.attr("min", e_min )
.attr("q1", e_q1 )
.attr("median", e_median )
.attr("q3", e_q3)
.attr("IQR", e_interQuantileRange)
.attr("max", e_max)
.attr("x", e_center - e_width/4 +3)
.attr("y", y(e_q3) )
.attr("height", (y(e_q1)-y(e_q3)) )
.attr("width", e_width/2 )
.attr("stroke", "black")
.style("fill", "#00d073")
.style("opacity","0.6")
.attr("stroke-width",0.8)
// show median, min and max horizontal lines
svg
.selectAll("toto")
.data([e_min, e_median, e_max])
.enter()
.append("line")
.attr('class','box_line')
.attr("x1", e_center-e_width/4+3)
.attr("x2", e_center+e_width/4+3)
.attr("y1", function(d){ return(y(d))} )
.attr("y2", function(d){ return(y(d))} )
.attr("stroke", "black")
.attr("stroke-width",0.8)
//////acousticness
let ayScale = d3.scaleLinear().domain([a_min,a_max]).range([y(a_min), y(a_max)])
let ayAxis = d3.axisRight().scale(ayScale).tickSize(200)
let areaA = d3.area()
.x0(d => -d.length/3 +0.5)
.x1(d => d.length/3+0.5)
.y(d => ayScale(d.x0))
.curve(d3.curveCatmullRom);
svg
.selectAll("myViolin")
.data([acousticness]).enter() // So now we are working group per group
.append("g")
.attr("transform", (d)=>"translate( 220)") // Translation on the right to be at the group position
.append("path")
.style("stroke", "none")
.style("fill","yellow")
.style("opacity","0.4")
.attr("d", d => areaA(histogram(d))) // This makes the line smoother to give the violin appearance. Try d3.curveStep to see the difference
var a_center = 220
var a_width = 50
// Show the vertical line
svg
.append("line")
.attr('class','whisker_line')
.attr("x1", a_center+0.5)
.attr("x2", a_center+0.5)
.attr("y1", y(a_min) )
.attr("y2", y(a_max) )
.attr("stroke", "black")
.attr("stroke-width",1)
// Show the box
svg
.append("rect")
.attr('id', 'acousticness')
.attr("min", a_min )
.attr("q1", a_q1 )
.attr("median", a_median )
.attr("q3", a_q3)
.attr("IQR", a_interQuantileRange)
.attr("max", a_max)
.attr("x", a_center - a_width/4+0.5)
.attr("y", y(a_q3) )
.attr("height", (y(a_q1)-y(a_q3)) )
.attr("width", a_width/2 )
.attr("stroke", "black")
.style("fill", "#ffe587")
.style("opacity","0.6")
.attr("stroke-width",0.8)
// show median, min and max horizontal lines
svg
.selectAll("toto")
.data([a_min, a_median, a_max])
.enter()
.append("line")
.attr('class','box_line')
.attr("x1", a_center-a_width/4+0.5)
.attr("x2", a_center+a_width/4+0.5)
.attr("y1", function(d){ return(y(d))} )
.attr("y2", function(d){ return(y(d))} )
.attr("stroke", "black")
.attr("stroke-width",0.8)
//danceability
let dyScale = d3.scaleLinear().domain([d_min,d_max]).range([y(d_min), y(d_max)])
let dyAxis = d3.axisRight().scale(dyScale).tickSize(200)
let areaD = d3.area()
.x0(d => -d.length/3 -1)
.x1(d => d.length/3-1)
.y(d => dyScale(d.x0))
.curve(d3.curveCatmullRom);
svg
.selectAll("myViolin")
.data([danceability]).enter() // So now we are working group per group
.append("g")
.attr("transform", (d)=>"translate(310)") // Translation on the right to be at the group position
.append("path")
.style("stroke", "none")
.style("fill","coral")
.style("opacity","0.7")
.attr("d", d => areaD(histogram(d))) // This makes the line smoother to give the violin appearance. Try d3.curveStep to see the difference
var d_center = 310
var d_width = 50
// Show the vertical line
svg
.append("line")
.attr('class','whisker_line')
.attr("x1", d_center-1)
.attr("x2", d_center-1)
.attr("y1", y(d_min) )
.attr("y2", y(d_max) )
.attr("stroke", "black")
.attr("stroke-width",1)
// Show the box
svg
.append("rect")
.attr('id', 'danceability' )
.attr("min", d_min )
.attr("q1", d_q1 )
.attr("median", d_median )
.attr("q3", d_q3)
.attr("IQR", d_interQuantileRange)
.attr("max", d_max)
.attr("x", d_center - d_width/4-1)
.attr("y", y(d_q3) )
.attr("height", (y(d_q1)-y(d_q3)) )
.attr("width", d_width/2 )
.attr("stroke", "black")
.style("fill", "#ff7972")
.style("opacity","0.6")
.attr("stroke-width",0.8)
// show median, min and max horizontal lines
svg
.selectAll("toto")
.data([d_min, d_median, d_max])
.enter()
.append("line")
.attr('class','box_line')
.attr("x1", d_center-d_width/4-1)
.attr("x2", d_center+d_width/4-1)
.attr("y1", function(d){ return(y(d))} )
.attr("y2", function(d){ return(y(d))} )
.attr("stroke", "black")
.attr("stroke-width",0.8)
//tempo
let tyScale = d3.scaleLinear().domain([t_min,t_max]).range([y(t_min), y(t_max)])
let tyAxis = d3.axisRight().scale(tyScale).tickSize(200)
let areaT = d3.area()
.x0(d => -d.length/3-3.5)
.x1(d => d.length/3-3.5)
.y(d => tyScale(d.x0))
.curve(d3.curveCatmullRom);
svg
.selectAll("myViolin")
.data([tempo]).enter() // So now we are working group per group
.append("g")
.attr("transform", (d)=>"translate(400)") // Translation on the right to be at the group position
.append("path")
.style("stroke", "none")
.style("fill","blue")
.style("opacity","0.3")
.attr("d", d => areaT(histogram(d))) // This makes the line smoother to give the violin appearance. Try d3.curveStep to see the difference
var t_center = 400
var t_width = 50
// Show the vertical line
svg
.append("line")
.attr('class','whisker_line')
.attr("x1", t_center-3.5)
.attr("x2", t_center-3.5)