Problem with a csv file in Processing

Hello all, I am a Processing rookie so maybe its a simple fault but my processing code does read my csv file, but doesnt draw anything like it does when i put it in regular arrays. my code:

    Table table;

     String land [] = {};//"Luxemburg","Noorwegen","Qatar","Zwitserland","Denemarken","Ierland","Nederland","Verenigde    Arabische Emiraten","Verenigde Staten","Oostenrijk","Australië","Finland","Zweden"
     float bbp []   = {};//104.5,79.1,68.9,67.6,56.1,51.4,48.2,46.9,46.4,46.0,45.6,44.5,44.0
     int nr []      = {};//13,12,11,10,9,8,7,6,5,4,3,2,1

     float r = random(250);

  void setup (){
  table = loadTable("data.csv" ,"header");

   for (TableRow row : table.rows()) {

   append(land, row.getString("land"));
   append(bbp, row.getFloat("bbp"));
   append(nr, row.getInt("nr"));
   println(land.length);

  size(2000,600);
  background(240);
  smooth();
  stroke(50,50,50,50);
  strokeWeight(2);

}
}

void draw (){
background(240);
line(0, height/2, width, height/2);
for(int i=0; i< nr.length;i++){
       float c = nr[i]*12;
fill(250, c,c,200);
ellipse(nr[i]*110 -20 , height/2, bbp[i]*3, bbp[i]*3);
fill(30);

pushMatrix();
translate(nr[i]*110 -20, height/2 - bbp[i]/2);
rotate(radians(-45));
text(land[i],-50 ,50);
text(bbp[i],10,10);
textSize(18);
popMatrix();

text("BBP per land plaats 1t/m13",10 ,height/3);
text("Bron: https://nl.wikipedia.org/wiki/Lijst_van_landen_naar_bbp_per_hoofd_van_de_bevolking", height/2, width/4);
smooth();
 }
 }

My csv:

land,bbp,nr
Luxemburg,104.5,1
Noorwegen,79.1,2
Qatar,68.9,3
Zwitserland,67.6,4
Denemarken,56.1,5
Ierland,51.4,6
Nederland,48.2,7
Verenigde Arabische Emiraten,46.9,8
Verenigde Staten, 46.4,9
Oostenrijk,46.0,10
Australië,45.6,11
Finland,44.5,12
Zweden,44.0,13

Answers

  • Table table;
    
    //String land2 [] = {
    //  "land,bbp,nr ", 
    //  "Luxemburg,104.5,1 ", 
    //  "Noorwegen,79.1,2 ", 
    //  "Qatar,68.9,3 ", 
    //  "Zwitserland,67.6,4 ", 
    //  "Denemarken,56.1,5 ", 
    //  "Ierland,51.4,6", "Nederland,48.2,7 ", 
    //  "Verenigde Arabische Emiraten,46.9,8 ", "Verenigde Staten, 46.4,9 ", "Oostenrijk,46.0,10 ", "Australië,45.6,11 ", "Finland,44.5,12 ", "Zweden,44.0,13"
    //};
    
    String land [] = {
    };//"Luxemburg","Noorwegen","Qatar","Zwitserland","Denemarken","Ierland","Nederland","Verenigde    Arabische Emiraten","Verenigde Staten","Oostenrijk","Australië","Finland","Zweden"
    float bbp []   = {
    };//104.5,79.1,68.9,67.6,56.1,51.4,48.2,46.9,46.4,46.0,45.6,44.5,44.0
    int nr []      = {
    };//13,12,11,10,9,8,7,6,5,4,3,2,1
    
    float r = random(250);
    
    void setup () {
    
      size(2000, 600);
      background(240);
      smooth();
    
      table = loadTable("data.csv", "header");
      //table = parseTable(land2, "header");
    
      for (TableRow row : table.rows ()) {
        land = append(land, row.getString("land"));
        bbp  = append(bbp, row.getFloat("bbp"));
        nr   = append(nr, row.getInt("nr"));
        println(land.length);
        println(nr);
      }
    
      stroke(50, 50, 50, 50);
      strokeWeight(2);
    }
    
    void draw () {
      background(240);
      line(0, height/2, width, height/2);
      for (int i=0; i< nr.length; i++) {
        float c = nr[i]*12;
        fill(250, c, c, 200);
        ellipse(nr[i]*110 -20, height/2, bbp[i]*3, bbp[i]*3);
        fill(30);
    
        pushMatrix();
        translate(nr[i]*110 -20, height/2 - bbp[i]/2);
        rotate(radians(-45));
        text(land[i], -50, 50);
        text(bbp[i], 10, 10);
        textSize(18);
        popMatrix();
    
        text("BBP per land plaats 1t/m13", 10, height/3);
        text("Bron: https://nl.wikipedia.org/wiki/Lijst_van_landen_naar_bbp_per_hoofd_van_de_bevolking", height/2, width/4);
        smooth();
      }
    }
    
  • edited September 2015

    make size the first line in setup()

    always

    you also had the closing } of the for loop too late in setup()

    then you need land = append..... etc. to make it work

Sign In or Register to comment.