# Loading data / Constructor problem

**URL:** <https://discourse.processing.org/t/loading-data-constructor-problem/5179>\
**Category:** Coding Questions\
**Created:** [November 4, 2018, 9:26am UTC](https://discourse.processing.org/t/loading-data-constructor-problem/5179 "2018-11-04T09:26:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)\
**Post date:** [November 4, 2018, 9:26am UTC](https://discourse.processing.org/t/loading-data-constructor-problem/5179/1 "2018-11-04T09:26:55Z")

</div>

Hi Everyone,

I am learning processing with Daniel @shiffman Book (learning processing + Youtube video) and got stuck at exercise 18-7 (Data input from text files). I can’t find the solution online, that is why i am asking for help here.

Here is my problem. I have a data.txt file with three different lines

```auto
BOB,40,100,50,90,180,150,122.5,130
PENELOPE,78,159,30,90,180,98,67.5,189
JANE,89,98,162,73,241,23,116.5,91

```

I want to create a sketch to visualizes the data with the following layout for each lines

 ![23](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5cdd40fa7424d6da72d6f5391c173afb74cd57d1.jpeg)

I managed coding this for one line but when i am turning my code in a object oriented version, my constructor doesn’t work and i am not sure to do the right thing anymore…

 ![28](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4431f5260c53580e80eccb1ae5b58124a5bcac45.png)

```auto
Board[] boards;

//Variables data
String [] values;
float [] score;

void setup(){
  size(400,250);
  
  String [] player = loadStrings("data.txt");
  boards = new Board[player.length];
  
  float x = 0;
  float w = width / boards.length;
  
  for (int i = 0; i < boards.length; i++){
      
    values = split(player[i],",");
    score = float(split (player[i],","));
    
    for (int j = 0; j < score.length; j++){
    
    float[]score2 = subset(score,1);
    float l = w /score2[j];
    
    boards = new Board(values[0],score2[j],l,w);
    x += w;
    }
  }
}

void draw(){
  
  background(255);
  for (int i = 0; i < boards.length; i++){
    boards.title();
    boards.pourcentage();
  }
  noLoop();
}

class Board{
  //float total;
  //float avg;
  String headline1;
  float score1;
  float l1;
  float w1;
  float x1;
  
  PFont f;
  
  Board (String _headline1, float _score1, float _l1, float _w1){
    headline1 = _headline1;
    score1 = _score1;
    l1 = _l1;
    w1 = _w1;
    x1 = 0;
    f = createFont ("Arial",32,false);
  }
  
  void title(){
    pushMatrix();
    fill(100,100);
    translate (w1/2,40);
    textFont(f);
    textAlign(CENTER);
    text(headline1,0,0);
    popMatrix();
  }
  
  void pourcentage(){
    stroke(255,200);
    strokeWeight(2);
    fill(100,100);    
    rect(x1,height,l1,-score1);
    x1 += w1;
   }  
 }

```

Can someone help me please?

Thank you,

Rémy

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 4, 2018, 10:29am UTC](https://discourse.processing.org/t/loading-data-constructor-problem/5179/2 "2018-11-04T10:29:30Z")

</div>

> pls. give link to video , code source…

some things could be more easy with

> **[loadTable() / Reference](https://processing.org/reference/loadTable_.html)**
>
> Reads the contents of a file or URL and creates a Table object with its values. If a file is specified, it must be located in the sketch's "data" folder. The filename parameter can also be a URL to a …

```auto
Table table;

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

```

for a unknown content better start with

```auto
Table table;
int i, k, trows, tcols;

void setup() {

  table = loadTable("test.csv", "tsv"); // "test.tsv" // "test.csv" , "tsv" // "test.dat" , "header , csv"
  trows=table.getRowCount();
  tcols=table.getColumnCount();

  println(trows + " rows/lines in table "); //(? - header ?)
  println(tcols + " cols in table");
  println("header: ");
  for ( i =0; i < tcols; i++) {
    println("col: "+i+" "+ table.getColumnTitle(i));
  }

  for ( k =0; k < trows; k++) {
    for ( i =0; i < tcols; i++) {
      println("row: "+k+" col: "+i+" string: "+table.getString(k, i));
    }
  }
}

```

with this table ( string ) and usage of

```auto
table.getFloat(k, i)

```

there is no need for more string or float arrays.

// \_\_\_\_\_\_\_\_\_\_\_\_  
for your Boards class

```auto
float _score1

```

should be the row array of data, not one single value.

and actually a tablerow works fine ( instead of a String array of one line )  
my version:

```auto

Table table;
int i, k, trows, tcols;

float rwidth=0; //(width-40)/tcols;
float rhighrange = 0;
int max;

boolean dprint = true; // print first calc only

My_graph[] charts;

void setup() {
  size(500, 500);
  background(100, 100, 0);
  check_table();
  get_ranges();
  charts = new My_graph[trows];
  for ( k =0; k < trows; k++) {
    charts[k] = new My_graph(table.getRow(k), 10, 20+k*(130+5), width-20 , 130, 0, max); //
    charts[k].draw();
  }
}

// tools: get the data and check for columns, rows, and data range

void check_table() {
  table = loadTable("data.txt", "csv"); // "test.tsv" // "test.txt" , "tsv" // "test.dat" , "header , csv"
  trows=table.getRowCount();
  tcols=table.getColumnCount();

  if (dprint) println(trows + " rows/lines in table "); //(? - header ?)
  if (dprint) println(tcols + " cols in table");
  if (dprint) println("header: ");
  for ( i =0; i < tcols; i++) {
    if (dprint) println("col: "+i+" "+ table.getColumnTitle(i));
  }

  for ( k =0; k < trows; k++) {
    for ( i =0; i < tcols; i++) {
      if (dprint) println("row: "+k+" col: "+i+" string: "+table.getString(k, i));
    }
    if (dprint) println(" ");
  }
}

void get_ranges() {
  rwidth=(width-40)/(tcols);
  for ( k =0; k < trows; k++) {
    for ( i =1; i < tcols; i++) {
      float getfnum = table.getFloat(k, i);
      if ( getfnum > rhighrange ) rhighrange = getfnum;
    }
  }
  max = ceil(rhighrange/100.0); // need a loop for 10 100 1000
  max = max*100;
  if (dprint) println("highest value: "+rhighrange+" max "+max+" rwidth "+rwidth);
}

class My_graph {
  TableRow data; 
  int posX, posY, wX, hY; 
  float nullr, highr;
  PFont f;
  float local_rwidth;
  
  // expect data = {"name","1","2","3","4","5.66","6"...}
  My_graph(TableRow _data, int _posX, int _posY, int _wX, int _hY, float _nullr, float _highr) {  
    data = _data; 
    posX = _posX; 
    posY = _posY; 
    wX = _wX; 
    hY = _hY; 
    nullr = _nullr; 
    highr = _highr;

    f = createFont ("Arial", 32, false);
    
    local_rwidth = (wX-20)/tcols;
  }

  void draw() {
    // background and header
    pushMatrix();
    translate (posX, posY); 
    fill(200, 200, 0);
    rect(1, 1, wX-1, hY-1);
    // print NAME
    fill(255);
    textFont(f, 20);
    textAlign(CENTER);
    text(data.getString(0), wX/2, 20);
    textFont(f, 10);
    // print range
    text(nf(nullr, 1, 1), 30, 25 + 100 );
    text(nf(highr, 1, 1), 30, 25);
    // make graph bars
    color cbar = color(random(50,255),random(50,255),random(50,255));
    for ( i =1; i < tcols; i++) {
      float getfnum = data.getFloat(i);
      float bar = map(getfnum/(-nullr+highr), 0, 1.0, 0, hY-25);
      fill(cbar);
      rect(i*(local_rwidth+2), 25, local_rwidth-1, 100);
      fill(0);
      rect(i*(local_rwidth+2), 25, local_rwidth-1, 100-bar);
      // print vals
      fill(255);
      text(nf(getfnum, 1, 1), i*(local_rwidth+2)+20, 35);
    }

    popMatrix();
  }
}

// ______________UNUSED_____________ OLD ( not Class ) WAY

//void draw() {
  //background(100, 100, 0);
  //graph_rows();
//}

void graph_rows() {
  for ( k =0; k < trows; k++) {
    graph_row(k);
  }
}

void graph_row(int k) {
  text("0", 30, 40 + 100 );
  text(max, 30, 40);
  text(table.getString(k, 0), width/2, 30 + k*130);
  for ( i =1; i < tcols; i++) {
    float getfnum = table.getFloat(k, i);
    float bar = map(getfnum/max, 0.0, 1.0, 0, 100);
    fill(0, 200, 0);
    rect(i*(rwidth+2), 40+ k*130, rwidth-1, 100);
    fill(0);
    rect(i*(rwidth+2), 40+ k*130, rwidth-1, 100-bar);
    fill(255);
    text(nf(getfnum, 1, 1), i*(rwidth+2)+10, 50+ k*130);
    if (dprint) println("k "+k+" i "+i+" val "+getfnum+" bar "+nf(bar, 1, 1));
  }
  dprint = false;
}

```

so actually what is the difference between the class and the old function  
( i leave the OLD CODE at the end )  
in that case?  
the **class** contains a row data copy,  
and is just used once for printing.  
so the **class array** holds a copy of all data  
and is actually UNUSED, and from this view unnecessary.

but was a good exercise for me.

---

<div class="post-metadata">

**Author:** ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)\
**Post date:** [November 11, 2018, 4:51pm UTC](https://discourse.processing.org/t/loading-data-constructor-problem/5179/3 "2018-11-11T16:51:37Z")

</div>

Hi @kll

Thank you for the helping hand.

Best,

Rémy

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [November 11, 2018, 5:09pm UTC](https://discourse.processing.org/t/loading-data-constructor-problem/5179/4 "2018-11-11T17:09:32Z")

</div>

If after working through the intro exercises you are still interested in rendering bar chart data, you may might to look into using the grafica library: [https://jagracar.com/sketches/multiplePlots.php](https://jagracar.com/sketches/multiplePlots.php)
