Fuel of the Week

I used data from my Nike FuelBand for this weeks project. “Fuel” is a proprietary unit that measures estimated activity over the course of the day. While “3206 Fuel” is meaningless on its own, this unit is an excellent relative measure of activity.

Fuel of the Week collects Fuel data from a .csv file, groups Fuel earned from every day of the week, and performs some math operations to create what we see below:

FergieFuel

Monday = m, Tuesday = t, Wednesday = w, Thursday = h, Friday = f, Saturday = s, Sunday = u.

Pressing a corresponding key will activate that day of the week. The size AND color of the column correspond to what proportion of weekly activity is attributed to that day (on average). Red = LOW, Green = HIGH. It will also show sample Fuel units from a day on that day of the week.

TIL it’s a good rule of thumb to conceptualize the final product before jumping into the code. This code can probably be made more succinct through use of a couple if statements and for loops.

Download the application here. Here is the code:

//Array of data
String [][] csv;
//sums
float mondaySum; float tuesdaySum;float wednesdaySum; float thursdaySum; float fridaySum; float saturdaySum; float sundaySum;
//proportions
float mondayP; float tuesdayP; float wednesdayP; float thursdayP; float fridayP; float saturdayP; float sundayP;
//cumulants
float mondayC; float tuesdayC; float wednesdayC; float thursdayC; float fridayC; float saturdayC; float sundayC;
//colors for mapping
color mon; color tue; color wed; color thu; color fri; color sat; color sun;
//counter and total for proportions
float counter; 
float total;
//visual presets
PFont font;
PImage swoosh;

void setup() {
//the basics
size(1000,1000);
font = loadFont("CenturyGothic-Bold-48.vlw");
textFont(font,32);
swoosh = loadImage("swoosh.png");

//CSV magic i borrowed from the internet
String lines[] = loadStrings("NikeFuel.csv");
int csvWidth=0;
//calculate max width of csv file
for (int i=0; i < lines.length; i++) {
  String [] chars=split(lines[i],',');
  if (chars.length>csvWidth){
    csvWidth=chars.length;
  }
}
//create csv array based on # of rows and columns in csv file
csv = new String [lines.length][csvWidth];
//parse values into 2d array
for (int i=0; i < lines.length; i++) {
  String [] temp = new String [lines.length];
  temp= split(lines[i], ',');
  for (int j=0; j < temp.length; j++){
   csv[i][j]=temp[j];
  }
  }

// make variables: sum, proportion, cumulants, color maps
for (int i=0; i<csv.length-6; i+=6) {
  counter = counter + i;
  mondaySum = mondaySum + parseInt(csv[i][1]);
  tuesdaySum = tuesdaySum + parseInt(csv[i+1][1]);
  wednesdaySum = wednesdaySum + parseInt(csv[i+2][1]);
  thursdaySum = thursdaySum + parseInt(csv[i+3][1]);
  fridaySum = fridaySum + parseInt(csv[i+4][1]);
  saturdaySum = saturdaySum + parseInt(csv[i+5][1]);
  sundaySum = sundaySum + parseInt(csv[i+6][1]);
  }

total = mondaySum + tuesdaySum + wednesdaySum + thursdaySum + fridaySum + saturdaySum + sundaySum;
mondayP = mondaySum/total;
tuesdayP = tuesdaySum/total;
wednesdayP = wednesdaySum/total;
thursdayP = thursdaySum/total;
fridayP = fridaySum/total;
saturdayP = saturdaySum/total;
sundayP = sundaySum/total;

//cumulants
mondayC = width*mondayP;
tuesdayC = mondayC + width*tuesdayP;
wednesdayC = tuesdayC + width*wednesdayP;
thursdayC = wednesdayC + width*thursdayP;
fridayC = thursdayC + width*fridayP;
saturdayC = fridayC + width*saturdayP;
sundayC = saturdayC + width*sundayP;

mon = color(map(mondayP,.13,.16,255,0),map(mondayP,.13,.16,0,255),0,10);
tue = color(map(tuesdayP,.13,.16,255,0),map(tuesdayP,.13,.16,0,255),0,10);
wed = color(map(wednesdayP,.13,.16,255,0),map(wednesdayP,.13,.16,0,255),0,10);
thu = color(map(thursdayP,.13,.16,255,0),map(thursdayP,.13,.16,0,255),0,10);
fri = color(map(fridayP,.13,.16,255,0),map(fridayP,.13,.16,0,255),0,10);
sat = color(map(saturdayP,.13,.16,255,0),map(saturdayP,.13,.16,0,255),0,10);
sun = color(map(sundayP,.13,.16,255,0),map(sundayP,0,.13,.16,255),0,10);

}

void draw () {
    noStroke();
    if (keyPressed) {
    if (key == 'm') {fill(mon);rect(0,0,mondayC,height);
    for (int i=0; i<csv.length-6; i+=6) {fill(255);text((csv[i][1]),0,i*6);};}
    if (key == 't') {fill(tue);rect(mondayC,0,width*tuesdayP,height);
    for (int i=0; i<csv.length-6; i+=6) {fill(255);text((csv[i+1][1]),mondayC,i*6);};}
    if (key == 'w') {fill(wed);rect(tuesdayC,0,width*wednesdayP, height);
    for (int i=0; i<csv.length-6; i+=6) {fill(255);text((csv[i+2][1]),tuesdayC,i*6);};}
    if (key == 'h') {fill(thu);rect(wednesdayC,0,width*thursdayP, height);
    for (int i=0; i<csv.length-6; i+=6) {fill(255);text((csv[i+3][1]),wednesdayC,i*6);};}
    if (key == 'f') {fill(fri);rect(thursdayC,0,width*fridayP,height);
    for (int i=0; i<csv.length-6; i+=6) {fill(255);text((csv[i+4][1]),thursdayC,i*6);};}
    if (key == 's') {fill(sat);rect(fridayC,0,width*saturdayP,height);
    for (int i=0; i<csv.length-6; i+=6) {fill(255);text((csv[i+5][1]),fridayC,i*6);};}
    if (key == 'u') {fill(sun);rect(saturdayC,0,width*sundayP,height);
    for (int i=0; i<csv.length-6; i+=6) {fill(255);text((csv[i+6][1]),saturdayC,i*6);};}
    }
    else {background(swoosh);
    fill(0);
    text("Press & Hold M,T,W,H,F,S, or U",300,.7*width);};
    fill(255);
}

 

 

Leave a Reply