/* * Graphs the historical temperatures for Saint Louis University. * * Data courtesy of: * http://www.weatherbase.com/weather/weather.php3?s=564732&cityname=St.-Louis--Saint-Louis-University-Missouri-United-States-of-America * * This version makes use of a plotTemperatures() function that * demonstrate how an array may be passed as a parameter. */ float[] high = { 39.8, 45.1, 55.4, 67.0, 76.8, 85.3, 89.1, 88.1, 80.8, 68.9, 55.9, 42.9 }; float[] low = { 23.8, 27.8, 37.0, 47.3, 57.4, 66.7, 71.0, 69.7, 61.1, 49.5, 39.3, 28.1 }; String[] month = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; int margin = 60; // number of pixels around the graph area void setup() { size(800, 600); background(255); textSize(16); strokeWeight(2); // plot horizontal axes textAlign(RIGHT, CENTER); stroke(127); fill(0); for (float t=0.0; t <= 100.0; t += 10.0) { float y = computeY(t); line(margin, y, width-margin, y); text(nf(t, 1, 1)+" ", margin, y); // nf = number format } // display the graph title textAlign(CENTER,CENTER); text("Average Monthly Temperatures for Saint Louis University", 0.5*width, 0.5*margin); // display the month labels textAlign(CENTER,TOP); for (int m=0; m < 12; m++) { float x = computeX(m); text(month[m], x, height-margin); } // plot the high temperatures plotDataLine(high, color(255,0,0)); // plot the low temperatures plotDataLine(low, color(0, 0, 255)); } void draw() { } // plot a data line for one set of temperatures in given color void plotDataLine(float[] temperature, color c) { stroke(c); fill(c); beginShape(); for (int m=0; m < 12; m++) { float x = computeX(m); float y = computeY(temperature[m]); vertex(x, y); ellipse(x, y, 5, 5); } noFill(); endShape(); } // determine an x-coordinate for ploting data about given month index // (with months indexed from 0 to 11) float computeX(int month) { float incr = (width - 2*margin) / 13.0; return margin + incr * (1 + month); } // map temperature scale from 0 to 100 degrees to // height of screen minus the margins float computeY(float temperature) { float incr = (height - 2*margin) / 100.0; return height - margin - incr * temperature; }