-
Notifications
You must be signed in to change notification settings - Fork 0
1568 lines (1361 loc) · 59.4 KB
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
import define1 from "./[email protected]";
import define2 from "./[email protected]";
function _1(md){return(
md`# Table 6 - D3 Visualizations`
)}
function _2(md){return(
md`### Google Doc link:
https://docs.google.com/document/d/1_ZIO4pQtSosJjzlg7lPf0nLL7SeKNSCyzvZtjsCvhts/edit
### Dataset
Download the .csv file from this Google Drive link
https://drive.google.com/file/d/1Wl5EkJOUrZGluVk_wjn1itU-sNQuS7LF/view?usp=share_link`
)}
function _3(md){return(
md`Note: To run the Observable notebook, download the .csv file from the Google Drive link and upload it to the notebook.`
)}
function _4(md){return(
md`### Data Preprocessing`
)}
function _5(htl){return(
htl.html`<h3 style= "white-space: nowrap">Click Choose File and select the Trending Youtube Videos Dataset from your local system </h3>`
)}
function _csvfile(Inputs){return(
Inputs.file({label: "Upload the .csv file", accept: ".csv", required: true})
)}
function _data(csvfile){return(
csvfile.csv({typed: true})
)}
function _8(md){return(
md`# Design 1`
)}
function _9(htl){return(
htl.html`<html>
<head>
</head>
<body height: 650px; border: 2px solid black; background-color: #F8F5F0; justify-content: center;>
<div class="col-md-4 align-self-center"; style="height: 100%; width:45%; />
<div class="col-md-5" style="padding-top: 100px; font-size: 27px; text-align: justify" >
<p style="font-size: 25px; font-weight: bolder; text-align: left;white-space: nowrap"> Net Popularity Index vs Sentiment Scores </p>
<ul style="list-style: square; margin-left: 20px; margin-top: 40px;">
<li style="white-space: nowrap">Below is the visualisation where we map the derived data "Net Popularity Index" against the calculated sentiment scores.<br> The purpose of this visualisation is to find the correlation between sentiment analysis results and the general viewing trend of people.</li>
<li style="white-space: nowrap">Hovering over the below graph gives the exact values of the particular score and their comparison.</li>
<li align="justify">Net Popularity Index is calculated as follows:<be>
<ol>
<li style="white-space: nowrap"> The net popularity of a specific video will be the subtraction of <i>"Number of Likes" </i> and <i>"Number of Dislikes" </i> </i></li>
<li style="white-space: nowrap">The net popularity index of a specific video will be the division of <i>"Net Popularity" </i> and <i>"Number of Views" </i> </i></li>
<li style="white-space: nowrap">Further more the values are normalised on a scale of 0 to 1 to make it comparable to the sentiment score</li>
</ol>
</li>
<li style="white-space: nowrap">Important inference like the following ones can be concluded from this visualization:
<ol>
<li style="white-space: nowrap">The general trend between sentiment score and Net Popularity index appears to be linear, as expected.
<li style="white-space: nowrap">More positive the sentiment, more the net popularity index of the category.
<li style="white-space: nowrap">The News and Politics section which appears to have the least sentiment score, appears to be the least favorite among the viewers, <br>this can be interpreted as Politics videos dividing the viewers as number of Likes and Dislikes are almost equal.
<li style="white-space: nowrap">Comedy appears to be the most popular and liked category of the lot. This conclusion is expected as well, because who doesn't like laughing?
<li style="white-space: nowrap">Science & Technology, and Travel videos which are generally positive in sentiment aren't as popular as expected.
<li style="white-space: nowrap">If the video uploaded did not trend within 10 days of publishing, the chances of trending are slim to none.
<li style="white-space: nowrap">The number of Education videos uploaded on weekends is very less compared to weekdays.
<li style="white-space: nowrap">People & Blog videos are evenly spread out during the week.</li>
</ol>
</li>
</ul>
</div>
</body>
</html>`
)}
function _chartClustered(d3,DOM,width,height,titleText,titleSize,dataNew,x0,groupKey,keys,x1,y,margin,color,tooltipSize,xAxis,yAxis,legend)
{
const svg = d3.select(DOM.svg(width, height));
svg.append('text')
.attr('class', 'title')
.text(titleText)
.attr('y', 15)
.attr('x', width/3)
.attr('font-size', titleSize);
var bars = svg.append("g")
.selectAll("g")
.data(dataNew.dat);
bars
.join("g")
.attr("transform", d => `translate(${x0(d[groupKey])},0)`)
.selectAll("rect")
.data(d => keys.map(key => ({key, value: d[key]})))
.join("rect")
.attr("x", d => x1(d.key))
.attr("y", d => d.value ? y(d.value) : margin.top)
.attr("width", x1.bandwidth())
.attr("height", d => d.value ? y(0) - y(d.value) : height - margin.bottom - margin.top)
.attr("fill", d => d.value ? color(d.key) : 'transparent')
.on("mouseenter", function(d) {
svg.append("text").attr("class", "tool")
.attr("x", x0(d.path[1].__data__[groupKey]) + x1(d.path[0].__data__.key))
// .attr("y", y(d.path[0].__data__.value)-25)
.attr("y", margin.top - 50)
.style("display", null)
// .style("fill", color(d.path[0].__data__.key))
.style("fill", 'black')
.text("Catergory: " + d.path[0].__data__.key.charAt(0).toUpperCase() + d.path[0].__data__.key.slice(1))
.attr('font-size', tooltipSize)
svg.append("text").attr("class", "tool1")
.attr("x", x0(d.path[1].__data__[groupKey]) + x1(d.path[0].__data__.key))
// .attr("y", y(d.path[0].__data__.value)-10)
.attr("y", margin.top - 35)
.style("display", null)
// .style("fill", color(d.path[0].__data__.key))
.style("fill", 'black')
.text("Score: " + Number(d.path[0].__data__.value).toFixed(2))
.attr('font-size', tooltipSize)
})
.on("mouseout", function(d) {
svg.selectAll("text.tool").style("display", "none")
svg.selectAll("text.tool1").style("display", "none")
});
svg.append("g")
.attr("class", "x-axis")
.call(xAxis);
svg.append("g")
.call(yAxis);
svg.append("g")
.call(legend);
svg.node().scrollBy(5, 0);
return svg.node();
}
function _11(md){return(
md`# Design 2`
)}
function _12(md){return(
md`## PIE CHART 1: Categories`
)}
function _13(htl){return(
htl.html`<html>
<head>
</head>
<body height: 650px; border: 2px solid black; background-color: #F8F5F0; justify-content: center;>
<div class="col-md-4 align-self-center"; style="height: 100%; width:45%; />
<div class="col-md-5" style="padding-top: 100px; font-size: 27px; text-align: justify" >
<p style="font-size: 25px; font-weight: bolder; text-align: left;white-space: nowrap"> Category wise feature visualization </p>
<ul style="list-style: square; margin-left: 20px; margin-top: 40px;">
<li style="white-space: nowrap">In the below dashboard, the pie chart denotes the video distribution by different categories.</li>
<li style="white-space: nowrap">If you hover on the pie chart, you can see the category and number of videos below change in real time.</li>
<li style="white-space: nowrap">Clicking on a category shows the different properties specific to that category in the following three charts.
<br>1. The number of Likes vs Dislikes over the period of time.
<br>2. Number of videos released on each day of the week.
<br>3. No of days taken for the videos to trend. </li>
<li style="white-space: nowrap">Important inference like the following ones can be concluded from this visualization:
<br>1. Most of the music videos are published on Fridays and most of them trend in 2-3 days utilizing the weekend.
<br>2. Most of the News & Politics videos are published on Mondays indicating the start of a working week.
<br>3. Novermber of 2020 indicates the most number of disliked for political videos in general, indicating the possible divide <br> because of the Presidential Elections.
<br>4. November and December has seen the most number of video uploads from Comedy, indicating the overall sentiment <br> taking into the account the holiday season.
<br>5. If the video uploaded did not trend in 10 days of publishing, the chances of trending are slim to none.
<br>6. The number of Education videos uploaded on weekend is very less compared to weekdays.
<br>6. People & Blog videos are evenly spread out during the week.</li>
</ul>
</div>
</body>
</html>
`
)}
function _14(textPieChart1,textPieChart2){return(
textPieChart1 + textPieChart2
)}
function _pie1(d3_v5,width,radius,pieData,arc,colorSeq,onMouseOver,onMouseOut,$0,labelHeight)
{
const svg = d3_v5
.create('svg')
.attr('width', width)
.attr('height', 400);
const chart = svg
.append('g')
.attr('transform', `translate(${radius},${radius})`);
const text = svg
.append('text')
.attr("id", 'toptext')
.attr("x", width - 450)
.attr("y", 550)
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("font-family", "sans-serif")
//.text(textPieChart1 + textPieChart2);
chart
.selectAll(null)
.data(pieData)
.enter()
.append('path')
.attr('d', arc)
.attr('fill', d => colorSeq(d.index))
.attr('stroke', 'grey')
.style('stroke-width', '1px')
.on('mouseover', onMouseOver)
.on('mouseout', onMouseOut)
.on('click', (event , r) => {
$0.value = event.data.key;
//mutable desiredCategory = hoverCategory;
});
const legend = svg
.append('g')
.attr('transform', `translate(${radius * 2 + 20},0)`);
legend
.selectAll(null)
.data(pieData)
.enter()
.append('rect')
.attr('y', d => labelHeight * d.index * 1)
.attr('width', labelHeight)
.attr('height', labelHeight)
.attr('fill', d => colorSeq(d.index))
.attr('stroke', 'grey')
.style('stroke-width', '1px');
legend
.selectAll(null)
.data(pieData)
.enter()
.append('text')
.text(d => d.data.key)
.attr('x', labelHeight * 1.1)
.attr('y', d => labelHeight * d.index * 1 + labelHeight)
.style('font-family', 'sans-serif')
.style('font-size', `${10}px`);
// .style('font-size', `${labelHeight}px`);
return svg.node();
}
function _16(md){return(
md`## BAR CHART 1: Likes vs Dislikes for different categories`
)}
function _chart1(d3_v5,DOM,width,height,titleText_D2,titleSize,dataDesign2,x0_D2,groupKey_D2,keys_D2,x1_D2,y_D2,margin,color_D2,xAxis_D2,yAxis_D2,legend_D2)
{
const svg = d3_v5.select(DOM.svg(width, height ));
svg.append('text')
.attr('class', 'title')
.text(titleText_D2)
.attr('y', 15)
.attr('x', width/3)
.attr('font-size', titleSize);
var bars = svg.append("g")
.selectAll("g")
.data(dataDesign2.dat);
bars
.join("g")
.attr("transform", d => `translate(${x0_D2(d[groupKey_D2])},0)`)
.selectAll("rect")
.data(d => keys_D2.map(key => ({key, value: d[key]})))
.join("rect")
.attr("x", d => x1_D2(d.key))
.attr("y", d => d.value ? y_D2(d.value) : margin.top)
.attr("width", x1_D2.bandwidth())
.attr("height", d => d.value ? y_D2(0) - y_D2(d.value) : height - margin.bottom - margin.top)
.attr("fill", d => d.value ? color_D2(d.key) : 'transparent')
// .on("mouseenter", function(d) {
// svg.append("text").attr("class", "tool")
// .attr("x", x0(d.path[1].__data__[groupKey]) + x1(d.path[0].__data__.key))
// // .attr("y", y(d.path[0].__data__.value)-25)
// .attr("y", margin.top - 50)
// .style("display", null)
// // .style("fill", color(d.path[0].__data__.key))
// .style("fill", 'black')
// .text("Catergory: " + d.path[0].__data__.key.charAt(0).toUpperCase() + d.path[0].__data__.key.slice(1))
// .attr('font-size', tooltipSize)
// svg.append("text").attr("class", "tool1")
// .attr("x", x0(d.path[1].__data__[groupKey]) + x1(d.path[0].__data__.key))
// // .attr("y", y(d.path[0].__data__.value)-10)
// .attr("y", margin.top - 35)
// .style("display", null)
// // .style("fill", color(d.path[0].__data__.key))
// .style("fill", 'black')
// .text("Score: " + Number(d.path[0].__data__.value).toFixed(2))
// .attr('font-size', tooltipSize)
// })
// .on("mouseout", function(d) {
// svg.selectAll("text.tool").style("display", "none")
// svg.selectAll("text.tool1").style("display", "none")
// });
svg.append("g")
.attr("class", "x-axis")
.call(xAxis_D2);
svg.append("g")
.call(yAxis_D2);
svg.append("g")
.call(legend_D2);
svg.node().scrollBy(5, 0);
return svg.node();
}
function _18(md){return(
md`#### *Note that YouTube disabled the dislike count from December 2021`
)}
function _19(md){return(
md`## BAR CHART 2: No of Videos vs Publishing date`
)}
function _chart3(BarChart,dataBarChart2,desiredCategory){return(
BarChart(dataBarChart2, {
x: d => d.day,
y: d => d.no_of_videos,
xDomain: dataBarChart2.day, // sort by descending frequency
yLabel: "↑ No of Videos vs Publishing Data for category: " + desiredCategory,
width: 500,
height: 500,
color: "#3288bd"
})
)}
function _21(md){return(
md`## BAR CHART 3: No of Days to Trend Specific to Category`
)}
function _chart4(BarChart,dataBarChart3,dataBarChart2,desiredCategory){return(
BarChart(dataBarChart3, {
x: d => d.date_range_to_trend,
y: d => d.no_of_videos,
xDomain: dataBarChart2.date_range_to_trend, // sort by descending frequency
yLabel: "↑ No of Days to Trend for category: " + desiredCategory,
width: 500,
height: 500,
color: "rgb(108,99,255)"
})
)}
function _23(md){return(
md`# Design 3`
)}
function _24(htl){return(
htl.html`<html>
<head>
</head>
<body height: 650px; border: 2px solid black; background-color: #F8F5F0; justify-content: center;>
<div class="col-md-4 align-self-center"; style="height: 100%; width:45%; />
<div class="col-md-5" style="padding-top: 100px; font-size: 27px; text-align: justify" >
<p style="font-size: 25px; font-weight: bolder; text-align: left;white-space: nowrap">Sentiment Category Distribution for top-n categories based on popularity </p>
<ul style="list-style: square; margin-left: 20px; margin-top: 40px;">
<li style="white-space: nowrap">Clicking on a category shows the sentiment category distribution for it, based on popularity.</li>
<li align="justify">Popularity is calculated as follows:<br>
Popularity is set to <b>"TRUE" </b> when the <i>number of views, count of likes</i> + <i> dislikes</i>, and <i> number of comments</i> of a category is greater than the <b>median</b> <i>number of views, count of likes</i> + <i> dislikes</i>, and <i>number of comments</i></li>
<li style="white-space: nowrap"> First level is the category level, second level is sentiment category level, third level is the popularity level (True/False).
<li>Click the center circle to revert to the previous visualization.</li>
<li style="white-space: nowrap">We can answer the correlation between video popularity with the sentiment score with this visualization.</li>
<li style="white-space: nowrap"> Also, this visualization gives us an overview of the distribution of sentiment categories for the top-n categories in the dataset.</li>
</ul>
<p class="mt-5">Hover your mouse over the pie chart to see more information.</p>
</div>
</body>
</html>
`
)}
function _25(html,filter){return(
html`You have chosen to view top <b>${filter}</b> categories. Please move the slider to change the number of categories. Ideally, choosing 5 is recommended`
)}
function _filter(html){return(
html`<input type="range" min=5 max=15 value=0 step=1>`
)}
function _custom_chart(d3,width,novelData,data)
{
let svg = d3
.create("svg")
.attr("width", width)
.attr("height", 1000)
.style("border", "1px solid black");
const g = svg
.append("g")
.attr("transform", `translate(${width/2},${1000/2})`);
function partition(data_array) {
const root = d3.hierarchy(data_array)
.sum(d => d.value)
.sort((a, b) => b.value - a.value);
return d3.partition()
.size([2 * Math.PI, root.height + 1])
(root);
}
const tooltip = d3.select("body")
.append('div')
.attr('class', "tooltip")
.attr('id', 'NovelTooltip')
.style('position', 'absolute');
const radius = width/8
var categorycolor = d3.scaleOrdinal(d3.schemeCategory10);
const color = { Positive:'#93C572', Negative:'#E97451', Neutral:'lightblue', TRUE:'orange', FALSE:'steelblue'}
var arc = d3.arc()
.startAngle(d => d.x0)
.endAngle(d => d.x1)
.padAngle(d => Math.min((d.x1 - d.x0) / 2, 0.005))
.padRadius(radius * 1.5)
.innerRadius(d => d.y0 * radius)
.outerRadius(d => Math.max(d.y0 * radius, d.y1 * radius - 1))
var root = partition(novelData(data));
root.each(d => d.current = d);
var path = g.append("g")
.selectAll("path")
.data(root.descendants().slice(1))
.join("path")
.attr("fill", (d,i) => {
if(d.depth === 1)
return 'white'
else {
return color[d.data.name];
}
})
.attr('stroke-width', '1px')
.attr('stroke', function (d){
if (d.depth === 1)
return 'black';
})
.attr("fill-opacity", d => arcVisible(d.current) ? (d.children ? 0.8 : 0.8) : 0)
.attr("pointer-events", d => arcVisible(d.current) ? "auto" : "none")
.attr("d", d => arc(d.current))
.on('mouseover', function(event, d){
d3.select(this)
.attr('stroke-width', '1px')
.attr('stroke', 'black')
let value = 0;
if(d.depth !== 3)
value = d.data.length;
else
value = d.data.value
tooltip
.style("opacity", 2)
.style("left", event.pageX + 20 + "px")
.style("top", event.pageY + "px")
.html(`
<div style="border: thin solid grey; border-radius: 5px; background: lightgrey; padding: 10px; width: 100px">
<h5>${d.data.name}<h3>
<h6> <span style="color:darkorange"><b>Value: </b>${value}</h6></span>
</div>`);
})
.on('mouseout', function(event, d){
d3.select(this)
.attr('stroke-width', '1px')
.attr('stroke', function (d){
if (d.depth === 1)
return 'black';
})
tooltip
.style("opacity", 0)
.style("left", 0)
.style("top", 0)
.html(``);
});
path.filter(d => d.children)
.style("cursor", "pointer")
.on("click", clicked);
var label = g.append("g")
.attr("pointer-events", "none")
.attr("text-anchor", "middle")
.style("user-select", "none")
.selectAll("text")
.data(root.descendants().slice(1))
.join("text")
.attr("dy", "0.35em")
.attr("fill-opacity", d => + labelVisible(d.current))
.attr("transform", d => labelTransform(d.current))
.text(function (d) {
if(d.data.name === 'TRUE')
return 'TRUE';
else if (d.data.name === 'FALSE')
return 'FALSE';
else
return d.data.name;
});
var parent = g.append("circle")
.datum(root)
.attr("r", radius)
.attr("fill", "#dbd5c9")
.attr("pointer-events", "all")
.on("click", clicked);
function clicked(event, p) {
parent.datum(p.parent || root);
root.each(d => d.target = {
x0: Math.max(0, Math.min(1, (d.x0 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
x1: Math.max(0, Math.min(1, (d.x1 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
y0: Math.max(0, d.y0 - p.depth),
y1: Math.max(0, d.y1 - p.depth)
});
const t = g.transition().duration(750);
path.transition(t)
.tween("data", d => {
const i = d3.interpolate(d.current, d.target);
return t => d.current = i(t);
})
.filter(function(d) {
return + this.getAttribute("fill-opacity") || arcVisible(d.target);
})
.attr("fill-opacity", d => arcVisible(d.target) ? (d.children ? 0.9 : 0.8) : 0)
.attr("pointer-events", d => arcVisible(d.target) ? "auto" : "none")
.attrTween("d", d => () => arc(d.current));
label.filter(function(d) {
return + this.getAttribute("fill-opacity") || labelVisible(d.target);
}).transition(t)
.attr("fill-opacity", d => +labelVisible(d.target))
.attrTween("transform", d => () => labelTransform(d.current));
}
function arcVisible(d) {
return d.y1 <= 3 && d.y0 >= 1 && d.x1 > d.x0;
}
function labelVisible(d) {
return d.y1 <= 3 && d.y0 >= 1 && (d.y1 - d.y0) * (d.x1 - d.x0) > 0.03;
}
function labelTransform(d) {
const x = (d.x0 + d.x1) / 2 * 180 / Math.PI;
const y = (d.y0 + d.y1) / 2 * radius;
return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
}
return svg.node();
}
function _28(md){return(
md`# Design 4`
)}
function _29(htl){return(
htl.html`<html>
<head>
</head>
<body height: 750px; border: 2px solid black; background-color: #F8F5F0; justify-content: center;>
<div class="col-md-4 align-self-center"; style="height: 100%; width:45%; />
<div class="col-md-5" style="padding-top: 100px; font-size: 27px; text-align: justify" >
<p style="font-size: 25px; font-weight: bolder; text-align: left;white-space: nowrap">Average Sentiment Scores Trend (per day)</p>
<ul style="list-style: square; margin-left: 20px; margin-top: 40px;">
<li style="white-space: nowrap">The chart displays the sentiment scores across all categories for each day mapped to a scale that might help understand <br>the variations througout the period for which the data has been collected.</li>
<li style="white-space: nowrap">The visualization attempts to answer our domain question - "Is negative sentiment increasing over time, or are there <br> noticeable patterns on how a video trends with respect to the sentiment for a given year?" </li>
<!-- <li style="white-space: nowrap">For example, we see that negative sentiment is more prevalent in the months of January, and February, in comparison, and December </br> seems to be leaning more towards positive sentiment for 2020 and 2021.</li> -->
<li style="white-space: nowrap">Except for few months such as January and February 2022, April, June, and July 2021, the general sentiment of the trending <br> videos are generally positive.</li>
<li style="white-space: nowrap">Unusual spike in negative sentiment in the aforementioned months can be explained by Capitol Hill incident (January 2022), <br>COVID pandemic peak (Delta phase of COVID from April 2021 - June 2021).</li>
<li style="white-space: nowrap">The original design was to link the calendar chart to a multi-series line chart showing the sentiment score trend for each category. </br>A brushable interaction was planned to achieve this visualization (yet to be completed).</li>
</ul>
<p class="mt-5">Hover your mouse over the calendar chart to see more information: date along with the corresponding average sentiment score</p>
</div>
</body>
</html>`
)}
function _key(Legend,chart){return(
Legend(chart.scales.color, {title: "Sentiment Scores", marginLeft: 40})
)}
function _chart(Calendar,data,width){return(
Calendar(data, {
x: d => d.trending_date,
y: d => d.Sentiment_Scores,
width
})
)}
function _32(md){return(
md`#### Design 1 - Code`
)}
function _data_title(data){return(
data.filter(i=>i.view_count != 0).map(i => {const viewdata = {
categories: i.category,
net_popularity_per_view: (i.likes - i.dislikes)/i.view_count,
sentiment_scores:i["Sentiment_Scores"]
}
return viewdata})
)}
function _columnsToSum(data_title){return(
Object.keys(data_title[0]).slice(1)
)}
function _columnsToSum2(data_view){return(
Object.keys(data_view[0]).slice(5)
)}
function _view_sentiment_array(d3,data_title,columnsToSum){return(
Array.from((d3.rollup(data_title,
v => Object.fromEntries(columnsToSum.map(col => [col, d3.mean(v, d => d[col])])),
d => d.categories)), ([categories, counts]) => {
const result={}
result.category = categories;
result.net_popularity_per_view = (counts.net_popularity_per_view - 0.012014798319959788) / 0.06709932458
result.sentiment_scores = counts.sentiment_scores
return result;
}).sort((a, b) => d3.ascending(a.category, b.category))
)}
function _data_view(data){return(
data.map(i => {const viewdata = {
video:i.video_id,
category:i.category,
published_week:String(i.publishedAt).split(" ")[0],
published_month:String(i.publishedAt).split(" ")[1]+"-"+String(i.publishedAt).split(" ")[3],
views:i.view_count,
likes:i.likes,
dislikes:i.dislikes
}
return viewdata})
)}
function _month_year_array(d3,data_view,columnsToSum2){return(
Array.from((d3.rollup(data_view,
v => Object.fromEntries(columnsToSum2.map(col => [col, d3.sum(v, d => d[col])])),
d => d.category,d => d.published_month)), ([categories, counts]) => {
const result={}
result.category = categories;
result.week= Array.from(counts, ([month_year, metrics]) => ({ month_year, likes:metrics.likes, dislikes:metrics.dislikes}));
// result.week = Array.from(counts.values());
// result.week.day= Array.from(counts.keys());
return result;
}).sort((a, b) => d3.ascending(a.category, b.category))
)}
function _week_array(d3,data_view){return(
Array.from((d3.rollup(data_view,
v => v.length,
d => d.category,d => d.published_week)), ([categories, counts]) => {
const result={}
result.category = categories;
result.week= Array.from(counts, ([day, no_of_videos]) => ({ day, no_of_videos})).sort((a, b) => d3.ascending(a.day, b.day));
// result.week = Array.from(counts.values());
// result.week.day= Array.from(counts.keys());
return result;
}).sort((a, b) => d3.ascending(a.category, b.category))
)}
function _days_to_trend(data){return(
(data.filter(i=>(i.days_to_trend>=0 && i.days_to_trend<=1)).map(i=> ({date_range:"0-1",video_id:i.video_id,category:i.category}))).concat(data.filter(i=>(i.days_to_trend>=2 && i.days_to_trend<=3)).map(i=> ({date_range:"2-3",video_id:i.video_id,category:i.category}))).concat(data.filter(i=>(i.days_to_trend>=4 && i.days_to_trend<=5)).map(i=> ({date_range:"4-5",video_id:i.video_id,category:i.category}))).concat(data.filter(i=>(i.days_to_trend>=6 && i.days_to_trend<=7)).map(i=> ({date_range:"6-7",video_id:i.video_id,category:i.category}))).concat(data.filter(i=>(i.days_to_trend>=8 && i.days_to_trend<=9)).map(i=> ({date_range:"8-9",video_id:i.video_id,category:i.category}))).concat(data.filter(i=>(i.days_to_trend>=10 && i.days_to_trend<=11)).map(i=> ({date_range:"10-11",video_id:i.video_id,category:i.category}))).concat(data.filter(i=>(i.days_to_trend>=12)).map(i=> ({date_range:">=12",video_id:i.video_id,category:i.category})))
)}
function _days_to_trend_array(d3,days_to_trend){return(
Array.from((d3.rollup(days_to_trend,
v => v.length,
d => d.category,d => d.date_range)), ([categories, counts]) => {
const result={}
result.category = categories;
result.week= Array.from(counts, ([date_range_to_trend, no_of_videos]) => ({ date_range_to_trend, no_of_videos}));
// result.week = Array.from(counts.values());
// result.week.day= Array.from(counts.keys());
return result;
}).sort((a, b) => d3.ascending(a.category, b.category))
)}
function _BarChart(d3){return(
function BarChart(data, {
x = (d, i) => i, // given d in data, returns the (ordinal) x-value
y = d => d, // given d in data, returns the (quantitative) y-value
title, // given d in data, returns the title text
marginTop = 20, // the top margin, in pixels
marginRight = 0, // the right margin, in pixels
marginBottom = 30, // the bottom margin, in pixels
marginLeft = 40, // the left margin, in pixels
width = 640, // the outer width of the chart, in pixels
height = 400, // the outer height of the chart, in pixels
xDomain, // an array of (ordinal) x-values
xRange = [marginLeft, width - marginRight], // [left, right]
yType = d3.scaleLinear, // y-scale type
yDomain, // [ymin, ymax]
yRange = [height - marginBottom, marginTop], // [bottom, top]
xPadding = 0.1, // amount of x-range to reserve to separate bars
yFormat, // a format specifier string for the y-axis
yLabel, // a label for the y-axis
color = "currentColor" // bar fill color
} = {}) {
// Compute values.
const X = d3.map(data, x);
const Y = d3.map(data, y);
// Compute default domains, and unique the x-domain.
if (xDomain === undefined) xDomain = X;
if (yDomain === undefined) yDomain = [0, d3.max(Y)];
xDomain = new d3.InternSet(xDomain);
// Omit any data not present in the x-domain.
const I = d3.range(X.length).filter(i => xDomain.has(X[i]));
// Construct scales, axes, and formats.
const xScale = d3.scaleBand(xDomain, xRange).padding(xPadding);
const yScale = yType(yDomain, yRange);
const xAxis = d3.axisBottom(xScale).tickSizeOuter(0);
const yAxis = d3.axisLeft(yScale).ticks(height / 40, yFormat);
// Compute titles.
if (title === undefined) {
const formatValue = yScale.tickFormat(100, yFormat);
title = i => `${X[i]}\n${formatValue(Y[i])}`;
} else {
const O = d3.map(data, d => d);
const T = title;
title = i => T(O[i], i, data);
}
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto; height: intrinsic;");
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(yAxis)
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("x2", width - marginLeft - marginRight)
.attr("stroke-opacity", 0.1))
.call(g => g.append("text")
.attr("x", -marginLeft)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(yLabel));
const bar = svg.append("g")
.attr("fill", color)
.selectAll("rect")
.data(I)
.join("rect")
.attr("x", i => xScale(X[i]))
.attr("y", i => yScale(Y[i]))
.attr("height", i => yScale(0) - yScale(Y[i]))
.attr("width", xScale.bandwidth());
if (title) bar.append("title")
.text(title);
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(xAxis);
return svg.node();
}
)}
function _zoom(margin,width,height,d3,x0,data,groupKey,keys,x1,xAxis){return(
function zoom(svg) {
const extent = [[margin.left, margin.top], [width - margin.right, height - margin.top]];
svg.call(d3.zoom()
.scaleExtent([1, 8])
.translateExtent(extent)
.extent(extent)
.on("zoom", zoomed));
function zoomed(event) {
x0.range([margin.left, width - margin.right].map(d => event.transform.applyX(d)));
svg.selectAll("rect").data(data.dat)
.attr("x", d => x0(d[groupKey])).attr("width", x0.bandwidth())
// .attr("transform", d => `translate(${x0(d[groupKey])},0)`)
.data(d => keys.map(key => ({key, value: d[key]}))).selectAll("rect")
.attr("x", d => x1(d.key)).attr("width", x1.bandwidth());
svg.selectAll(".x-axis").call(xAxis);
}
}
)}
function _wrap(d3){return(
(text) => {
const width=50
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (words.length >0) {
word=words.pop()
line.push(word);
tspan.text(line.join(" "));
// if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y-10).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
// }
}
});
}
)}
function _legend(width,legendDistanceFromRight,margin,legendTextSize,color){return(
svg => {
const g = svg
.attr("transform", `translate(${width-legendDistanceFromRight},${margin.top})`)
.attr("text-anchor", "end")
.attr("font-family", "sans-serif")
.attr("font-size", legendTextSize)
.selectAll("g")
.data(color.domain().slice().reverse())
.join("g")
.attr("transform", (d, i) => `translate(0,${i * 20})`);
g.append("rect")
.attr("x", -19)
.attr("width", 19)
.attr("height", 19)
.attr("fill", color);
g.append("text")
.attr("x", -24)
.attr("y", 9.5)
.attr("dy", "0.35em")
.text(d => d);
}
)}
function _x0(d3,dataNew,groupKey,margin,width){return(
d3.scaleBand()
.domain(dataNew.dat.map(d => d[groupKey]))
.rangeRound([margin.left, width - margin.right])
.paddingInner(0.15)
)}
function _x1(d3,keys,x0){return(
d3.scaleBand()
.domain(keys)
.rangeRound([0, x0.bandwidth()])
.padding(0.05)
)}
function _y(d3,dataNew,keys,height,margin){return(
d3.scaleLinear()
.domain([0, d3.max(dataNew.dat, d => d3.max(keys, key => d[key]))]).nice()
.rangeRound([height - margin.bottom, margin.top])
)}
function _legendDistanceFromRight(){return(
20
)}
function _color(d3_v5,colorType,keys){return(
d3_v5.scaleOrdinal(d3_v5.quantize(colorType, keys.length))
)}
function _colorType(d3){return(
d3.interpolate("rgb(10, 100, 220)", "red")
)}
function _xAxis(height,margin,d3,x0,xAxisFontSize,wrap){return(
g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x0).tickSizeOuter(0))
.attr("font-size", xAxisFontSize)
.selectAll("text").call(wrap, x0.bandwidth())
.call(g => g.select(".domain").remove())
)}
function _yAxis(margin,d3,y,dataNew){return(
g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", -10)
.attr("y", -20)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(dataNew.y))
)}
function _groupKey(dataNew){return(
dataNew.columns[0]
)}
function _margin(){return(
{top: 90, right: 120, bottom: 50, left: 40}
)}
function _xAxisFontSize(){return(
8
)}
function _titleSize(){return(
20
)}
function _legendTextSize(){return(
12
)}
function _tooltipSize(){return(
12
)}
function _titleText(){return(
"Net Popularity Index vs Sentiment Score"
)}
function _yAxisText(){return(
"Score"
)}
function _height(){return(
450
)}
function _keys(view_sentiment_array){return(
Object.keys(view_sentiment_array[0]).slice(1)
)}
function _d3(require){return(
require("d3@6")
)}
function _dataNew(view_sentiment_array,yAxisText){return(
{
"dat": view_sentiment_array,
"columns": ["category", "net_popularity_per_view", "sentiment_scores"],
"y": yAxisText
}
)}
function _69(md){return(
md`#### Design 2 - Code`
)}
function _color_D2(d3_v5,colorType_D2,keys_D2){return(
d3_v5.scaleOrdinal(d3_v5.quantize(colorType_D2, keys_D2.length))
)}
function _colorType_D2(d3){return(
d3.interpolate("rgb(148, 220, 121)", "red")
)}
function _x0_D2(d3,dataDesign2,groupKey_D2,margin,width){return(
d3.scaleBand()
.domain(dataDesign2.dat.map(d => d[groupKey_D2]))
.rangeRound([margin.left, width - margin.right])
.paddingInner(0.15)
)}
function _x1_D2(d3,keys_D2,x0_D2){return(
d3.scaleBand()
.domain(keys_D2)
.rangeRound([0, x0_D2.bandwidth()])
.padding(0.05)