Facebook
From Badaoui Noamane, 5 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 166
  1. final int ROW_HEIGHT = 30;
  2. final int COL_WIDTH = 200;
  3. final int COLOR_BOX_SIZE = 20;
  4. final int TEXT_OFFSET_X = 5;
  5. final int TEXT_OFFSET_Y = -10;
  6. final int LINE_OFFSET = 16;
  7. final int COLOR_COLUMN_OFFSET = 150;
  8. String[] getSectors() {
  9.   return new String[] {
  10.     "Health",
  11.     "Education",
  12.     "Public order and safety",
  13.     "Social security and welfare",
  14.     "Housing and community amenities",
  15.     "Transport and communications",
  16.     "Recreation and culture",
  17.     "General public services",
  18.     "Agriculture, forestry, fishing and hunting",
  19.     "Fuel and energy",
  20.     "Mining and mineral resources other than fuels",
  21.     "Other economic affairs",
  22.     "Other purposes",
  23.     "Total"
  24.   };
  25. }
  26.  
  27. int[] getBudgets() {
  28.   return new int[] {
  29.     5618, 4364, 1752, 1437, 1275, 1219, 421,
  30.     408, 213, 74, 73, 390, 766, sumArray(new int[]{5618, 4364, 1752, 1437, 1275, 1219, 421, 408, 213, 74, 73, 390, 766})
  31.   };
  32. }
  33.  
  34. color[] getColors() {
  35.   return new color[] {
  36.     color(255, 0, 0),    
  37.     color(0, 255, 0),    
  38.     color(0, 0, 255),    
  39.     color(255, 255, 0),  
  40.     color(255, 0, 255),  
  41.     color(0, 255, 255),  
  42.     color(128, 0, 0),    
  43.     color(128, 128, 0),  
  44.     color(0, 128, 0),  
  45.     color(128, 0, 128),  
  46.     color(0, 128, 128),  
  47.     color(0, 0, 128),    
  48.     color(128, 128, 128)
  49.   };
  50. }
  51.  
  52.  
  53. int sumArray(int[] values) {
  54.   int sum = 0;
  55.   for (int value : values) {
  56.     sum += value;
  57.   }
  58.   return sum;
  59. }
  60.  
  61. void setup() {
  62.   size(800, 800);
  63.   textSize(10);
  64. }
  65. void drawPieChart(int[] budgets, color[] colors, float centerX, float centerY, float diameter) {
  66.   int totalBudget = sumArray(budgets);
  67.   float lastAngle = 0;
  68.  
  69.   for (int i = 0; i < budgets.length - 1; i++) {
  70.      float proporti / totalBudget;
  71.     float angle = proportion * TWO_PI * 2;
  72.  
  73.     fill(colors[i]);
  74.     arc(centerX, centerY, diameter, diameter, lastAngle, lastAngle + angle);
  75.    
  76.     lastAngle += angle;
  77.  
  78.   }
  79.   save("Problem_Data_visualisation.png");
  80. }
  81.  
  82.  
  83. void draw() {
  84.   background(255);
  85.   drawTable();
  86.   drawPieChart(getBudgets(), getColors(), width/1.5 , height / 4, 300);
  87. }
  88.  
  89.  
  90.  
  91. void drawTable() {
  92.   String[] sectors = getSectors();
  93.   int[] budgets = getBudgets();
  94.   color[] colors = getColors();
  95.  
  96.   int x = 10;
  97.   int y = 40;
  98.  
  99.   drawColumnTitles(x, y);
  100.  
  101.   for (int i = 0; i < sectors.length; i++) {
  102.     int currentY = y + i * ROW_HEIGHT;
  103.  
  104.     drawText(sectors[i], x + TEXT_OFFSET_X, currentY);
  105.     drawText(str(budgets[i]), x + COL_WIDTH + TEXT_OFFSET_X, currentY);
  106.  
  107.     if (i < sectors.length-1 ) {
  108.       drawColorBox(colors[i], x + 2 * COL_WIDTH - COLOR_COLUMN_OFFSET + TEXT_OFFSET_X, currentY + TEXT_OFFSET_Y);
  109.     }
  110.  
  111.     drawHorizontalLine(x, currentY + ROW_HEIGHT / 2, 3 * COL_WIDTH - COLOR_COLUMN_OFFSET);
  112.   }
  113.  
  114.   drawVerticalLines(x, y, sectors.length);
  115.  
  116. }
  117.  
  118. void drawText(String text, int x, int y) {
  119.   fill(0);
  120.   text(text, x, y);
  121. }
  122.  
  123. void drawColorBox(color c, int x, int y) {
  124.   fill(c);
  125.   rect(x, y, COLOR_BOX_SIZE, COLOR_BOX_SIZE);
  126. }
  127.  
  128. void drawHorizontalLine(int x, int y, int length) {
  129.   line(x, y, x + length-150, y);
  130. }
  131.  
  132. void drawVerticalLines(int x, int y, int numRows) {
  133.   int endY = y + numRows * ROW_HEIGHT - LINE_OFFSET;
  134.   line(x, y - LINE_OFFSET - ROW_HEIGHT, x, endY);
  135.   line(x + COL_WIDTH, y - LINE_OFFSET - ROW_HEIGHT, x + COL_WIDTH, endY);
  136.   line(x + 2 * COL_WIDTH - COLOR_COLUMN_OFFSET, y - LINE_OFFSET - ROW_HEIGHT, x + 2 * COL_WIDTH - COLOR_COLUMN_OFFSET, endY);
  137.   line(x + 3 * COL_WIDTH - COLOR_COLUMN_OFFSET * 2, y - LINE_OFFSET - ROW_HEIGHT, x + 3 * COL_WIDTH - COLOR_COLUMN_OFFSET * 2, endY);
  138. }
  139.  
  140. void drawColumnTitles(int x, int y) {
  141.   drawText("Sector", x + 1, y - ROW_HEIGHT);
  142.   drawText("Budget", x + COL_WIDTH + 1, y - ROW_HEIGHT);
  143.   drawText("Color", x + 2 * COL_WIDTH + 1 - COLOR_COLUMN_OFFSET, y - ROW_HEIGHT);
  144. }