float[] volume;
float minVolume, maxVolume;
int[] year;
float plotX1, plotX2, plotY1, plotY2; // Plot area variables
PFont legendFont;
void setup() {
String[] lines = loadStrings("USPSData.csv"); // Read Data file
// Create Data arrays
volume = new float[lines.length];
year = new int[lines.length];
// Parse data
int j = 0;
for (int i=0; i<lines.length; i++) {
if (lines[i].charAt(0) == '#') continue;
String[] pieces = split(lines[i], ",");
// get the year and volume
year[i] = int(pieces[0]);
volume[i] = float(pieces[1]);
}
println("Data Loaded:");
minVolume = min(volume);
maxVolume = max(volume);
println(minVolume);
println(maxVolume);
// chart drawing setup
//size(800, 400);
fullScreen();
plotX1 = 120;
plotX2 = width-30;
plotY1 = 50;
plotY2 = height - plotY1;
legendFont = createFont("SansSerif", 20);
textFont(legendFont);
} // setup
void draw() {
background(0);
rectMode(CORNERS);
noStroke();
fill(255);
rect(plotX1, plotY1, plotX2, plotY2);
stroke(0);
fill(125);
// draw graph
drawGraph(volume, minVolume, maxVolume);
// draw legend
// title
fill(255);
textSize(18);
textAlign(LEFT);
text("US Postal Service First Class Mail Volume (in Billions)", plotX1, plotY1 - 10);
textSize(10);
textAlign(RIGHT, BOTTOM);
text("Source: USPS.gov", width-10, height-10);
// draw axis labels
drawXLabels();
drawYLabels();
} // draw
void drawGraph(float[] data, float minValue, float maxValue) {
beginShape();
for (int i=0; i < data.length; i++) {
float x = map(i, 0, data.length-1, plotX1, plotX2);
float y = map(data[i], 0, maxValue, plotY2, plotY1);
vertex(x, y);
}
vertex(plotX2, plotY2);
vertex(plotX1, plotY2);
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, plotX1, plotX2);
text(year[i], x, plotY2+10);
line(x, plotY1, x, plotY2);
}
textSize(18);
textAlign(CENTER, TOP);
text("Year", width/2, plotY2+10);
} // drawXLabels
void drawYLabels () {
fill(255);
textSize(10);
textAlign(RIGHT);
for (float i=minVolume-30; i <= maxVolume+30; i += 10000) {
float y = map(i, minVolume-30, maxVolume+30, plotY2, plotY1);
text(floor(i/1000), plotX1-10, y);
line(plotX1, y, plotX2, y);
}
textSize(18);
text("Volume", plotX1-40, height/2);
}