// data set (hardcoded based on our knowledge of format) String CENSUS_FILE = "2010Census.csv"; String CITY_FILE = "zip_names.csv"; float DIAM = 0.8; // as fraction of the overall canvas height int START_COL = 7; int NUM_GROUPS = 8; String label[] = {"White", "Black", "American Indian", "Asian", "Pacific", "Other", "Multiracial", "Latino"}; color c[] = {color(0, 0, 255), color(0, 255, 0), color(255, 0, 0), color(0, 255, 255), color(255, 0, 255), color(255, 255, 0), color(255, 127, 255), color(127, 50, 127)}; Table rawdata; float pct[] = new float[NUM_GROUPS]; int zip = 63103; // currently select zipcode String zipTemp = ""; float yZip, yLabel; void setup() { rawdata = loadTable(CENSUS_FILE, "header"); size(500, 500); textSize(0.25*height*(1-DIAM)); yZip = 0.25*height*(1-DIAM); yLabel = height - 0.25*height*(1-DIAM); loadPercentages(); noStroke(); } // load the pct array based on current zip void loadPercentages() { for (int j=0; j < NUM_GROUPS; j++) { pct[j] = 0.0; } // lets look for our zip code for (int r=0; r < rawdata.getRowCount(); r++) { if (zip == rawdata.getRow(r).getInt("Zip")) { for (int j=0; j < NUM_GROUPS; j++) { pct[j] = rawdata.getRow(r).getFloat(START_COL + j); } } } } void draw() { background(255); // are we in a pie slice??? float d = dist(width/2, height/2, mouseX, mouseY); float angle = atan2(mouseY-height/2, mouseX-width/2); float startAngle = -PI; for (int j=0; j < NUM_GROUPS; j++) { fill(c[j]); float endAngle = startAngle + TWO_PI*pct[j]; arc(width/2, height/2, DIAM*height, DIAM*height, startAngle, endAngle); if (d <= 0.5*DIAM*height && startAngle <= angle && angle <= endAngle) { // mouse is in this pie slice! fill(0); textAlign(CENTER, CENTER); text(label[j] + " " + nf(100*pct[j], 1, 2), width/2, yLabel); } startAngle = endAngle; } textAlign(LEFT, CENTER); fill(0); // black text text("Zip: ", 20, yZip); if (zipTemp.length() > 0) { fill(255,0,0); text(zipTemp, 20+textWidth("Zip: "), yZip); } else { text(zip, 20+textWidth("Zip: "), yZip); } } void keyPressed() { if (key >= '0' && key <= '9') { zipTemp += key; if (zipTemp.length() == 5) { zip = int(zipTemp); zipTemp = ""; loadPercentages(); } } }