Facebook
From Evangelos Nikolaou, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 119
  1.  
  2. PFont boldFont, normalFont; // holds some custom fonts (bold vs normal weight)
  3.  
  4. void setup() {
  5.   size(1200, 500);
  6.   background(255);
  7.   fill(0);
  8.  
  9.   // fixed data for sectors and budgets (extracted from the PDF)
  10.   String[] sectors = new String[] {
  11.       "General public services",
  12.       "Public order and safety",
  13.       "Education",
  14.       "Health",
  15.       "Social security and welfare",
  16.       "Housing and community amenities",
  17.       "Recreation and culture",
  18.       "Fuel and energy",
  19.       "Agriculture, forestry, fishing and hunting",
  20.       "Mining and mineral resources other than fuels",
  21.       "Transport and communications",
  22.       "Other economic affairs",
  23.       "Other purposes"
  24.     };
  25.    
  26.   int[] budgets = new int[] {
  27.       408, 1752, 4364, 5618, 1437, 1275, 421, 74, 213, 73, 1219, 390, 766
  28.   };
  29.  
  30.   color[] colors = randomColors(budgets.length);
  31.  
  32.   // creates desired fonts
  33.   boldFont = createFont("Arial Bold", 12);
  34.   normalFont = createFont("Arial", 12);
  35.  
  36.   // sets the default (normal weight) font
  37.   textFont(normalFont);
  38.  
  39.   // Stage 1 requirement: renders the data in a grid
  40.   renderDataAsGrid(sectors, budgets, colors);
  41.  
  42.   // Stage 2 requirement: calculate sum of budgets and show it up
  43.   int totalBudget = sumArray(budgets);
  44.   renderTotalSum(totalBudget);
  45.  
  46.   // Stage 3: display the pie
  47.   int[] angles = calculateAngles(budgets, totalBudget);
  48.   pieChart(angles, colors);
  49.  
  50.   save("visual1.png");
  51. }
  52.  
  53. /*
  54.   Calculates the relative angles for all budgets so as they can be used in a pie
  55. */
  56. int[] calculateAngles(int[] budgets, int totalBudget) {
  57.   int[] angles = new int[budgets.length];
  58.   int sum = 0;
  59.   for(int i=0; i < angles.length; i++) {
  60.     int x = (int)((budgets[i] * 360) / totalBudget);
  61.     angles[i] = x;
  62.     sum += x;
  63.   }
  64.   if(sum < 360) { // adjust the possible remainder in radians due to previous roundings
  65.     int diff = 360 - sum;
  66.     for(int j=1; j <= diff; j++) {
  67.       angles[j%budgets.length] += 1; // just add 1 angles to every segment till all the diff is used
  68.     }
  69.   }
  70.   return angles;
  71. }
  72.  
  73. /*
  74.   Sets random colors into a lookup table for segments
  75. */
  76. color[] randomColors(int len) {
  77.   int[] colors = new int[len];
  78.   for(int i=0; i < colors.length; i++) {
  79.     colors[i] = color(random(0,255), random(0,255), random(0,255));
  80.   }
  81.  
  82.   return colors;  
  83. }
  84.  
  85. /*
  86.   Renders the total sum of budgets under the grid
  87. */
  88. void renderTotalSum(int totalBudget) {
  89.   textFont(boldFont);
  90.  
  91.   int tm = (height / 2) - 150;
  92.   text("Total budget:", 210, (tm + 40) + (13 * 20));
  93.   text(totalBudget, 300, (tm + 40) + (13 * 20));
  94.  
  95.   textFont(normalFont);
  96. }
  97.  
  98. /*
  99.   Calculates the sum of integer values of a given array
  100. */
  101. int sumArray(int[] values) {
  102.   int sum = 0;
  103.   for(int i=0; i < values.length; i++) {
  104.     sum += values[i];
  105.   }
  106.  
  107.   return sum;
  108. }
  109.  
  110. /*
  111.   Display sectors and budgets within a grid on canvas
  112. */
  113. void renderDataAsGrid(String[] sectors, int[] budgets, color[] colors) {
  114.   // captions & basic lines
  115.   textFont(boldFont);
  116.  
  117.   int tm = (height / 2) - 150;
  118.  
  119.   line(20, tm, 500, tm);
  120.   text("Sector", 25, tm+15);
  121.   text("Budget ($million)", 300, tm+15);
  122.   text("Color", 455, tm+15);
  123.   line(20, tm+20, 500, tm+20);
  124.  
  125.   textFont(normalFont);
  126.  
  127.   line(20, tm, 20, (tm+20) + (13 * 20));
  128.   line(295, tm, 295, (tm+20) + (13 * 20));
  129.   line(450, tm, 450, (tm+20) + (13 * 20));
  130.   line(500, tm, 500, (tm+20) + (13 * 20));
  131.  
  132.   for(int i=0; i < sectors.length; i++) {
  133.     text(sectors[i], 25, (tm + 35) + (i * 20));
  134.     text(budgets[i], 300, (tm + 35) + (i * 20));
  135.     fill(colors[i]);
  136.     rect(452, (tm + 35) + ((i-1) * 20) + 8, 46, 14);
  137.     fill(0);
  138.     line(20, (tm + 20)  + (i * 20), 500, (tm + 20)  + (i * 20));    
  139.   }
  140.  
  141.   line(20, (tm + 20)  + (13 * 20), 500, (tm + 20)  + (13 * 20));
  142. }
  143.  
  144. /*
  145.   Creates the pie considering the budgets and colors of segments
  146. */
  147. void pieChart(int[] data, color[] colors) {
  148.   float lastAngle = 0;
  149.   int diameter = height - 50;
  150.  
  151.   for (int i = 0; i < data.length; i++) {
  152.     fill(colors[i]);
  153.     arc(550 + (diameter / 2), height/2, diameter, diameter, lastAngle, lastAngle+radians(data[i]));
  154.     lastAngle += radians(data[i]);
  155.   }
  156. }
  157.