getting data

We picked data of death causes in newyork open source page.

and we arrange in a graphic drawing by year as the following video

here’ s the code:

String filename = "death.csv";
String[] rawData;
ArrayList allYears = new ArrayList();

int overallMin, overallMax;
float xSpacer, ySpacer;

int currLevel = 2007;

void setup() {
size(800, 500);
processData();
}

void draw() {

background(40);
drawGUI();
for (Year y : allYears) {
y.display();
}
}
void mouseReleased() {
for (Year y : allYears) {
if (dist(mouseX, mouseY, y.button.x, y.button.y) <y.buttonSize/2) {
currLevel = y.level;
}
}
}

void drawGUI() {
stroke(100);
fill(200);
String[] ethnicity = {
“Asian&Pacific”, “Hispanic”, “NH-Black”, “NH-White”
};
int [] years = {
2007, 2008, 2009, 2010, 2011
};

//x-asis
for (int x=0; x<ethnicity.length; x++) {
float xPos = xSpacer + (xSpacer*x);
line(xPos, height – ySpacer, xPos, ySpacer);
text(ethnicity[x], xPos – 25, height – (ySpacer – 20));
}
//min/max values
text(overallMin,xSpacer-50,height-ySpacer);
text(overallMax,xSpacer-50,ySpacer);

//instructions
text(“Homicides per year:”, 50, 25);

}

void processData() {
rawData = loadStrings(filename);
for (int i =1; i< rawData.length; i +=4) {
Year y = new Year();
String [] firstRow = split(rawData[i], “,”);
y.level = int(firstRow[0]);
for (int j = 0; j<4; j++) {
String[] thisRow = split(rawData[i+j], “,”);
y.number[j] = int(thisRow[3]);
}
y.setMinMax();
allYears.add(y);
}

xSpacer = width / (4+1);
ySpacer = 100;
int [] mins = new int [5];
int [] maxs = new int [5];
for (int i =0; i < allYears.size (); i++) {
Year y = allYears.get(i);
mins[i] = y.min;
maxs[i] = y.max;
}
overallMin = min(mins);
overallMax = max(maxs);

println(overallMin);
println(overallMax);

for (Year y : allYears) {
y.setValues();
}
}

class Year {
//variables
int level;
String[] ethnicity = {
“Asian&Pacific”, “Hispanic”, “NH-Black”, “NH-White”
};
int [] number = new int[4];
int min, max;
PVector[] pos = new PVector[4];

PVector button = new PVector();
int buttonSize = 40;

//constructor
Year() {
}
void display() {

if (currLevel == level) {
stroke(255, 0, 0);
fill(255, 0, 0);
for (int i = 0; i< pos.length; i++) {
fill(255);
text(number[i], pos[i].x, pos[i].y-15);
fill(255, 0, 0);
ellipse(pos[i].x, pos[i].y, 10, 10);
if (i line(pos[i].x, pos[i].y, pos[i+1].x, pos[i+1].y);
}
}
fill(200);
stroke(0);
ellipse(button.x, button.y, buttonSize, buttonSize);
fill(255);
text(level, button.x – 15, button.y+4);
} else {
noFill();
stroke(0);
ellipse(button.x, button.y, buttonSize, buttonSize);
fill(255);
text(level, button.x – 15, button.y+4);
}
}

//function
void setMinMax() {
min = min(number);
max = max(number);
}

void setValues() {
float graphHeight = (height – ySpacer) – ySpacer;
for (int i = 0; i<number.length; i ++) {
float adjVal = map(number[i], overallMin, overallMax, 0, graphHeight);
pos[i] = new PVector();
pos[i].x = xSpacer*(i+1);
pos[i].y = (height – ySpacer) – adjVal;
}

//button stuff
button.y = 25;
button.x = 250+(((level%1000 ) – 6)* 60);
}
}

 

Leave a Reply