We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Loading CSV (Read 1245 times)
Loading CSV
Nov 5th, 2009, 6:33am
 
Hi everyone, I'm experimenting with some DataVisualization.
I managed to load two different CSV files and visualize some circles that interpolates from the data on the first CSV to the one on the second CSV. A sort of parallel coordinates graph, but visualized in a different way.
My problem is that i can't manage to visualize the data if the values in each file are named in a different way, for example:

2008.csv:
A;10344
B;9000
C;7000
D;3456

2009.csv:
B;13344
D;8000
C;1500
E;1355

"A", passing from 2008 to 2009 has been substituted by "E"

this is the code:

Code:


int numBalls = 4;
int indList;
int oldIndList;

boolean sw;
boolean file=true;
Record[]records;
Record[]oldRecords;
String[]lines;
String[]oldLines;
int recordCount;
int oldRecordCount;

PFont font;

Ball[] balls = new Ball[numBalls];
Att[] att = new Att[numBalls];

void setup() {

size(screen.width, screen.height);

noStroke();
smooth();
sw=true;
indList=2009;
oldIndList=indList-1;

font = loadFont("Monospaced-12.vlw");

for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), 0, balls);
att[i]=new Att();
}

for (int i = 0; i < numBalls; i++) {
att[i].init();
}
}

void draw() {
background(242,255,229);

mouseOver();

load();

for(int i=0; i < recordCount; i++){
for(int j=0; j < oldRecordCount; j++){
if(records[i].name.equals(oldRecords[j].name)==true){
//println((i+1)+": "+records[i].val+" - "+(j+1)+": "+oldRecords[j].val );
//valore da attribuire all'attrattore, la spesa totale
att[j].k=(records[j].val)*0.000006;
balls[i].pos=j+1;
balls[i].diameter=sqrt(records[j].val)*0.2;
balls[i].name=records[j].name;
attract(i,j);
}
}

}


for (int i = 0; i < numBalls; i++) {
balls[i].move();
balls[i].display();
// att[i].render();
att[i].update();

}
}


class Ball {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;
String name;
int pos;
Ball[] others;
float alp=40;
float kx,ky;
boolean over=false;

Ball(float xin, float yin, float din, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
others = oin;
}

void move() {
x += vx+kx/100;
y += vy+ky/100;
}

void name(){
textFont (font);
float tw= textWidth(name+pos);
float th= textAscent();
noStroke();
fill(255);
rect(x-10+diameter,y+diameter-th-5,tw+30,th+10);
fill(0, 102, 153);
text(pos+"°"+" "+name,x+diameter,y+diameter);
//text(name,x+diameter,y+diameter);
}

void display() {
if(over==false){
noStroke();
fill(106,22,61,220);
ellipse(x, y, diameter, diameter);
over=true;
}

else{
noStroke();
fill(106,22,61,30);
ellipse(x, y, diameter, diameter);
noFill();
stroke(0);
strokeWeight(0.4);
ellipse(width/2,height/2,dist(x,y,width/2,height/2)*2,dist(x,y,width/2,height/2)*2);
over=false;
}
}
}


class Att{
float x;
float y;
float a;
float k;
float molt =0.000001;
void init(){
a=random(0,10);
}
void update(){
a+=(1/k)*molt;
x=sin(a)*(1/k)+width/2;
y=cos(a)*(1/k)+height/2;
}

void render(){
stroke(255);
strokeWeight(1);
point (x,y);
}
}


class Record{
String name;
float val;

public Record(String[] pieces){
name = pieces[0];
val = float(pieces[1]);
}
}

void mouseOver(){
for (int i = 0; i < numBalls; i++) {
if(mouseX<=(balls[i].x+(balls[i].diameter)/2) && mouseX>=(balls[i].x-(balls[i].diameter)/2) && mouseY>=(balls[i].y-(balls[i].diameter)/2) && mouseY<=(balls[i].y+(balls[i].diameter)/2)){
balls[i].name();
balls[i].over=true;
att[i].molt=0;
}
else{
balls[i].over=false;
att[i].molt=0.000001;
}
}
}


void mousePressed(){

oldIndList=indList;

if (indList==2009){
indList--;
}
else{
indList++;
}
}

void attract (int i, int j){
float dx=(balls[i].x-att[j].x);
float dy=(balls[i].y-att[j].y);
float d=dist(balls[i].x,balls[i].y,att[j].x,att[j].y);

if(d<1){
balls[i].kx=0;
balls[i].ky=0;
balls[i].x=att[j].x;
balls[i].y=att[j].y;
}

else{
balls[i].kx=(-dx/(d))*100;
balls[i].ky=(-dy/(d))*100;
}
}



void load(){

lines = loadStrings(indList+".csv");
oldLines=loadStrings(oldIndList+".csv");

records=new Record[lines.length];
oldRecords=new Record[oldLines.length];

recordCount=0;
oldRecordCount=0;

for(int i=0; i < lines.length; i++){
String[] pieces = split(lines[i], ';');
if (pieces.length==2){
records[recordCount]= new Record(pieces);
recordCount++;
}
}

for(int i=0; i < oldLines.length; i++){
String[] pieces = split(oldLines[i], ';');
if (pieces.length==2){
oldRecords[oldRecordCount]= new Record(pieces);
oldRecordCount++;
}
}
}




Thank you for your help.
Lorenzo
Re: Loading CSV
Reply #1 - Nov 5th, 2009, 9:37am
 
If I understood correctly your problem, you should store in the Record the line number. Then you compare by line number instead of comparing by name.
Page Index Toggle Pages: 1