This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String [][] getCSVfile(String fname){ | |
String lines[] = loadStrings(fname); | |
int csvWidth=0; | |
for (int i=0; i < lines.length; i++) { | |
String [] chars=split(lines[i],','); | |
if (chars.length>csvWidth){ | |
csvWidth=chars.length; | |
} | |
} | |
//create csv array based on # of rows and columns in csv file | |
String [][] csv = new String [lines.length][csvWidth]; | |
//parse values into 2d array | |
for (int i=0; i < lines.length; i++) { | |
String [] temp = new String [lines.length]; | |
temp= split(lines[i], ','); | |
for (int j=0; j < temp.length; j++){ | |
csv[i][j]=temp[j]; | |
} | |
} | |
return csv; | |
} | |
color voteColour(String col){ | |
color retCol=#ffffff; | |
if (int(col)==-9) retCol=#000000; | |
else if (int(col)==1) retCol=#009900; | |
else if (int(col)==2){ retCol=#00ff00;} | |
else if (int(col)==3) retCol=#ffff00; | |
else if (int(col)==4) retCol=#ff0000; | |
else if (int(col)==5) retCol=#990000; | |
return retCol; | |
} | |
color diffColour(String col, int i, int p){ | |
color retCol=0; | |
int d=int(col)-3; | |
if ((d==1)&&(partymaj[i][p]<0)||(d==-1)&&(partymaj[i][p]>0)) retCol= 0; | |
else if ((d==-1)&&(partymaj[i][p]<0)) retCol=#00ff00; | |
else if ((d==1)&&(partymaj[i][p]>0)) retCol=#ff0000; | |
return retCol; | |
} | |
int [] mp; | |
int [] vote; | |
String [][] votes; | |
String [][] details; | |
int [] votemaj; | |
int [][] partymaj; | |
void pv(String typ, int p, int i){ | |
if (int(typ)==2) {partymaj[i][p]++;votemaj[i]++;} | |
else if (int(typ)==4) {partymaj[i][p]--;votemaj[i]--;} | |
} | |
boolean diffplot=true; | |
boolean missing=false; | |
void setup(){ | |
size(1400,800); | |
background(0); | |
// frameRate(1); | |
String fname="/Users/ajh59/Documents/vizdata/GE2010/votematrix-2005.csv"; | |
votes = getCSVfile(fname); | |
fname = "/Users/ajh59/Documents/vizdata/GE2010/votematrix-2005-det.csv"; | |
details = getCSVfile(fname); | |
votemaj= new int [votes.length]; | |
partymaj=new int [votes.length][4]; | |
for (int c=0;c<votes.length;c++) { | |
votemaj[c]=0; | |
for (int b=0;b<4;b++) partymaj[c][b]=0; | |
} | |
mp=new int [height]; for (int c=0;c<height;c++) mp[c]=-1; | |
vote=new int [width]; for (int c=0;c<width;c++) vote[c]=-1; | |
strokeWeight(1.5); | |
text("Voting in the 2005-10 UK Parliament", 50, 20); | |
text("x-axis: vote; y-axis: MP; red - no; green - yes",50,40); | |
text("Lab",10,65); | |
text("Con",10,500); | |
text("LibDem",10,430); | |
text("Other",10,710); | |
if (true) | |
for (int i=0;i<votes.length-1;i++) | |
for (int j=4;j<votes[0].length-3;j++) | |
for (int k=1; k<details.length;k++) | |
if (details[k][0].equals(votes[0][j])) | |
if (details[k][3].equals("Lab")){pv(votes[i][j],0,i); k=details.length;} | |
else if (details[k][3].equals("Con")){pv(votes[i][j],1,i); k=details.length;} | |
else if (details[k][3].equals("LDem")){pv(votes[i][j],2,i);k=details.length;} | |
else {pv(votes[i][j],3,i);k=details.length;} | |
if (true) | |
for (int i=1;i<votes.length-1;i++){ | |
int con=485;//415/485 | |
int lab=50;//50 | |
int lib=415;///625/415 | |
int other= 695; | |
for (int j=4;j<votes[0].length-3;j++){ | |
color vc=0; | |
if (!(diffplot)) vc=voteColour(votes[i][j]); | |
int yy=10+j; | |
int xx=100+i; | |
int party=-1; int mpi=-1; | |
for (int k=1; k<details.length;k++){ | |
if (details[k][0].equals(votes[0][j])){ | |
if (details[k][3].equals("Lab")){ | |
yy=lab; mpi=k;lab=lab+1;k=details.length;party=0; | |
} else if (details[k][3].equals("Con")){ | |
yy=con; mpi=k;con=con+1;k=details.length;party=1; | |
} else if (details[k][3].equals("LDem")){ | |
yy=lib; mpi=k;lib=lib+1;k=details.length;party=2; | |
} else { | |
yy=other;mpi=k; other=other+1;k=details.length;party=3; | |
} | |
} | |
} | |
if (diffplot) vc=diffColour(votes[i][j],i,party); | |
if (missing) if (int(votes[i][j])==-9) vc=255; else vc=0; | |
stroke(vc); | |
point(xx,yy); | |
mp[yy]=mpi; | |
vote[xx]=i; | |
} | |
} | |
} | |
void draw(){ | |
fill(0); | |
rect(0,750,width,48); | |
int mpc=votes[0].length-3+10; | |
int vc=votes.length+100; | |
int d=mp[mouseY]; | |
int v=vote[mouseX]; | |
if (d>-1) { | |
fill(255); | |
text(details[d][0]+": "+details[d][1]+" "+details[d][2]+" ("+details[d][3]+")",20,770); | |
} | |
if (v>-1) { | |
fill(255); | |
text(votes[v][3]+" ("+votes[v][2]+"): "+votemaj[v],20,790); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment