-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
517 lines (459 loc) · 17.1 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>How Does a YouTube Video Go Viral - Demo by ZenRows</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
<link
rel="stylesheet"
href="https://www.unpkg.com/[email protected]/dist/tailwind.min.css"
integrity="sha512-2x3sObNVbtxF++/xI9Rj0kqptCr1uNghrcoSzzVnWIkN4OBpP3NqBLR3+ezvzAVOtGKaWiy//TkdDF+SWseCSA=="
crossorigin=""
/>
<link
rel="stylesheet"
href="https://unpkg.com/[email protected]/themes/prism.css"
integrity="sha512-jtWR3pdYjGwfw9df601YF6uGrKdhXV37c+/6VNzNctmrXoO0nkgHcS03BFxfkWycOa2P2Nw9Y9PCT9vjG9jkVg=="
crossorigin=""
/>
<script
src="https://unpkg.com/[email protected]/prism.js"
integrity="sha512-S2+nGI85suJO7CXXZDiu8ygn+SOGUddvdqa2qu04j2D11jSOjWw8sA9n7EDX95J6aZnQImsqDuO/ItqCrwWDCg=="
crossorigin=""
></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"
integrity="sha256-cJnCTPRTD3OUjTD4Ml0WEMsmTiLl71arKaZ9DEZJk0o="
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"
integrity="sha256-qoN08nWXsFH+S9CtIq99e5yzYHioRHtNB9t2qy1MSmc="
crossorigin=""
></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chartjs-adapter-luxon.min.js"
integrity="sha256-q8w2Mgq36OwAFKLLbdSi+aCHAb6WJhIswZ7N6k+xsf0="
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"
integrity="sha256-qXBd/EfAdjOA2FGrGAG+b3YBn2tn5A6bhz+LSgYD96k="
crossorigin=""
></script>
</head>
<body>
<main class="my-10 lg:my-14 xl:my-16 max-w-6xl mx-auto px-4 sm:px-6">
<h1 class="text-3xl tracking-tight leading-tight font-extrabold text-gray-900 sm:text-4xl lg:text-5xl mb-8">How Does a YouTube Video Go Viral</h1>
<p class="font-medium my-6 text-xl text-gray-700">
This is an example dataset of Youtube videos extracted from a ZenRows task,
<a class="hover:text-blue-700 text-blue-600" href="https://app.zenrows.com/register">try it for free</a>.
</p>
<p class="font-medium my-6 text-base text-gray-600">
This data was extracted and plugged here with <strong>minimum effort</strong>. The original dataset contains more fields that were
not needed here and many more entries, so we removed them. But there is no need, you can use the with the original file and the
remaining fields will be ignored. Just be careful with the size since it can freeze your browser.
</p>
<h2 class="text-xl tracking-tight leading-tight font-extrabold text-gray-900 sm:text-2xl lg:text-3xl my-8 sm:my:12">
Views count per hour
</h2>
<canvas id="yt-chart1" class="my-8 max-h-screen w-full" width="800" height="800"></canvas>
<h2 class="text-xl tracking-tight leading-tight font-extrabold text-gray-900 sm:text-2xl lg:text-3xl my-8 sm:my:12">
Latest videos by PewDiePie
</h2>
<canvas id="yt-chart2" class="my-8 max-h-screen w-full" width="800" height="800"></canvas>
<h2 class="text-xl tracking-tight leading-tight font-extrabold text-gray-900 sm:text-2xl lg:text-3xl my-8 sm:my:12">
Channel views per day
</h2>
<canvas id="yt-chart3" class="my-8 max-h-screen w-full" width="800" height="800"></canvas>
<h2 class="text-xl tracking-tight leading-tight font-extrabold text-gray-900 sm:text-2xl lg:text-3xl my-8 sm:my:12">
Daily views per video by PewDiePie
</h2>
<canvas id="yt-chart4" class="my-8 max-h-screen w-full" width="800" height="800"></canvas>
<h2 class="text-xl tracking-tight leading-tight font-extrabold text-gray-900 sm:text-2xl lg:text-3xl my-8 sm:my:12">
How does it work
</h2>
<p class="font-medium mt-6 text-base text-gray-600">
There are two main parts: <strong>the dataset and the chart</strong>.<br />
The data comes from a separate file: <code>data.jsonp</code>. You can get a different dataset and modifying that file, the
<strong>chart should adjust automatically</strong>. To get new data you will need at least all the values present in the original
but the channel Id.
</p>
<pre class="language-javascript"><code class="language-javascript">// data is defined in the data.jsonp file
dataConfig.dataset =
// drop your JSON below this line
[
{
channelName: "✿ Kids Diana Show",
title: "Diana and Roma Cheer Up Baby Oliver with Music",
videoId: "SqiyEsUBe4o",
viewCount: 172723,
date: 162289398009
},
...
];</code></pre>
<pre class="language-javascript"><code class="language-javascript">// variables to handle all the info in the dataset
var dataConfig = {
maxViewCountFirstView: 500000, // avoid old videos
minViewCount: 1000000, // not to show an overwhelming amount of entries
minVideos: 8, // filter out channels with few videos
channelNameToFilter: "PewDiePie", // channel to filter for 2nd and 4th charts
numberLastVideosForChannelViewsPerDay: 15, // for the 3rd chart
minDateForChannelViewsPerDay: 1622930400000, // avoid partial days
numberLastVideosForAllChannelVideos: 8 // for the 4th chart
};</code></pre>
<p class="font-medium mt-8 text-base text-gray-600">
The other part, the chart, should not be modified even after data changes.<br />
Just to understand how it works, we are using chart.js and luxon, with chartjs-adapter-luxon.
</p>
<p class="font-medium mt-2 text-base text-gray-600">
Each of the charts is done differently but the idea remains. Take the original data, group and filter it, create a dataset and pass
it to the chart constructor.<br />
The first two are line charts and the last ones bar charts,but the same principles apply.
</p>
<p class="font-medium mt-10 text-base text-gray-600 sm:mt-16">
Created using <a class="hover:text-blue-700 text-blue-600" href="https://www.chartjs.org">Chart.js</a>,
<a class="hover:text-blue-700 text-blue-600" href="https://moment.github.io/luxon">Luxon</a> and
<a class="hover:text-blue-700 text-blue-600" href="https://github.com/chartjs/chartjs-adapter-luxon">chartjs-adapter-luxon</a>.
Thanks to all of them for working hard on open-source. 🙏
</p>
</main>
<footer>
<div class="max-w-6xl mx-auto px-4 sm:px-6">
<div class="md:flex md:items-center md:justify-between py-4 md:py-8 border-t border-gray-200">
<div class="text-sm text-gray-600 mr-4">© 2021 ZenRows, Inc. All rights reserved.</div>
</div>
</div>
</footer>
<script>
var dataConfig = {
maxViewCountFirstView: 500000,
minViewCount: 1000000,
minVideos: 8,
channelNameToFilter: 'PewDiePie',
numberLastVideosForChannelViewsPerDay: 15,
minDateForChannelViewsPerDay: 1622930400000,
numberLastVideosForAllChannelVideos: 8,
};
document.addEventListener('DOMContentLoaded', function () {
if (!window.dataConfig || !Array.isArray(dataConfig.dataset)) {
console.error('Please review you data.jsonp file');
return;
}
mapYT1();
mapYT2();
mapYT3();
mapYT4();
});
function mapYT1() {
var ctx = document.getElementById('yt-chart1').getContext('2d');
if (!ctx) {
console.error('There is a problem with the canvas node');
return;
}
var maxLength = 0;
var colorGenerator = getColorGenerator();
var dataByVideo = getDataGroupedByVideo();
var datasets = dataByVideo.map(function (data) {
var { channelName, title } = data[0];
var color = colorGenerator();
maxLength = Math.max(data.length, maxLength);
return {
label: channelName + ' - ' + truncate(title),
backgroundColor: color,
borderColor: color,
cubicInterpolationMode: 'monotone',
tension: 0.4,
pointRadius: 2,
data: data.map(function (v) {
return v.viewCount;
}),
};
});
var config = {
type: 'line',
data: {
labels: Array.from(Array(maxLength).keys()).map(formatHours),
datasets,
},
options: {
responsive: true,
scales: {
x: {
grid: {
display: false,
},
},
y: {
beginAtZero: true,
title: {
display: true,
text: 'Views',
},
},
},
},
};
new Chart(ctx, config);
}
function mapYT2() {
var ctx = document.getElementById('yt-chart2').getContext('2d');
if (!ctx) {
console.error('There is a problem with the canvas node');
return;
}
var maxLength = 0;
var colorGenerator = getColorGenerator();
var dataByVideo = getDataGroupedByVideo(true);
var datasets = dataByVideo.map(function (data) {
var { channelName, title } = data[0];
var color = colorGenerator();
maxLength = Math.max(data.length, maxLength);
return {
label: channelName + ' - ' + truncate(title),
backgroundColor: color,
borderColor: color,
cubicInterpolationMode: 'monotone',
tension: 0.4,
pointRadius: 2,
data: data.map(function (video) {
return {
x: video.date,
y: video.viewCount,
};
}),
};
});
var config = {
type: 'line',
data: {
datasets,
},
options: {
responsive: true,
scales: {
x: {
type: 'time',
time: {
unit: 'day',
tooltipFormat: 'DD T',
},
grid: {
display: false,
},
},
y: {
beginAtZero: true,
title: {
display: true,
text: 'Views',
},
},
},
},
};
new Chart(ctx, config);
}
function mapYT3() {
var ctx = document.getElementById('yt-chart3').getContext('2d');
if (!ctx) {
console.error('There is a problem with the canvas node');
return;
}
var channelViewsPerDay = {};
var byChannel = _.groupBy(dataConfig.dataset, 'channelName');
_.map(byChannel, function (channel, channelName) {
var t = _.uniqBy(channel, 'videoId');
var ids = _.map(_.slice(t, -dataConfig.numberLastVideosForChannelViewsPerDay), v => v.videoId);
var videos = _.filter(channel, v => ids.includes(v.videoId) && v.date >= dataConfig.minDateForChannelViewsPerDay);
var videosGrouped = _.groupBy(videos, 'videoId');
var acc = {};
_.map(videosGrouped, function (videosById) {
var vByDay = _.groupBy(videosById, formatVideoDate);
var days = {};
var prevDay;
var prevViewCount = 0;
_.map(vByDay, function (videos, day) {
var viewCount = _.minBy(videos, 'date').viewCount || 0;
if (prevDay) {
days[prevDay] = viewCount - prevViewCount;
}
days[day] = (_.maxBy(videos, 'date').viewCount || 0) - viewCount;
prevViewCount = viewCount;
prevDay = day;
});
acc = _.mergeWith(acc, days, function (a = 0, b = 0) {
return a + b;
});
});
channelViewsPerDay[channelName] = acc;
});
var colorGenerator = getColorGenerator();
var labels = _.uniq(
_.reduce(
channelViewsPerDay,
function (acc, c) {
acc.push(Object.keys(c));
return acc;
},
[]
)
.flat()
.sort()
);
var datasets = _.map(channelViewsPerDay, function (data, channelName) {
var color = colorGenerator();
return {
label: channelName,
backgroundColor: color,
borderColor: color,
data: _.map(labels, function (day) {
return data[day] || 0;
}),
};
});
var config = {
type: 'bar',
data: {
labels: labels,
datasets,
},
options: {
responsive: true,
scales: {
x: {
grid: {
display: false,
},
},
y: {
beginAtZero: true,
title: {
display: true,
text: 'Views',
},
},
},
},
};
new Chart(ctx, config);
}
function mapYT4() {
var ctx = document.getElementById('yt-chart4').getContext('2d');
if (!ctx) {
console.error('There is a problem with the canvas node');
return;
}
var datasets = [];
var colorGenerator = getColorGenerator();
var allVideos = _.filter(dataConfig.dataset, { channelName: dataConfig.channelNameToFilter });
var uniqVideosById = _.uniqBy(allVideos, 'videoId');
var ids = _.map(_.slice(uniqVideosById, -dataConfig.numberLastVideosForAllChannelVideos), v => v.videoId);
var videos = _.filter(allVideos, v => ids.includes(v.videoId));
var allDates = _.uniqBy(dataConfig.dataset, formatVideoDate).map(formatVideoDate);
var videoListById = _.groupBy(videos, 'videoId');
_.map(videoListById, function (videosById, videoId) {
var videosByDate = _.groupBy(videosById, formatVideoDate);
var viewCountByDate = _.reduce(
videosByDate,
function (acc, v, date) {
acc[date] = _.maxBy(v, 'viewCount').viewCount - _.minBy(v, 'viewCount').viewCount;
return acc;
},
allDates.reduce((acc, date) => {
acc[date] = 0;
return acc;
}, {})
);
datasets.push({
label: truncate(videosById[0].title),
backgroundColor: colorGenerator(),
data: viewCountByDate,
});
});
var config = {
type: 'bar',
data: {
datasets,
},
options: {
responsive: true,
scales: {
x: {
grid: {
display: false,
},
stacked: true,
},
y: {
beginAtZero: true,
title: {
display: true,
text: 'Views',
},
stacked: true,
},
},
},
};
new Chart(ctx, config);
}
function getDataGroupedByVideo(filterByChannelName = false) {
var dataByVideo = _.groupBy(dataConfig.dataset, 'videoId');
_.map(dataByVideo, videoList => {
if (
!videoList[0].viewCount ||
videoList[0].viewCount > dataConfig.maxViewCountFirstView ||
_.last(videoList).viewCount < dataConfig.minViewCount ||
videoList.length < dataConfig.minVideos ||
(filterByChannelName && videoList[0].channelName !== dataConfig.channelNameToFilter)
) {
delete dataByVideo[videoList[0].videoId];
}
return videoList;
});
return Object.values(dataByVideo);
}
function getColorGenerator() {
var colors = [
'#03045e',
'#94d2bd',
'#48cae4',
'#bb3e03',
'#ade8f4',
'#4d908e',
'#d9ed92',
'#577590',
'#003049',
'#7400b8',
'#00FFC5',
'#c73a57',
'#ADF5FF',
'#9FFFCB',
'#0075A2',
'#D55672',
'#78C0E0',
'#F39B6D',
'#F4AFB4',
'#7AE582',
];
return function () {
return colors.pop() || '#' + Math.random().toString(16).substr(2, 6);
};
}
function formatVideoDate(video) {
return luxon.DateTime.fromMillis(video.date).toFormat('yyyy-MM-dd');
}
function truncate(input, limit = 18) {
return input.length > limit ? `${input.substring(0, limit)}…` : input;
}
function formatHours(initialHours) {
const days = ~~(initialHours / 24);
const hours = ~~(initialHours % 24);
return `${days ? `${days}d ` : ''}${hours / 2}h`;
}
</script>
<script src="data.jsonp"></script>
</body>
</html>