/*************** * Author: Bradley McGinnis * Project 1: Andy's Utilities * FloatTable was written by Ben Fry * http://benfry.com/writing/series/FloatTable.pde ****************/ FloatTable data; float dataMin, dataMax; float plotX1, plotY1; float plotX2, plotY2; int currentColumn = 1; int columnCount; int yearMin = 1998; int yearMax = 2010; int currentYear = yearMin; int currentMonth = 0; int[] years; String[] months = { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int[] yIntervals = { 10, 10, 1, 50}; Boolean viewMode = false;//changes between single month and year comparison modes (year = false, month = true) String monthMode = "Compare years"; String yearMode = "View a month"; Button[] myButtons = new Button[5]; PFont plotFont; void setup() { size(1024,700); data = new FloatTable("data.csv"); columnCount = data.getColumnCount(); years = int(data.getRowNames()); dataMin = 0; dataMax = ceil( data.getColumnMax( currentColumn) / yIntervals[ currentColumn - 1]) * yIntervals[ currentColumn - 1]; plotX1 = 100; plotX2 = width - 50; plotY1 = 60; plotY2 = height - 100; plotFont = createFont("Arial", 20); textFont(plotFont); smooth(); myButtons[0] = new Button( 5, 5, 30, 20, "-"); myButtons[1] = new Button( 37, 5, 30, 20, "+"); myButtons[2] = new Button( 5, 27, 30, 20, "<"); myButtons[3] = new Button( 37, 27, 30, 20, ">"); myButtons[4] = new Button( width/2, height - 40, 150, 30, yearMode); } void draw() { background(224); fill(255); rectMode(CORNERS); noStroke(); rect( plotX1, plotY1, plotX2, plotY2); drawTitle(); drawXLabels(); drawYLabels(); strokeWeight(5); stroke(#5679C1); noFill(); if( viewMode) { drawMonthData(currentColumn, currentMonth); }else { drawYearData(currentColumn, currentYear); } drawButtons(); } void drawYearData(int col, int theYear) { int startRow, myMonth; startRow = 0; while ( years[startRow] != theYear) { startRow++; } //int rowCount = data.getRowCount(); if ( startRow == 0) { myMonth = 5; } else { myMonth = 12; } beginShape(); for ( int row = startRow; row < startRow + myMonth; row++) { if ( data.isValid(row, col)) { float value = data.getFloat( row, col); float x = map( data.getFloat( row, 0), 1, 12, plotX1, plotX2); float y = map(value, dataMin, dataMax, plotY2, plotY1); curveVertex( x, y); if((row == startRow) || (row == (startRow + myMonth - 1) )) { curveVertex(x,y); }//end if }//end if }//end for endShape(); } void drawMonthData( int col, int theMonth) { int startRow, myYear, yearCol; startRow = 0; yearCol = 0; while ( data.getFloat(startRow, yearCol) != theMonth + 1) { startRow++; } if ( startRow < 7) { myYear = 1999; }else { myYear = 1998; } beginShape(); for ( int row = startRow; row < data.getRowCount(); row += 12) { println( row); if ( data.isValid(row, col)) { float value = data.getFloat( row, col); float x = map( myYear, yearMin, yearMax, plotX1, plotX2); float y = map ( value, dataMin, dataMax, plotY2, plotY1); curveVertex( x, y); if((row == startRow) || (row == (startRow + ( 12 * 11)) )) { curveVertex(x,y); }//end if myYear++; }//endif }//end for endShape(); } void mousePressed() { int whichButton = -1; for( int i = 0; i < myButtons.length; i++) { if( mouseX > myButtons[i].x && mouseX < (myButtons[i].x + myButtons[i].w)) { if( mouseY > myButtons[i].y && mouseY < (myButtons[i].y + myButtons[i].h)) { whichButton = i; }//end if }//end if }//end for switch( whichButton) { case 0: if ( viewMode) { currentMonth --; if (currentMonth < 0) { currentMonth = 11; } }else { currentYear--; if (currentYear < yearMin) { currentYear = yearMax; } } break; case 1: if ( viewMode) { currentMonth ++; if (currentMonth > 11) { currentMonth = 0; } }else { currentYear++; if (currentYear > yearMax) { currentYear = yearMin; } } break; case 2: currentColumn--; if (currentColumn < 1) { currentColumn = columnCount - 1; } else if (currentColumn == 2) { currentColumn = 1; } dataMax = ceil( data.getColumnMax( currentColumn) / yIntervals[ currentColumn - 1]) * yIntervals[ currentColumn - 1]; break; case 3: currentColumn++; if (currentColumn == columnCount) { currentColumn = 1; } else if (currentColumn == 2) { currentColumn = 3; } dataMax = ceil( data.getColumnMax( currentColumn) / yIntervals[ currentColumn - 1]) * yIntervals[ currentColumn - 1]; break; case 4: if(viewMode) { myButtons[4].label = monthMode; }else { myButtons[4].label = yearMode; } viewMode = !viewMode; break; } } void mouseMoved() { for( int i = 0; i < myButtons.length; i++) { if( mouseX > myButtons[i].x && mouseX < (myButtons[i].x + myButtons[i].w)) { if( mouseY > myButtons[i].y && mouseY < (myButtons[i].y + myButtons[i].h)) { myButtons[i].mouseHover = true; } else { myButtons[i].mouseHover = false; }//end if } else { myButtons[i].mouseHover = false; }//end if }//end for } void drawXLabels() { fill(0); textSize(10); textAlign( CENTER, TOP); int offsetYear; //the code for viewing a single month over a couple of years if( viewMode) { if( currentMonth < 7) { offsetYear = 1999; }else { offsetYear = 1998; } for ( int myYear = offsetYear; myYear < offsetYear + 12; myYear++) { float x = map ( myYear, yearMin, yearMax, plotX1, plotX2); text( myYear, x, plotY2 + 10); } }else//the code for view years compared to one another { for ( int theMonth = 1; theMonth < 13; theMonth++) { float x = map ( theMonth, 1, 12, plotX1, plotX2); text( months[theMonth - 1], x, plotY2 + 10); } } } void drawTitle() { fill(0); textSize(20); textAlign(LEFT); String title = data.getColumnName(currentColumn); if (viewMode) { text( title + " for " + months[currentMonth], plotX1, plotY1 - 10); }else { text( title + " for " + currentYear, plotX1, plotY1 - 10); } } void drawYLabels() { fill(0); textSize(10); textAlign( RIGHT, CENTER); stroke(224); strokeWeight(1); for ( float v = dataMin; v < dataMax; v += yIntervals[ currentColumn - 1]) { float y = map( v, dataMin, dataMax, plotY2, plotY1); text( floor(v), plotX1 - 10, y); line( plotX1, y, plotX2, y); }//end for } void drawButtons() { rectMode(CORNER); for( int i = 0; i < myButtons.length; i++) { if( myButtons[i].mouseHover == false) { stroke( 0, 100, 255); } else { stroke( 255, 255, 0); } strokeWeight(2); fill( 0, 128, 255); rect( myButtons[i].x, myButtons[i].y, myButtons[i].w, myButtons[i].h); fill(0); textAlign( CENTER, CENTER); textSize( 20); text( myButtons[i].label, ((myButtons[i].x * 2) + myButtons[i].w)/2, ((myButtons[i].y * 2) + myButtons[i].h)/2); } } class Button { public int x, y, w, h; public String label; public Boolean mouseHover; public Boolean hasBeenPressed; public Button( int x, int y, int w, int h, String name) { this.x = x; this.y = y; this.w = w; this.h = h; this.label = name; mouseHover = false; hasBeenPressed = false; } }