Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3552fb508b
Fetching contributors…

Cannot retrieve contributors at this time

file 302 lines (255 sloc) 6.568 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
/* CS 486: Information Visualization
* Final Visualization Project
*
* Rodrigo Thauby
*/

color colorA = color(247,182,8);
color colorB = color(255,255,255);
int width = 1024;
int height = 600;
PFont plotFont; // font for text labels
Timeline timeline; // timeline class
DataLoader data; // data values
int samples, lines;
int[][] values;
int[][] absValues;
boolean stroke = true;
boolean peaks = false;
boolean highlighting = false;
Category cat;
int selectedCategory = 0;
int categoryId = 1;

  // View specifics
  int[] yearMarks, monthMarks;
  int plotX1, plotY1; // upper-left corner of plot area
  int plotX2, plotY2; // lower-right corner of plot area
  int numberOfYearsToShow = 1;
  int startMonth = 420; // That's Jan 2010 in months terms
  int firstYear = 1975;
  
  
  String[] monthLabels;

void setup()
{
  debug("Initializing main window...");
  
  size(width,height);
  smooth();

  // initialize plot area
  plotX1 = 60;
  plotY1 = 40;
  plotX2 = width - plotX1;
  plotY2 = height - 40;
  
  monthLabels = new String[12];
  monthLabels[0] = "Jan";
  monthLabels[1] = "Feb";
  monthLabels[2] = "Mar";
  monthLabels[3] = "Apr";
  monthLabels[4] = "May";
  monthLabels[5] = "Jun";
  monthLabels[6] = "Jul";
  monthLabels[7] = "Aug";
  monthLabels[8] = "Sep";
  monthLabels[9] = "Oct";
  monthLabels[10] = "Nov";
  monthLabels[11] = "Dec";
  
  plotFont = createFont("SansSerif", 12, true);
  data = new DataLoader("games-large.csv");
  cat = new Category(data.genres);
  samples = numberOfYearsToShow * 12;
  lines = data.genres.length;
  timeline = new Timeline(data);
  
  monthMarks = new int[12];
  
  for(int i = 0; i < monthMarks.length; i++)
  {
    // equally place columns in space between plotX1 and plotX2
    monthMarks[i] = round(map(i, 0, monthMarks.length - 1, plotX1, plotX2));
  }
  
  debug(data.numRows() + " rows and " + data.numCols() + " columns loaded.");
  debug("Initialization complete.");
}

void draw()
{
  background(230);
  drawGrid();
  drawYear();
  getValues();
  drawArea();
  if(peaks) writePeaks();
  drawPlot();
}

void drawYear(){
  fill(0,0,0,16);
  textSize(120);
  textAlign(CENTER, TOP);
  int currYear = (startMonth / 12) + firstYear;
  text(currYear, round((plotX2 - plotX1) / 2) + 70, plotY1 + 100);
  textSize(12);
}

void getValues()
{
  absValues = new int[lines][samples];
  values = new int[lines][samples];
  int[] sums = new int[samples];
  for(int i=0; i<samples; i++) {sums[i]=0;}
  for(int i=0; i<lines; i++){
    
    int monthIndex = 0;
    String keyname = (String) cat.applet.categories[i];
    
    for(int j = startMonth; j < (startMonth + numberOfYearsToShow * 12); j++){
      ArrayList listArr = new ArrayList();
      switch(categoryId){
        case 1:
          listArr = (ArrayList) data.genreData.get(keyname);
          break;
        case 2:
          listArr = (ArrayList) data.platformData.get(keyname);
          break;
      }
      
      ArrayList month = (ArrayList) listArr.get(j);
      int yValue = month.size();
      sums[monthIndex] += yValue;
      values[i][monthIndex] = sums[monthIndex];
      absValues[i][monthIndex] = yValue;
      monthIndex++;
    }
    
       
  }
}

void writePeaks(){
  int i = lines-1;
  
  if(!highlighting){
    fill(100);
    for(int j=0; j<samples; j++){
        textAlign(CENTER, BOTTOM);
        int y = round(map(values[i][j], 0, data.yMax, plotY2, plotY1));
        text(values[i][j], monthMarks[j], y - 10);
    }
  }
  
  if(highlighting){
    i = selectedCategory;
    fill(0);
    for(int j=0; j<samples; j++){
        textAlign(CENTER, BOTTOM);
        int y = round(map(values[i][j], 0, data.yMax, plotY2, plotY1));
        text(absValues[i][j], monthMarks[j], y - 10);
    }
  }
  
  noFill();
}

void drawArea()
{
  for(int i=lines-1; i>=0; i--){
    stroke(255);
    if(stroke) strokeWeight(0.5);
    else noStroke();
    
    float lerpIndex = parseFloat(i) / parseFloat(lines);
    color fillColor = lerpColor(colorA,colorB,lerpIndex);
    if(highlighting && selectedCategory != i) fill(180);
    else if(highlighting) fill(colorA);
    else fill(fillColor);
    beginShape();
    
    vertex(plotX1, plotY2);

    
    for(int j=0; j<samples; j++){
      // Vertices go here
      int x = monthMarks[j];
      int y = round(map(values[i][j], 0, data.yMax, plotY2, plotY1));
      vertex(x,y);
    }
    
    

    vertex(plotX2, plotY2);
    
    endShape();
    
    noStroke();
    noFill();
  }
}

int monthToYear(int month)
{
  return (firstYear + (month / 12));
}

void drawPlot()
{
  fill(0);
  stroke(0);
  strokeWeight(1);
  textSize(9);

  line(plotX1,plotY2,plotX2,plotY2);
  line(plotX1,plotY2,plotX1,plotY1);

  for(int i = 0; i < monthMarks.length; i++)
  {
      textAlign(LEFT, TOP);
      text(monthLabels[i], monthMarks[i], plotY2 + 8);
      text(str(monthToYear(startMonth)), monthMarks[i], plotY2 + 20);
  }

  textAlign(RIGHT, CENTER);
  for(int i=0; i<5; i++){
    int yHeight = round(plotY1 + (parseFloat(plotY2 - plotY1) / 5) * i);
    text(round((parseFloat(data.yMax) / 5) * (5 - i)) + "\nun/month", plotX1 - 8, yHeight);
  }

  noStroke();
  noFill();
}

void drawGrid(){
  stroke(248);
  for(int i=0; i<5; i++){
    int yHeight = round(plotY1 + (parseFloat(plotY2 - plotY1) / 5) * i);
    line(plotX1,yHeight,plotX2,yHeight);
  }

  noStroke();
  noFill();
}

void switchToCategoryId(int id){
  categoryId = id;
  switch(id){
    case 1:
      cat.applet.categories = data.genres;
      lines = data.genres.length;
      break;
    case 2:
      cat.applet.categories = data.platforms;
      lines = data.platforms.length;
      break;
  }
}

void keyPressed()
{
  switch(key){
    case '1':
      selectedCategory=0;
      switchToCategoryId(1);
      break;
    case '2':
      selectedCategory=0;
      switchToCategoryId(2);
      break;
    case 'h':
      highlighting = !highlighting;
      if(highlighting) selectedCategory=0;
      break;
    case 's':
      stroke = !stroke;
      break;
    case 'p':
      peaks = !peaks;
      break;
    case '-':
    case '_':
    case '[':
      if(startMonth > 0) startMonth -= 12;
      break;
    case '+':
    case '=':
    case ']':
      if(startMonth < 432) startMonth += 12;
      break;
  }
  
  if(key == CODED)
  {

    if(keyCode == UP && selectedCategory > 0)
    {
      selectedCategory -= 1;
    }
    if(keyCode == DOWN && selectedCategory < lines-1)
    {
      selectedCategory += 1;
    }
  }
}

void debug(String message)
{
    println("DEBUG: " + message);
}
Something went wrong with that request. Please try again.