import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
import proxml.*;
import proxml.XMLElement;
PFont font;
PeasyCam cam;
ArrayList graph = new ArrayList();
XMLInOut xmlIO;
String dateRange;
String siteUri;
String totalVisits;
float cScreen;
float notNumericValues; // take account of non numerical values to draw the graph correctly, starting from -elem_length/2
XMLElement[] monthlyVisits;
ArrayList numericaData = new ArrayList();
float dataMin = MAX_FLOAT;
float dataMax = MIN_FLOAT;
void setup(){
size(900,600,P3D);
smooth();
cam = new PeasyCam(this, 46);
xmlIO = new XMLInOut(this);
xmlIO.loadElement("imageslide-report.xml");
textFont(loadFont("ArialRoundedMTBold-30.vlw"), 1.0);
}
void xmlEvent(XMLElement element){
monthlyVisits = element.firstChild().getChild(3).getChildren();
dateRange = element.firstChild().getChild(1).getChild(2).firstChild().getText();
siteUri = element.firstChild().getChild(1).getChild(8).firstChild().getText();
totalVisits = element.firstChild().getChild(4).getChild(0).firstChild().firstChild().getText();
for(int i = 0; i < monthlyVisits.length; i++)
{
String dailyVisits = monthlyVisits[i].firstChild().getText();
if(itsNumber(dailyVisits))
{
DataMinMax(dailyVisits);
Float f = new Float(dailyVisits);
numericaData.add(f);
}
}
for(int j=0; j<numericaData.size(); j++) {
cScreen = numericaData.size()/2;
float f = (Float) numericaData.get(j);
String dailyVisits = monthlyVisits[j].firstChild().getText();
String dayOfVisit = element.firstChild().getChild(2).getChild(10).getChild(10+(j-1)).getChild(1).firstChild().getText();
float blockHeight = map(f, 0, dataMax, 0, 3);
graph.add(new Block(new PVector((-cScreen + j)*2, -1, 1),new PVector(1,blockHeight,1), 1, 1, 1, dailyVisits, dayOfVisit));
}
}
//void xmlEvent(XMLElement element){
// println(element.firstChild().getChild(2).getChild(10).getChild(10).getChild(1).firstChild().getText());
//}
void draw()
{
background(#ffffff);
rotateX(radians(-23));
pushMatrix();
fill(#000000);
textSize(1.2);
textAlign(CENTER);
text("Page URL: "+ siteUri + "\n" + "Time Interval: " + dateRange + "\n" + "Total Visits: " + totalVisits, 0, -12, 1);
popMatrix();
for(int j=0; j<graph.size(); j++) {
Block b = (Block) graph.get(j);
b.build();
}
// mm.addFrame();
}
boolean itsNumber(String in) {
try {
Float.valueOf(in.trim()).floatValue();
}
catch (NumberFormatException ex) {
notNumericValues++;
return false;
}
return true;
}
void DataMinMax(String v)
{
float value = Float.parseFloat(v);
if (value > dataMax) {
dataMax = value;
}
if (value < dataMin) {
dataMin = value;
}
}
class Block
{
PVector initPos = new PVector();
PVector scaleRatio = new PVector(1,1,1);
float rotX, rotY, rotZ;
String dailyVisits;
String weekDay;
//int txTure = int(random(6));
float[] coords = {
/*+z front*/ -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1,
/*-z back*/ 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1,
/*+y bottom*/ -1, 1, 1 , 1, 1, 1, 1, 1, -1 ,-1, 1, -1,
/*-y top*/ -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1,
/*+x right*/ 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1,
/*-x left*/ -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1
};
Block()
{
}
Block(PVector _initPos, PVector _scaleRatio, float _rotX, float _rotY, float _rotZ, String dv, String wd)
{
initPos = _initPos;
scaleRatio = _scaleRatio;
rotX = _rotX;
rotY = _rotY;
rotZ = _rotZ;
dailyVisits = dv;
weekDay = wd;
}
void build()
{
for(int i = 0; i <= 60; i+=12){
stroke(#000000);
line(coords[i]+initPos.x, 0, coords[i+2]+initPos.z, coords[i]+initPos.x, 0, coords[i+2]+initPos.z+2);
float dv = Float.parseFloat(dailyVisits);
if(dv >= dataMax){
fill(#24A4BC);
}
else fill(#ffffff);
beginShape(QUADS);
vertex((coords[i]+initPos.x)*scaleRatio.x, (coords[i+1]+initPos.y)*scaleRatio.y, (coords[i+2]+initPos.z)*scaleRatio.z);
vertex((coords[i+3]+initPos.x)*scaleRatio.x, (coords[i+4]+initPos.y)*scaleRatio.y, (coords[i+5]+initPos.z)*scaleRatio.z);
vertex((coords[i+6]+initPos.x)*scaleRatio.x, (coords[i+7]+initPos.y)*scaleRatio.y, (coords[i+8]+initPos.z)*scaleRatio.z);
vertex((coords[i+9]+initPos.x)*scaleRatio.x, (coords[i+10]+initPos.y)*scaleRatio.y, (coords[i+11]+initPos.z)*scaleRatio.z);
endShape();
fill(#000000);
textSize(0.45);
pushMatrix();
textAlign(CENTER);
rotateX(HALF_PI);
text("Visits \n"+dailyVisits, initPos.x, 3, 0);
pushMatrix();
textSize(0.7);
textAlign(RIGHT);
rotateZ(-HALF_PI);
text(weekDay, -5, initPos.x , 0);
popMatrix();
popMatrix();
}
}
}