// Drawing a Pie Chart of BMC Science Majors // The data... int[] nMajors; String[] majors; void setup() { size(900, 600); background(255); textSize(12); noLoop(); // Read data from data file into data arrays // Read the data file into lines[] String[] lines = loadStrings("SciMajors.txt"); for (String line : lines) { println(line); } println("There were " + lines.length + " data items."); // Crate the data arrays majors = new String[lines.length]; nMajors = new int[lines.length]; // Parse the data in lines into the data arrays // Parse the lines into data arrays for (int i=0; i < lines.length; i++) { // Parse the ith line into data String[] pieces = split(lines[i], ","); majors[i] = pieces[0]; nMajors[i] = int(pieces[1]); } } // setup()
void draw() { float minX = width*0.05; float maxX = width*0.95; float minY = height*0.05; float maxY = height*0.95; float N = nMajors.length; float barSpace = (maxX - minX)/N; float barW = barSpace*0.8; // Draw the graph float x = minX; noFill(); beginShape(); for (int i = 0; i < N; i++) { // Plot the ith data bar float h = map(nMajors[i], 0, 100, 0, maxY-minY); float y = maxY - h; vertex(x, y); x = x + barSpace; } endShape(); } // draw()