// Drawing a Bar Chart of BMC Science Majors
// The data...
int[] nMajors;
String[] majors;
void setup() {
size(600, 600);
background(255);
textSize(12);
noLoop();
// Read data from a data file: SciMajors.txt
// The data file should be created using a Plain Text Editor
// It should be stored in the Data folder of the sketch
// Read the data file as an array of strings, each line in a string.
String[] lines = loadStrings("SciMajors.txt");
// Create the data arrays depending on how many lines were read from the data file
println("Read " + lines.length + " entries from data file.");
nMajors = new int[lines.length];
majors = new String[lines.length];
// Parse the strings in line[] into the data arrays
for (int i = 0; i < lines.length; i++) {
String[] pieces = split(lines[i], ",");
majors[i] = pieces[0];
nMajors[i] = int(pieces[1]);
}
} // setup()
void draw() {
float minX = 0.05*width;
float maxX = 0.95*width;
float minY = 0.05*height;
float maxY = 0.95*height;
float N = nMajors.length;
float barSpace = (maxX - minX)/N;
float barW = barSpace*0.9;
float x = minX;
noFill();
for (int i=0; i < N; i++) {
float h = map(nMajors[i], 0, 92, 0, maxY-minY);
float y = maxY-h;
fill(0);
rect(x, y, barW, h);
text(majors[i], x, y+h+10);
text(nMajors[i], x, y-10);
x = x + barSpace;
}
} // draw()