import java.util.*; String[] DeweyNames = {"Computer Science & Information", "Philosophy & Psychology", "Religion", "Social Science", "Languages", "Science & Mathematics", "Technology and Applied Science", "Arts and Recreation", "Literature", "History, Geography & Biography" }; int num=6; ArrayList transactions = null; float[] freq = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; String[] weekdays ={"M","T","W","R","F","S","S","M","T","W","R","F","S","S","M","T","W","R","F","S","S","M","T","W","R","F","S","S","M","T","W","R"}; boolean daydisplay = false; boolean grid = false; int total = 0; int curIdx = 0; int maxCnt = 0; int curKeyIdx = (int) 'a'; int date; int dew; int dkey = 1; int gridspacing = 20; float dataMin, dataMax; float barWidth = 10; float plotX1, plotY1; float plotX2, plotY2; float labelX, labelY; float colormap; Integrator[] interpolators; int rowCount; int columnCount; int currentColumn = 0; float[] tabLeft, tabRight; float tabTop, tabBottom; float tabPad = 10; int dayMin = 1, dayMax = 31; int[] years; int dayInterval = 1; int volumeInterval = 500; int volumeIntervalMinor = 250; PFont font0; PFont font1; PFont font2; void setup() { size(900,600); background(0); frameRate(60); font0 = loadFont("HelveticaNeue-Light-13.vlw"); font1 = loadFont("HelveticaNeue-Light-10.vlw"); font2 = loadFont("HelveticaNeue-Light-22.vlw"); long before = millis(); println("about to load..."); this.transactions = loadTransactions("splxmldata.txt"); //println("loaded " + this.transactions.size() + " transactions in " + (millis() - before) + " miiliseconds."); dataMin = 0; dataMax = 2500; plotX1 = 120; plotX2 = width - 60; labelX = 40; plotY1 = 60; plotY2 = height - 80; labelY = height - 40; for(int i=0; i < transactions.size(); i+=1){ Transaction t = (Transaction) transactions.get(i); date = Integer.parseInt(t.ckidate); if(t.deweyClass != null){ dew = Integer.parseInt(t.deweyClass); if(dew == num){ freq[date] = freq[date] +1; } } } interpolators = new Integrator[32]; for (int row = 0; row < 31; row++) { float initialValue = freq[row]; total = total + (int)freq[row]; interpolators[row] = new Integrator(initialValue); interpolators[row].attraction = 0.15; } println("KEY CONTROLS:"); println("VIEW GRID \t\tg"); println("VIEW DAYS \t\td"); println("NEXT CATEGORY \t]"); println("PREVIOUS CATEORY \t["); println("GRAPH MODES \t1-5"); smooth(); } void draw(){ background(255); drawGrid(); fill(128); rectMode(CORNERS); stroke(100); strokeWeight(1); rect(plotX1, plotY1, plotX2, plotY2); drawTitle(); drawAxisLabels(); drawVolumeLabels(); drawDayLabels(); for(int row=0; row < 31; row++){ interpolators[row].update(); } fill(255); drawMethod(dkey); drawDataHighlight(); textFont(font1); fill(180); textAlign(RIGHT); text("REZA ALI - MAT 259 - INFORMATION VISUALIZATION", width-5, height-15); text("PROJECT 1 - LINEAR", width-5, height-5); } void drawMethod(int k){ if(k == 1){ drawDataPoints(); } if(k == 2){ drawDataArea(); } if(k == 3){ drawDataLine(); } if(k == 4){ drawDataCurve(); } if(k == 5){ drawDataBars(); } } public ArrayList loadTransactions(String nameOfDataFileWithinDataDirectory) { ArrayList transactions = new ArrayList(); String[] lines = loadStrings(nameOfDataFileWithinDataDirectory); for (int i = 0; i < lines.length; i++) { transactions.add(parseTransaction(lines[i])); } return transactions; } // This function returns all the files in a directory as an array of File objects // This is useful if you want more info about the file public Transaction parseTransaction(String line) { String sections[] = split(line, ","); Transaction t = new Transaction(); t.ckidate = sections[0].substring(8,10); t.ckitime = sections[1].substring(0,2); if(sections.length> 2){ t.deweyClass = sections[2].substring(0,1); } return t; } void drawGrid(){ if(grid){ for(int k = 0; k < width; k += gridspacing){ stroke(128,80); strokeWeight(.5); line(0,k,width,k); line(k,0,k,height); } } } void drawDayLabels() { fill(100); //textSize(10); textAlign(CENTER, TOP); // Use thin, gray lines to draw the grid stroke(255,20); strokeWeight(1); for (int row = 0; row <= 31; row++) { if (row % dayInterval == 0) { float x = map(row, dayMin, dayMax, plotX1, plotX2); textFont(font0); if (row == dayMin) { textAlign(LEFT, CENTER); // Align by the bottom } else if (row == dayMax) { textAlign(RIGHT, CENTER); // Align by the top } else { textAlign(CENTER, CENTER); // Center vertically } if(row != 0){ fill(100); text(row, x, plotY2 + 10); if(daydisplay){ fill(170); text(weekdays[row], x, plotY2 + 23); } line(x, plotY1, x, plotY2); } } } } void drawVolumeLabels() { fill(100); //textSize(10); textFont(font0); stroke(128); strokeWeight(1); for (float v = dataMin; v <= dataMax; v += volumeIntervalMinor) { if (v % volumeIntervalMinor == 0) { // If a tick mark float y = map(v, dataMin, dataMax, plotY2, plotY1); if (v % volumeInterval == 0) { // If a major tick mark if (v == dataMin) { textAlign(RIGHT); // Align by the bottom } else if (v == dataMax) { textAlign(RIGHT, TOP); // Align by the top } else { textAlign(RIGHT, CENTER); // Center vertically } text(floor(v), plotX1 - 10, y); line(plotX1 - 4, y, plotX1, y); // Draw major tick } else { // Commented out, too distracting visually //line(plotX1 - 2, y, plotX1, y); // Draw minor tick } } } } void drawTitle() { fill(70); //textSize(20); textAlign(LEFT); textFont(font2); text("Dewey Category: " + DeweyNames[num], plotX1, plotY1 - 10); textAlign(RIGHT); text("Total Items " + nf(total,6,0), plotX2, plotY1 - 10); } void drawAxisLabels() { fill(100); //textSize(13); textLeading(15); textFont(font0); textAlign(CENTER, CENTER); // Use \n (enter/linefeed) to break the text into separate lines text("# of Items\nChecked\nIn", labelX, (plotY1+plotY2)/2); textAlign(CENTER, CENTER); text("Days of January 2008", (plotX1+plotX2)/2, labelY); } void drawDataPoints() { for (int row = 0; row < 31; row++) { float value = interpolators[row].value; float x = map(row+1, dayMin, dayMax, plotX1, plotX2); float y = map(value, dataMin, dataMax, plotY2, plotY1); //println(value); strokeWeight(1.5); line(x,plotY2,x,y); stroke(255); fill(128); ellipse(x, y, 7, 7); } } void drawDataLine() { noFill(); stroke(255); strokeWeight(1); beginShape(); for (int row = 0; row < 31; row++) { float value = interpolators[row].value; float x = map(row+1, dayMin, dayMax, plotX1, plotX2); float y = map(value, dataMin, dataMax, plotY2, plotY1); vertex(x,y); } endShape(); } void drawDataArea() { noStroke(); beginShape(); for (int row = 0; row < 31; row++) { float value = interpolators[row].value; float x = map(row+1, dayMin, dayMax, plotX1, plotX2); float y = map(value, dataMin, dataMax, plotY2, plotY1); vertex(x,y); } vertex(plotX2,plotY2); vertex(plotX1,plotY2); endShape(CLOSE); } void drawDataBars() { noStroke(); rectMode(CORNERS); for (int row = 0; row < 31; row++) { float value = interpolators[row].value; float x = map(row+1, dayMin, dayMax, plotX1, plotX2); float y = map(value, dataMin, dataMax, plotY2, plotY1); rect(x-barWidth/2, y, x+barWidth/2,plotY2); } } void drawDataCurve() { noFill(); strokeWeight(1); stroke(255); beginShape(); for (int row = 0; row < 31; row++) { float value = interpolators[row].value; float x = map(row+1, dayMin, dayMax, plotX1, plotX2); float y = map(value, dataMin, dataMax, plotY2, plotY1); curveVertex(x,y); if((row == 0) || (row == 30)){ curveVertex(x,y); } } endShape(); } void drawDataHighlight() { for (int row = 0; row < 31; row++) { float value = interpolators[row].value; float x = map(row+1, dayMin, dayMax, plotX1, plotX2); float y = map(value, dataMin, dataMax, plotY2, plotY1); if(dist(mouseX, mouseY, x, y) < 6){ strokeWeight(7); point(x,y); textFont(font0); fill(60); textAlign(CENTER); text((int)value + " (" + nf((row+1),2,0) + ")", x, y-8); } } } void keyPressed(){ if(key == ']'){ num = num+1; if(num == 10){ num = 9; } for(int j = 0; j < freq.length; j++){ freq[j] = 0; } for(int i=0; i < transactions.size(); i+=1){ Transaction t = (Transaction) transactions.get(i); date = Integer.parseInt(t.ckidate); if(t.deweyClass != null){ dew = Integer.parseInt(t.deweyClass); if(dew == num){ freq[date-1] = freq[date-1] +1; } } } calTotal(); } if(key =='['){ if(num == 0){ num = 1; } num = num - 1; for(int j = 0; j < freq.length; j++){ freq[j] = 0; } for(int i=0; i < transactions.size(); i+=1){ Transaction t = (Transaction) transactions.get(i); date = Integer.parseInt(t.ckidate); if(t.deweyClass != null){ dew = Integer.parseInt(t.deweyClass); if(dew == num){ freq[date-1] = freq[date-1] +1; } } } calTotal(); } if(key == 'd'){ daydisplay = !daydisplay; } if(key == 'g'){ grid = !grid; } if(key == '1'){ dkey = 1; } if(key == '2'){ dkey = 2; } if(key == '3'){ dkey = 3; } if(key == '4'){ dkey = 4; } if(key == '5'){ dkey = 5; } setCurrent(); } void setCurrent(){ for(int row=0; row < 31; row++){ interpolators[row].target(freq[row]); } } void calTotal(){ total = 0; for(int j = 0; j < freq.length; j++){ total = total + (int)freq[j]; } }