// some variables that are being used throughout the program int kMargin = 25; int kSize = 10; color bgColor = 200; // medium grey int n; int highlight=-1; // coordinates int[] xPt, yPt, y25Pt, y75Pt; // data values float[] x, y, y25, y75; float minX, minY, maxX, maxY; void setup() { size(600, 400); background(bgColor); // load data from local file String lines[] = loadStrings("meps-small.csv"); println("there are " + lines.length + " lines"); n = lines.length; xPt = new int[n]; yPt = new int[n]; y25Pt = new int[n]; y75Pt = new int[n]; x = new float[n]; y = new float[n]; y25 = new float[n]; y75 = new float[n]; // read data from file for (int i=0; i < lines.length; i++) { println(lines[0]); String list[] = split(lines[i], ','); // list[0] year // list[1] count // list[2] median expense // list[3] lower quartile expense // list[4] upper quartile expense x[i] = float(list[0]); y[i] = float(list[2]); y25[i] = float(list[3]); y75[i] = float(list[4]); } // find minimum and maximum in X and Y direction minX = maxX = x[1]; minY = y25[1]; maxY = y75[1]; for (int i=2; i < n; i++) { if (x[i] < minX) minX = x[i]; if (x[i] > maxX) maxX = x[i]; if (y25[i] < minY) minY = y25[i]; if (y75[i] > maxY) maxY = y75[i]; } // compute x, y coordinates for points for (int i = 1; i < n; i++) { xPt[i] = getXCoord(x[i]); yPt[i] = getYCoord(y[i]); y25Pt[i] = getYCoord(y25[i]); y75Pt[i] = getYCoord(y75[i]); } } int getXCoord (float xval) { int w = width - 2*kMargin; return(kMargin+round(1.0*(xval-minX)/(maxX-minX)*w)); } int getYCoord (float yval) { int h = height - 2*kMargin; return(kMargin + h- round(1.0*(yval-minY)/(maxY-minY)*h)); } int onCircle() { // set for (int i=1; i < n; i++) { if (overCircle(xPt[i], yPt[i], kSize)) return(i); } return(-1); } boolean overCircle(int x, int y, int diameter) { float disX = x - mouseX; float disY = y - mouseY; if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) { return true; } else { return false; } } void draw() { // return index of data, if mouse is on one of the points highlight = onCircle(); // draw shape between lower and upper quartile fill(bgColor-20); noStroke(); beginShape(); vertex(xPt[1]-kSize,y25Pt[1]); for (int i = 1; i < n; i++) { vertex(xPt[i],y25Pt[i]); } vertex(xPt[n-1]+kSize,y25Pt[n-1]); vertex(xPt[n-1]+kSize,y75Pt[n-1]); for (int i = n-1; i >= 1; i--) { vertex(xPt[i],y75Pt[i]); } vertex(xPt[1]-kSize,y75Pt[1]); endShape(CLOSE); // draw points for (int i = 1; i < n; i++) { noStroke(); fill(230); ellipse(xPt[i],yPt[i],kSize,kSize); } // draw highlighted point if (highlight > -1) { noStroke(); fill(70,130,180); ellipse(xPt[highlight],yPt[highlight],kSize+2,kSize+2); } // Hello Mouse fill(0); ellipse(mouseX, mouseY, 3,3); } void mousePressed() { fill(bgColor); rect(0,0,width, height); }