// Divided Bar 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 x, y, barL, barH;
float startX, startY, stopX, stopY;
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, 250);
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;
}
// divided bar variables
barL = width-100;
barH = 50;
x = 50;
y = (height-barH)/2.0;
noLoop();
} // setup()
void draw() {
startX = x;
startY = y;
stopX = startX;
stopY = startY;
for (int i=0; i < perc.length; i++) {
// set up pie parameters for ith bar
startX = stopX;
startY = stopY;
stopX = startX + perc[i]*barL;
// draw the divided bar
noStroke();
fill(colors[i]);
rect(startX, startY, stopX-startX, barH);
// legend
stroke(0);
fill(0);
textSize(12);
if (i%2 == 0) {
line(startX, startY+barH, startX, startY+barH+10);
text(labels[i], startX, startY+barH+10+12);
}
else {
line(startX, startY, startX, startY-10);
text(labels[i], startX, startY-10);
}
}
// draw legend
// draw title
fill(0);
textSize(24);
rectMode(CENTER);
text("% Births by Day of Week", 100, 50);
} // draw()