Guest User

Netflix data viz

a guest
Aug 4th, 2024
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. let table;
  2. let entries = [];
  3. let years = [];
  4. let yearCounts = {};
  5. let yearSegments = [];
  6. const lineColor = '#8944EC';
  7. const margin = 100; // 5% of 1200 (height)
  8. const yearBarHeight = 20;
  9. const yearBarGap = 2;
  10.  
  11. function preload() {
  12. table = loadTable('netflix_data.csv', 'csv', 'header');
  13. }
  14.  
  15. function setup() {
  16. createCanvas(800, 1500);
  17.  
  18. // Extract data and unique years
  19. for (let i = 0; i < table.getRowCount(); i++) {
  20. let year = int(table.getString(i, 'year'));
  21. let title = table.getString(i, 'main_title');
  22. entries.push({ year, title });
  23. if (!years.includes(year)) {
  24. years.push(year);
  25. yearCounts[year] = 1;
  26. } else {
  27. yearCounts[year]++;
  28. }
  29. }
  30. years.sort((a, b) => a - b);
  31.  
  32. // Create year segments
  33. let totalCount = Object.values(yearCounts).reduce((a, b) => a + b, 0);
  34. let currentX = margin;
  35. let availableWidth = width - 2 * margin;
  36. for (let year of years) {
  37. let segmentWidth = (availableWidth - (years.length - 1) * yearBarGap) * (yearCounts[year] / totalCount);
  38. yearSegments.push({
  39. year: year,
  40. start: currentX,
  41. end: currentX + segmentWidth,
  42. count: yearCounts[year]
  43. });
  44. currentX += segmentWidth + yearBarGap;
  45. }
  46.  
  47. // Assign random positions to entries
  48. for (let entry of entries) {
  49. let segment = yearSegments.find(seg => seg.year === entry.year);
  50. entry.x = random(margin, width - margin);
  51. entry.y = random(margin, height / 3);
  52. entry.startX = random(segment.start, segment.end);
  53. }
  54. }
  55.  
  56. function draw() {
  57. background(0); // Black background
  58. drawVisualization();
  59. noLoop();
  60. }
  61.  
  62. function drawVisualization() {
  63. // Draw curved lines
  64. stroke(lineColor);
  65. strokeWeight(0.1875);
  66. noFill();
  67.  
  68. for (let entry of entries) {
  69. push();
  70. drawingContext.globalAlpha = 0.6;
  71. beginShape();
  72. vertex(entry.startX, height - margin + yearBarHeight / 2);
  73. let midY = (entry.y + height - margin) / 2;
  74. quadraticVertex(entry.x, midY, entry.x, entry.y);
  75. endShape();
  76. pop();
  77. }
  78.  
  79. // Draw white dots for titles
  80. fill(255, 255, 255, 128); // White with 50% opacity
  81. noStroke();
  82. for (let entry of entries) {
  83. ellipse(entry.x, entry.y, 1.5, 1.5); // 50% larger than previous (was 1, now 1.5)
  84. }
  85.  
  86. // Draw year bars and labels
  87. textAlign(CENTER, BOTTOM);
  88. noStroke();
  89.  
  90. for (let segment of yearSegments) {
  91. // Year bar
  92. fill(lineColor);
  93. rect(segment.start, height - margin, segment.end - segment.start, yearBarHeight);
  94.  
  95. // Year label
  96. fill(255);
  97. textStyle(BOLD);
  98. textSize(12);
  99. text(segment.year, (segment.start + segment.end) / 2, height - margin + yearBarHeight + 20);
  100.  
  101. // Total titles viewed
  102. textStyle(NORMAL);
  103. textSize(10);
  104. text(segment.count, (segment.start + segment.end) / 2, height - margin - 5);
  105. }
  106. }
  107.  
  108. function keyPressed() {
  109. if (key === 's' || key === 'S') {
  110. saveCanvas('netflix_visualization', 'pdf');
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment