// Bar Chart visualization of births by weekday
// The data variables...
// sun, mon, tue, wed, thu, fri, sat
int[] data = {
5, 5, 1, 4, 4, 4, 8
};
String[] labels = {
"SUN", "MON", "TUE", "WED",
"THU", "FRI", "SAT"
};
int total;
float[] perc = new float[7];
// The sketch variables
float maxW, maxH, barW, barH, lastX;
float x, y;
color [] colors = {
color(238, 118, 0), // sunday
color(123, 165, 248),
color(7, 57, 1),
color(255, 246, 63),
color(255, 0, 0),
color(0, 255, 0),
color(0, 0, 255) // saturday
};
void setup() {
size(500, 400);
background(255);
smooth();
// process
// compute the total population
total = 0;
for (int i=0; i < data.length; i++) {
total += data[i];
}
// compute percentages
for (int i=0; i < data.length; i++) {
perc[i] = float(data[i])/total;
}
// sketch variables
maxW = width/(1+data.length);
maxH = height;
barW = maxW/2;
lastX = 0;
noLoop();
} // setup()
void draw() {
for (int i=0; i < perc.length; i++) {
// set up parameters for ith bar
x = lastX+maxW;
barH = perc[i]*maxH;
// draw the bar
noStroke();
fill(colors[i]);
rect(x, height-barH-50, barW, barH);
lastX = x;
// labels
stroke(0);
fill(0);
textSize(12);
text(int(perc[i]*100)+"%", x, height-barH-65);
text(labels[i], x, height-25);
}
// draw title
fill(0);
textSize(24);
rectMode(CENTER);
text("% Births by Day of Week", 100, 50);
} // draw()