Skip to content

Instantly share code, notes, and snippets.

@Xeus
Created October 17, 2012 01:08
Show Gist options
  • Save Xeus/3903167 to your computer and use it in GitHub Desktop.
Save Xeus/3903167 to your computer and use it in GitHub Desktop.
Understanding Genomes Midterm: Data Viz Tests for Galapag.us
int SIZE_BOX = 20;
int SIZE_BAR = 10;
int SIZE_GRID = 5;
color mindColor = color(255, 0, 0);
color heartColor = color(0, 255, 0);
color bodyColor = color(0, 0, 255);
import org.json.*;
String BASE_URL = "http://127.0.0.1:5000";
String galapaguserName;
ArrayList<Integer> traits;
JSONArray galapaguser, galapaguser2;
User testUser, testUser2;
int[] userArray;
void setup() {
size(500, 500);
smooth();
galapaguser = getImportData(-1);
testUser = new User(galapaguser);
galapaguser2 = getImportData(99);
testUser2 = new User(galapaguser2);
}
void drawBox(int x, int y) {
rect(x, y, SIZE_BOX, SIZE_BOX);
}
void drawBar(int x, int y, int outwardness) {
rect(x, y, SIZE_BAR, outwardness);
}
void draw() {
background(0);
drawGrid();
//drawLine();
//fillScreen();
//drawUtilityOutwardness(testUser, 0);
//drawUtilityOutwardness(testUser2, 10);
}
void drawGrid() {
noStroke();
int j = 0;
int k = 0;
userArray = testUser.setUserArray();
for (int i=0; i<userArray.length; i++) {
fill(round(255 / 10 * userArray[i]));
drawBox(SIZE_BOX * j, SIZE_BOX * k);
if (j % SIZE_GRID == 0 && j != 0) { // desired col size - 1
k++;
j = 0;
}
else {
j++;
}
}
}
void drawUtilityOutwardness(User user, int offset) {
int[] outwardness = { 8, 3, 5, 2, 1, 5, 6, 6, 10, 4, 10, 2, 7, 8, 10, 3, 10, 3, 1, 9, 6 };
int j = 0;
noStroke();
ArrayList<Integer> mindArrayList = new ArrayList();
int[] mindArray = user.setMindArray();
for (int i = 0; i<mindArray.length; i++) {
mindArrayList.add(mindArray[i]);
fill(mindColor, round(255 / 10 * outwardness[j]));
drawBar(SIZE_BAR * j * 2 + SIZE_BAR + offset, 50, mindArrayList.get(i)*15);
j++;
}
ArrayList<Integer> heartArrayList = new ArrayList();
int[] heartArray = user.setHeartArray();
for (int i=0; i<heartArray.length; i++) {
heartArrayList.add(heartArray[i]);
fill(heartColor, round(255 / 10 * outwardness[j]));
drawBar(SIZE_BAR * j * 2 + SIZE_BAR + offset, 50, heartArrayList.get(i)*15);
j++;
}
ArrayList<Integer> bodyArrayList = new ArrayList();
int[] bodyArray = user.setBodyArray();
for (int i=0; i<bodyArray.length; i++) {
bodyArrayList.add(bodyArray[i]);
fill(bodyColor, round(255 / 10 * outwardness[j]));
drawBar(SIZE_BAR * j * 2+ SIZE_BAR + offset, 50, bodyArrayList.get(i)*15);
j++;
}
stroke(255);
line(0, 49, width, 49);
line(0, 125, width, 125);
line(0, 201, width, 201);
}
void drawLine() {
int j = 0;
noStroke();
ArrayList<Integer> mindArrayList = new ArrayList();
int[] mindArray = testUser.setMindArray();
for (int i = 0; i<mindArray.length; i++) {
mindArrayList.add(mindArray[i]);
fill(mindColor, round(255 / 10 * mindArrayList.get(i)));
drawBox(SIZE_BOX * j, 0);
j++;
}
ArrayList<Integer> heartArrayList = new ArrayList();
int[] heartArray = testUser.setHeartArray();
for (int i=0; i<heartArray.length; i++) {
heartArrayList.add(heartArray[i]);
fill(heartColor, round(255 / 10 * heartArrayList.get(i)));
drawBox(SIZE_BOX * j, 0);
j++;
}
ArrayList<Integer> bodyArrayList = new ArrayList();
int[] bodyArray = testUser.setBodyArray();
for (int i=0; i<bodyArray.length; i++) {
bodyArrayList.add(bodyArray[i]);
fill(bodyColor, round(255 / 10 * bodyArrayList.get(i)));
drawBox(SIZE_BOX * j, 0);
j++;
}
}
void fillScreen() {
noStroke();
int j = 0;
int k = 0;
userArray = testUser.setUserArray();
while (k * SIZE_BOX < height) {
for (int i=0; i<userArray.length; i++) {
fill(round(255 / 10 * userArray[i]));
drawBox(SIZE_BOX * j, SIZE_BOX * k);
if (j * SIZE_BOX > width) {
k++;
j = 0;
}
else {
j++;
}
}
}
}
JSONObject fetchJSON(String URL_PATH) {
// Get the JSON formatted response
String response = loadStrings(BASE_URL + URL_PATH)[0];
// Make sure we got a response.
if ( response != null ) {
// Initialize the JSONObject for the response
JSONObject jsonRoot = new JSONObject( response );
println(jsonRoot.toString());
return jsonRoot;
}
else {
return null;
}
}
JSONArray getImportData(int userNum) {
String mainImportURL = "/json/v1/profile/" + userNum;
JSONObject jsonImportData = fetchJSON(mainImportURL);
// Get the "galapagusers" JSONObject
JSONArray user = jsonImportData.getJSONArray("profile");
return user;
}
class User {
int u_strength = 0, u_app = 0, u_gra = 0, u_hum = 0, u_int = 0,
u_res = 0, u_sta = 0, u_hea = 0, u_rel = 0, u_hon = 0, u_mon = 0,
u_talA = 0, u_wis = 0, u_talS = 0, u_luc = 0, u_stress = 0,
u_ent = 0, u_edu = 0, u_cre = 0, u_wit = 0, u_dis = 0, u_cha = 0;
User(JSONArray galapagusers) {
JSONObject fetchedGalapaguser = (JSONObject)galapagusers.opt(0);
u_strength = fetchedGalapaguser.optInt("strength");
u_app = fetchedGalapaguser.optInt("appearance");
u_cha = fetchedGalapaguser.optInt("charisma");
u_dis = fetchedGalapaguser.optInt("discipline");
u_wit = fetchedGalapaguser.optInt("wit");
u_cre = fetchedGalapaguser.optInt("creativity");
u_edu = fetchedGalapaguser.optInt("education");
u_ent = fetchedGalapaguser.optInt("entrepreneurialism");
u_stress = fetchedGalapaguser.optInt("stress");
u_luc = fetchedGalapaguser.optInt("luck");
u_talS = fetchedGalapaguser.optInt("talent_sports");
u_wis = fetchedGalapaguser.optInt("wisdom");
u_talA = fetchedGalapaguser.optInt("talent_art");
u_mon = fetchedGalapaguser.optInt("money");
u_hon = fetchedGalapaguser.optInt("honesty");
u_rel = fetchedGalapaguser.optInt("religiosity");
u_hea = fetchedGalapaguser.optInt("health");
u_res = fetchedGalapaguser.optInt("responsibility");
u_sta = fetchedGalapaguser.optInt("stamina");
u_int = fetchedGalapaguser.optInt("intelligence");
u_hum = fetchedGalapaguser.optInt("humor");
u_gra = fetchedGalapaguser.optInt("gracefulness");
}
int[] setUserArray() {
int[] profile = {
u_strength, u_app, u_gra, u_hum, u_int, u_res,
u_sta, u_hea, u_rel, u_hon, u_mon, u_talA, u_wis,
u_talS, u_luc, u_stress, u_ent, u_edu, u_cre,
u_wit, u_dis, u_cha
};
return profile;
}
int[] setMindArray() {
int[] profile = {
u_hum, u_int, u_talA, u_wis, u_stress, u_edu, u_cre, u_wit
};
return profile;
}
int[] setHeartArray() {
int[] profile = {
u_res, u_rel, u_hon, u_luc, u_dis, u_ent, u_cha
};
return profile;
}
int[] setBodyArray() {
int[] profile = {
u_strength, u_app, u_gra, u_sta, u_hea, u_talS
};
return profile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment