//Homework #10 -- Data Visualzation and Animation //Erica D. Cochran //ericac@andrew.cmu.edu //Section B //Copyright © Erica D. Cochran //NOvember 1, 2009 //Carnegie Mellon University //Exploring Programming with Graphics //Pittsburgh, PA, USA //All Rights Reserved /** Instructions


Who was President of the U.S. at various prices of Gold?

Any key press will move to the next President

Read source code for programming information
U.S. President images retrieved from http://www.presidency.ucsb.edu/


To Start Over


Press the space bar at any time to start over.

When finished, either close program or
press the space bar to start over


*/ float [ ] gold = { 39.31, 41.28, 35.95, 38.21, 58.27, 97.21, 158.88, 160.87, 124.78, 147.81, 193.50, 305.84, 614.62, 459.21, 375.63, 423.68, 360.71, 317.35, 368.02, 446.53, 436.82, 381.04, 383.64, 362.29, 343.90, 359.93, 384.05, 384.06, 387.80, 331.14, 294.10, 270.24, 279.19, 271.11, 309.87, 363.57, 409.34, 444.71, 604.01, 695.90, 871.96, 935.00 }; PFont f1; PFont f2; PImage sky3; PImage nixon; PImage ford; PImage carter; PImage reagan; PImage bush; PImage clinton; PImage bush2; PImage obama; int xAxisLine; int yAxisLine; int xSpace; float verticalSpacingRatio; int highValueIndex; float highValue; void setup( ) { size( 1000, 300 ); f1 = loadFont( "f1.vlw"); f2 = loadFont( "f2.vlw"); sky3 = loadImage ("sky3.jpg"); nixon = loadImage ("nixon.jpg"); ford = loadImage ("ford.jpg"); carter = loadImage ("carter.jpg"); reagan = loadImage ("reagan.jpg"); bush = loadImage ("bush.jpg"); clinton = loadImage ("clinton.jpg"); bush2 = loadImage ("bush2.jpg"); obama = loadImage ("obama.jpg"); highValueIndex = findHighValueIndex(); highValue = gold[highValueIndex]; yAxisLine = (int)(height*.90); xAxisLine = (int)(width*.05); xSpace = (width - xAxisLine) /gold.length; verticalSpacingRatio = (yAxisLine-(yAxisLine*.1 ) )/ highValue ; // the offset keeps data on screen vertically background( sky3 ); } void draw( ) { drawAxis( ); drawTitle( ); drawBars (); drawGraph( ); repeat (); pres (); noLoop( ); } void drawGraph( ) { for(int i = 0; i < gold.length; i++) { stroke( 255, 214, 3); // sorta' a gold color strokeWeight( 7 ); //points hidden // point ( (xAxisLine + (xAxisLine*.22)) + (i*xSpace), yAxisLine - (gold[i]*verticalSpacingRatio) ); if (i > 0 ) { strokeWeight( 2 ); if ( gold[i] < gold[i-1] ) { stroke( 255, 0, 0 ); // price fell -- color is red } else { stroke( 20, 65, 235); // price rose - color is blue } line( (xAxisLine + (xAxisLine*.22)) + ( i*xSpace ), yAxisLine - (gold[i]*verticalSpacingRatio) , (xAxisLine + (xAxisLine*.22)) + ( (i-1)*xSpace ), yAxisLine - (gold[i-1]*verticalSpacingRatio) ); } } } void drawAxis( ) { textFont( f1 ); stroke (0, 0, 0, 50); strokeWeight( 1 ); // vertical axis line line( xAxisLine, 0, xAxisLine, yAxisLine); // horizontal axis line line( xAxisLine, yAxisLine, width, yAxisLine); // horizointal axis labels and lines int yr = 68; fill( 255, 255, 0); textFont( f1, 12 ); for(int x = xAxisLine; x < width- xSpace; x += xSpace) { if ( yr < 10) { text( "0"+yr, x + (xSpace*.15), height*.98); } else { text( yr, x + (xSpace*.15), height*.98); } yr++; if ( yr >= 100 ) { yr = 0; } } // vertical axis labels and lines if ( gold != null) { for( int i = 0; i <= 1000; i += 100 ) { println( xAxisLine + " " + (int)(yAxisLine - (i * verticalSpacingRatio ) ) ); line( xAxisLine-10, yAxisLine - (i * verticalSpacingRatio ) , xAxisLine, yAxisLine - (i * verticalSpacingRatio )); text( i, xAxisLine-40, yAxisLine - (i * verticalSpacingRatio )+5 ); } } } // Draws vertical bars at height of price void drawBars () { for(int i = 0; i < gold.length; i++) { fill (245, 214, 57, 125); // gold and translucent strokeWeight( 1 ); noStroke (); rect ( xAxisLine + ( i*xSpace ), yAxisLine - (gold[i]*verticalSpacingRatio), xSpace, height - ((yAxisLine - (gold[i]*verticalSpacingRatio)) - yAxisLine) - height ); } } //user input allows image to appear then rectangle around years he was to searve in office. //starts with blank image void pres () { if (frameCount == 1) { repeat (); } else if (frameCount == 2) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine, 0, xSpace*4, height ); image (nixon, xAxisLine, 0, xSpace*4, xSpace*4 ); } else if (frameCount == 3) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*4, 0, xSpace*4, height ); image (nixon, xAxisLine + xSpace*4, 0, xSpace*4, xSpace*4 ); } else if (frameCount == 4) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*6, 0, xSpace*2, height ); image (ford, xAxisLine + xSpace*6, 0, xSpace*2, xSpace*2 ); } else if (frameCount == 5) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*8, 0, xSpace*4, height ); image (carter, xAxisLine + xSpace*8, 0, xSpace*4, xSpace*4 ); } else if (frameCount == 6) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*12, 0, xSpace*4, height ); image (reagan, xAxisLine + xSpace*12, 0, xSpace*4, xSpace*4 ); } else if (frameCount == 7) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*16, 0, xSpace*4, height ); image (reagan, xAxisLine + xSpace*16, 0, xSpace*4, xSpace*4 ); } else if (frameCount == 8) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*20, 0, xSpace*4, height ); image (bush, xAxisLine + xSpace*20, 0, xSpace*4, xSpace*4 ); } else if (frameCount == 9) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*24, 0, xSpace*4, height ); image (clinton, xAxisLine + xSpace*24, 0, xSpace*4, xSpace*4 ); } else if (frameCount == 10) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*28, 0, xSpace*4, height ); image (clinton, xAxisLine + xSpace*28, 0, xSpace*4, xSpace*4 ); } else if (frameCount == 11) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*32, 0, xSpace*4, height ); image (bush2, xAxisLine + xSpace*32, 0, xSpace*4, xSpace*4 ); } else if (frameCount == 12) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*36, 0, xSpace*4, height ); image (bush2, xAxisLine + xSpace*36, 0, xSpace*4, xSpace*4 ); } else if (frameCount == 13) { repeat (); stroke (255, 100, 100); fill (247, 150, 150, 50); rect (xAxisLine + xSpace*40, 0, xSpace*4, height ); image (obama, xAxisLine + xSpace*40, 0, xSpace*4, xSpace*4 ); } else //return to graph with no images { repeat (); } } void repeat () { image (sky3, 0, 0); drawAxis( ); drawTitle( ); drawBars (); drawGraph( ); } void keyPressed () { if (key == ' ') { frameCount = 1; } loop (); } void drawTitle ( ) { textFont( f2, 20 ); fill (19, 90, 24); text( "Gold Prices 1968 to 2009", width/2-120, height*.1 ); text( "Compared with U.S. President", width/2-140, height*.2 ); } //Not utilized, but kept in program for possible further use int findHighValueIndex( ) { int highIndex = 0; for ( int i = 1 ; i < gold.length ; i++ ) { if (gold[i] > gold[highIndex] ) { highIndex = i; } } return highIndex; }