// An INTERACTIVE bar chart visualizer
 // As long as label-value pairs are provided it will draw a bar chart
 // Uses array initialization, and is more general in terms of
 // the size of the screen, number of bars, etc.
 // the labels to display under the bars
 // the labels to display under the bars
 String[] labels;
 // the frequencies as collected during the in class survey
 int[] num; 
// Font stuff
   PFont tnr;
void setup() {
 size(800, 500);
   smooth();
   background(255);
 fill(0);
 // Read data from file
   String[] lines = loadStrings("statePop2.txt");
   //println(lines);
 // Create data arrays
   labels = new String[lines.length];
   num = new int[lines.length];
 // Populate them (parse the data)
   for (int i=0; i < lines.length; i++) {
   // First split each line using commas as separator
   String[] pieces = split(lines[i], ",");
 labels[i] = pieces[0];
   num[i] = int(pieces[1]);
   }
 // Fonts
   tnr = createFont("Times New Roman", 48);
   } // setup()
void draw() {
   background(216, 151, 46);
 // set up plot dimensions relative to screen size
   float x = width*0.05;
   float y = height*0.75;
   float delta = width*0.9/num.length;
   float w = delta*0.8;
   int high = max(num);
 // Ticks
   fill(0);
   for (int i=5000000; i < high; i += 5000000) {
   float x1 = width*0.05;
   float x2 = width*0.95;
   float yi = map(i, 0, high, y, y-height*0.65);
   stroke(125, 125, 125);
   line(x1, yi, x2, yi);
   fill(75, 75, 75);
   textSize(10);
   text(i/1000000, x1-10, yi);
   pushMatrix();
   translate(width*0.025, height/2);
   rotate(3*PI/2);
   textSize(16);
   fill(0);
   text("Millions", 0, 0);
   popMatrix();
   }
   // Fonts...
   textFont(tnr);
 for (int i = 0; i < num.length; i++) {
   float h = map(num[i], 0, high, 0, height*0.65);
   if (inBox(mouseX, mouseY, x, y-h, w, h)) { // Mouse interaction
   //if (dist(mouseX, mouseY, x+w/2, y-h) < w/2) { // Mouse interaction
   fill(255);
   stroke(0);
   rect(x, y-h, w, h);
   textSize(36);
   String msg = labels[i]+": "+(num[i]/10000)/100.0+" Million";
   text(msg, (width-textWidth(msg))/2, 100 );
   }
   else { // no interaction
   fill(0);
   stroke(0);
   rect(x, y-h, w, h);
   }
 // Draw labels for each bar
   pushMatrix();
   translate(x, y);
   rotate(PI/2);
   textSize(12);
   text(labels[i], 2, 0);
   popMatrix();
 x += delta;
   }
 // Draw Title and legend
   textSize(9);
   fill(0);
   text("Source: US Census Bureau (www.census.gov)", width-200, height-20);
   text("Created by Deepak Kumar on October 29, 2013.", width-200, height-10);
   textSize(24);
   text("2012 US State Population Projections", width*0.05, 25);
   } // draw
boolean inBox(float mx, float my, float x, float y, float w, float h) {
   // test to see if moint <mx, my> is in the bounds of rect <x, y> <w,h>
   return (mx > x) && (mx < x+w) && (my > y) && (my < y+h);
} // inBox()