int zipcode = 63103; int SIZE = 800; float SCALE = 0.8; // pie chart uses 80% of screen size String SOURCE = "2010Census.csv"; int ZIP_COL = 0; int START_COL = 7; int END_COL = 14; class Group { String label; float value; float pct; float startAngle; float endAngle; color fillColor; }; Group[] data; void setup() { size(SIZE, SIZE); textAlign(CENTER, CENTER); strokeWeight(2); textSize(28); loadData(); analyzeData(); renderData(); } // responsible for loading raw information from file void loadData() { String[] lines = loadStrings(SOURCE); String[] headers = split(lines[0], ','); int numRaces = 1 + END_COL - START_COL; data = new Group[numRaces]; for (int j=1; j < lines.length; j++) { String[] pieces = split(lines[j], ','); if (int(pieces[ZIP_COL]) == zipcode) { // found the zip code we want for (int k=0; k < numRaces; k++) { // create Group data[k] and set its label and value data[k] = new Group(); data[k].label = headers[k + START_COL]; data[k].value = float(pieces[k + START_COL]); } } } } // this is called after data is loaded, to compute relevant parameters void analyzeData() { float total=0.0; for (int j=0; j < data.length; j++) { total += data[j].value; } // compute percentages, and angles float theta = 0.0; for (int j=0; j < data.length; j++) { data[j].pct = data[j].value / total; data[j].startAngle = theta; theta += data[j].pct * TWO_PI; data[j].endAngle = theta; data[j].fillColor = color(random(255), random(255), random(255)); } } void renderData() { background(200); fill(0); float y = 0.25 * SIZE * (1-SCALE); text("Zipcode: " + zipcode, 0.5*height, y); stroke(0); for (int j=0; j < data.length; j++) { fill(data[j].fillColor); arc(0.5*width, 0.5*height, SIZE*SCALE, SIZE*SCALE, data[j].startAngle, data[j].endAngle, PIE); } } void draw() { } void mouseMoved() { float d = sqrt(sq(mouseX - 0.5*width) + sq(mouseY - 0.5*height)); if (d <= SIZE*SCALE*0.5) { // we are inside the circle float angle = atan2(mouseY - 0.5*height, mouseX - 0.5*width); if (angle < 0) { angle += TWO_PI; } println("Angle is " + angle); } }