float[] volume; float minVolume, maxVolume;
int[] year;
float X1, X2, Y1, Y2;
PFont legendFont = createFont("SansSerif", 20);
void setup() { String[] lines = loadStrings("USPSData.txt"); volume = new float[lines.length]; year = new int[lines.length];
int j = 0; for (int i=0; i<lines.length; i++) { if (lines[i].charAt(0) == '#') continue; String[] pieces = split(lines[i], ","); // get the closing price of stock, and month volume[i] = float(pieces[1]); year[i] = int(pieces[0]); } println("Data Loaded:");
minVolume = min(volume); maxVolume = max(volume); println(minVolume); println(maxVolume); // drawing setup size(800, 400); // plot rectangle X1 = 120; X2 = width-30; Y1 = 50; Y2 = height - Y1; smooth(); textFont(legendFont); } // setup
void draw() { background(0); rectMode(CORNERS); noStroke(); fill(255); rect(X1, Y1, X2, Y2); stroke(0); fill(200); drawGraph(volume); // draw legend // title & Source info fill(255); textSize(18); textAlign(LEFT); text("US Postal Service First Class Mail Volume (in Billions)", X1, Y1 - 10); textSize(10); textAlign(RIGHT, BOTTOM); text("USPS.gov", width-10, height-10); // axis labels drawXLabels(); drawYLabels(); } // draw
void drawGraph(float[] data) { beginShape(); for (int i=0; i < data.length; i++) { float x = map(i, 0, data.length-1, X1, X2); float y = map(data[i], 0, maxVolume, Y2, Y1); vertex(x, y); } vertex(X2, Y2); vertex(X1, Y2); endShape(CLOSE); } // drawGraph
void drawXLabels() { fill(255); textSize(10); textAlign(CENTER);
for (int i=0; i<year.length; i+=10) { float x = map(i, 0, year.length, X1, X2); text(year[i], x, Y2+10); stroke(0); line(x, Y1, x, Y2); } textSize(18); textAlign(CENTER, TOP); text("Year", width/2, Y2+10); } // drawXLabels
void drawYLabels () { fill(255); textSize(10); textAlign(RIGHT); for (float i=0; i <= maxVolume; i += 10000) { float y = map(i, 0, maxVolume, Y2, Y1); text(floor(i/1000), X1-10, y); line(X1, y, X2, y); } textSize(18); text("Volume", X1-40, height/2); }
void mouseClicked(){ save("USPSFirstClassColume.tiff"); }