// function to read the parsed txt file and use it // to draw dewey vs non dewey int [][] aDay = new int[24][2]; float plotX1, plotY1; float plotX2, plotY2; float labelX, labelY; String [] Hourslabel = {"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"}; PFont font; void setup() { size(900,300); String lines[] = loadStrings("splxmldata2010-10-29.txt"); font = loadFont("UniversLTStd-Cn-14.vlw"); // fills global array with 0 values for (int k=0;k<24;k++){ aDay[k][0]=0; aDay[k][1]=0; } // going line by line for (int i=0; i < lines.length; i++){ //breaks the string - give input string and separator String[] ParseList = split(lines[i],','); //if second string is empty then its non-dewy, so increment the count of non-dewey if(ParseList[1].length()==0){ //substring(0,2) - we are only reading the first two values - hour information aDay[int(ParseList[0].substring(0,2))][0]++; } else{ aDay[int(ParseList[0].substring(0,2))][1]++; } } //print out non-dewey to dewey // println ("Non-Dewey" + aDay[10][0] + " Dewey" + aDay[10][1]); // Setting the limits and margins plotX1 = 30; plotX2 = width - 30; labelX = 20; plotY1 = 30; plotY2 = height - 40; labelY = height - 20; } void draw(){ // Three different ways to plot the data, enable the one you want to see: // 1. Tile graphs with a greyscale drawGrayScale(); // 2. Bar graph // drawBar(); // 3. Spine Plot, height is normalized, width depends on the number of transactions. // drawSpinePlot(); } void drawGrayScale(){ float Hside; float Vside; int MaxD =0; int MaxND =0; int MaxScale=0; background(230); strokeWeight(1); Hside = (plotX2-plotX1)/24; Vside = (plotY2-plotY1)/2; // finding the maximum to adjust the scale: for (int k=0;k<24;k++){ if ((aDay[k][0] +aDay[k][1]) > MaxScale){ MaxScale = (aDay[k][0] +aDay[k][1]); } } stroke(0); for (int h =0;h<24;h++){ //255,0 are the colors float NewFill = map(aDay[h][1],0,MaxScale,255,0); fill(NewFill); rect(plotX1+h*Hside, plotY1,floor(Hside*.95),floor(Vside*.95)); NewFill = map(aDay[h][0],0,MaxScale,0,255); // text rect(plotX1+h*Hside, plotY1+Vside,floor(Hside*.95),Vside); } // Labeling textFont(font); fill(128); for (int h =0;h<24;h++){ textAlign(CENTER,CENTER); text(Hourslabel[h],plotX1 + Hside*(h+.5),plotY2+20); } } void drawBar(){ float Hside; float Vside; int MaxScale =0; background(230); strokeWeight(1); Hside = (plotX2-plotX1)/24; Vside = (plotY2-plotY1); // finding the maximun to adjust the scale: for (int k=0;k<24;k++){ if ((aDay[k][0] +aDay[k][1]) > MaxScale){ MaxScale = (aDay[k][0] +aDay[k][1]); } } fill(160); noStroke(); //The total for (int h =0;h<24;h++){ float NewHeigh = map((aDay[h][0]+aDay[h][1]),0,MaxScale,0,Vside); rect(plotX1+h*Hside, plotY2-NewHeigh,floor(Hside*.95),NewHeigh); } // The non Dewey ///////////////////// fill(160, 120,0); noStroke(); //The total for (int h =0;h<24;h++){ float NewHeigh = map((aDay[h][0]),0,MaxScale,0,Vside); rect(plotX1+h*Hside, plotY2-NewHeigh,floor(Hside*.95),NewHeigh); } //////////////////// // Labeling textFont(font); fill(128); for (int h =0;h<24;h++){ textAlign(CENTER,CENTER); text(Hourslabel[h],plotX1 + Hside*(h+.5),plotY2+20); } } void drawSpinePlot(){ float Hall; float Vside; float TotalSum; float WithAcum; background(230); strokeWeight(1); Hall = (plotX2-plotX1); Vside = (plotY2-plotY1); // finding the total sum to adjust the scale: TotalSum=0; for (int k=0;k<24;k++){ TotalSum += (aDay[k][0] +aDay[k][1]); } fill(160); noStroke(); //The total WithAcum=plotX1; for (int h =0;h<24;h++){ float NewWidth = map((aDay[h][0]+aDay[h][1]),0,TotalSum,0,Hall); rect(WithAcum, plotY1,floor(NewWidth*.95),Vside); WithAcum += NewWidth; } // The non Dewey fill(200,0,0); noStroke(); WithAcum=plotX1; for (int h =0;h<24;h++){ float NewWidth = map((aDay[h][0]+aDay[h][1]),0,TotalSum,0,Hall); float NewDeweyHeight =map(aDay[h][0],0,(aDay[h][0]+aDay[h][1]),0,Vside); rect(WithAcum, plotY2-NewDeweyHeight,floor(NewWidth*.95),NewDeweyHeight); WithAcum += NewWidth; } // border line noFill(); stroke(0); WithAcum=plotX1; for (int h =0;h<24;h++){ float NewWidth = map((aDay[h][0]+aDay[h][1]),0,TotalSum,0,Hall); rect(WithAcum, plotY1,floor(NewWidth*.95),Vside); WithAcum += NewWidth; } }