Table data;
PFont font;
//general
float center_x;
float center_y;
float centerCircleRad;
color bg = color(242,230,230);
color blue = color(13,66,81);
color grey = color(200);
//data
int total = 43289646;
int avg = 727688;
int max = 22111409;
int min = 1625;
//labels
int labelWidth = 300; //600 if arced around data circle
int labelHeight = 13;
void setup(){
fullScreen(P3D);
smooth(8);
loadData("simpleData.csv");
//font
font = createFont("univers.ttf", 150);
textFont(font);
center_x = width/2;
center_y = height/2;
centerCircleRad = sqrt(min);
}
void draw(){
background(242,230,230);
drawTitle();
pushMatrix();{
translate(center_x,center_y);
scale(0.1);
renderData(data);
}popMatrix();
}
void loadData(String url){
data = loadTable(url);
}
void renderData(Table t) {
int numResults = t.getRowCount();
float increment = map((360.0/numResults),0,360,0,TWO_PI);
//angle stuff
float angle = 0;
float x, y;
int j = 0;
int k = 0;
int m = 0;
for(int i = 0; i < numResults; i++){
x = (centerCircleRad/2) * cos(angle);
y = -((centerCircleRad/2) * sin(angle));
TableRow row = t.getRow(i);
//Country - str, name of country
String country = row.getString(0);
//Estimate - int, num of immigrants from this country in the USA
//Range ~= 5000 - 50mil
//float estimate = map(row.getInt(1), 1600, 23000000, 50, 500);
float estimate = sqrt(row.getInt(1));
//Error - sampling error on the estimate
int error = row.getInt(2);
float col = map(error, 670, 89000, 0, 100);
//draw circle for this estimate
stroke(140);
noFill();
pushMatrix();{
translate(0,0,-1);
//ellipse(0, 0, centerCircleRad + estimate * 2, centerCircleRad + estimate * 2);
}popMatrix();
//data lines
stroke(blue);
strokeWeight(5);
line(x, y, x + (estimate*cos(angle)), y - (estimate*sin(angle)));
//text(row.getInt(1), x + (estimate*cos(angle)), y - (estimate*sin(angle)));
if(angle > PI*0.5 && angle <= PI*1.5){
fill(blue);
text(country,
-(center_x * 10) + (20 * 10),
-(center_y * 10) + ((20 + (labelHeight * j)) * 10));
noFill();
stroke(grey);
j++;
//draw line under label
line(-(center_x * 10) + (20 * 10),
-(center_y * 10) + ((9 + (labelHeight * j)) * 10),
-(center_x * 10) + (labelWidth * 10),
-(center_y * 10) + ((9 + (labelHeight * j)) * 10));
//draw line from data point
line(x + (estimate*cos(angle)),
y - (estimate*sin(angle)),
x + (sqrt(max)*cos(angle)),
y - (sqrt(max)*sin(angle)));
int curveFix = (80 * 10);
//draw bezier to label
float c1x,c1y,c2x,c2y; // bezier controlpoints
c1x = -(center_x * 10) + (labelWidth * 10) + curveFix;
c1y = y - (sqrt(max)*sin(angle));
c2x = x + (sqrt(max)*cos(angle)) - curveFix;
c2y = -(center_y * 10) + ((9 + (labelHeight * j)) * 10);
//draw line to label
bezier(x + (sqrt(max)*cos(angle)),y - (sqrt(max)*sin(angle)), //line from data point
c1x,c1y,
c2x,c2y,
-(center_x * 10) + (labelWidth * 10), -(center_y * 10) + ((9 + (labelHeight * j)) * 10)); //line under label
} else if(angle < PI*0.5){
fill(blue);
text(country,
(center_x * 10) - ((labelWidth) *10),
(((-labelHeight * k)) * 10));
noFill();
stroke(grey);
k++;
//draw line under label
line((center_x * 10) - (20 * 10),
((15 + (-labelHeight * k)) * 10),
(center_x * 10) - (20 * 10) - ((labelWidth) * 10),
((15 + (-labelHeight * k)) * 10));
//draw line from data point
line(x + (estimate*cos(angle)),
y - (estimate*sin(angle)),
x + (sqrt(max)*cos(angle)),
y - (sqrt(max)*sin(angle)));
int curveFix = (100 * 10);
//draw bezier to label
float c1x,c1y,c2x,c2y; // bezier controlpoints
c1x = (center_x * 10) - (20 * 10) - ((labelWidth) * 10) - curveFix; //line under label x
c1y = y - (sqrt(max)*sin(angle)); // line from data point y
c2x = x + (sqrt(max)*cos(angle)) + curveFix; // line from data point x
c2y = ((15 + (-labelHeight * k)) * 10); //line under label y
//draw line to label
bezier(x + (sqrt(max)*cos(angle)),y - (sqrt(max)*sin(angle)), //line from data point
c1x,c1y,
c2x,c2y,
(center_x * 10) - (20 * 10) - ((labelWidth) * 10), ((15 + (-labelHeight * k)) * 10)); //line under label
} else {
fill(blue);
text(country,
(center_x * 10)- ((labelWidth) *10),
(center_y * 10) - (19 * 10) - (((labelHeight * m)) * 10));
noFill();
stroke(grey);
m++;
//draw line under label
line((center_x * 10) - (20 * 10),
(center_y * 10) - (3 * 10) - ((labelHeight * m) * 10),
(center_x * 10) - (20 * 10) - ((labelWidth) * 10),
(center_y * 10) - (3 * 10) - ((labelHeight * m) * 10));
//draw line from data point
line(x + (estimate*cos(angle)),
y - (estimate*sin(angle)),
x + (sqrt(max)*cos(angle)),
y - (sqrt(max)*sin(angle)));
int curveFix = (100 * 10);
//draw bezier to label
float c1x,c1y,c2x,c2y; // bezier controlpoints
c1x = (center_x * 10) - (20 * 10) - ((labelWidth) * 10) - curveFix; //line under label x
c1y = y - (sqrt(max)*sin(angle)); // line from data point y
c2x = x + (sqrt(max)*cos(angle)) + curveFix; // line from data point x
c2y = (center_y * 10) - (3 * 10) - ((labelHeight * m) * 10); //line under label y
//draw line to label
bezier(x + (sqrt(max)*cos(angle)),y - (sqrt(max)*sin(angle)), //line from data point
c1x,c1y,
c2x,c2y,
(center_x * 10) - (20 * 10) - ((labelWidth) * 10), (center_y * 10) - (3 * 10) - ((labelHeight * m) * 10)); //line under label
}
angle += increment;
}
}
void drawTitle(){
fill(255);
text("FOREIGN-BORN U.S.", 300, 200);
text("POPULATION", 300, 320);
text("(2015)", 300, 450);
fill(255);
}
Posted on and Updated on