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.
IndexProgramming Questions & HelpSyntax Questions › newb- Milk, Tea, Coffee in Ch4
Page Index Toggle Pages: 1
newb- Milk, Tea, Coffee in Ch4 (Read 626 times)
newb- Milk, Tea, Coffee in Ch4
Aug 5th, 2008, 9:20pm
 
Hi I'm going through Ch4 of Visualizing Data and on pg.58 a drawDataPoints method is introduced to plot the values of the Milk column. My question is why does drawDataPoints(0) plot the values for the Milk column rather than the Year column? Isn't column 0 the Year column?
Re: newb- Milk, Tea, Coffee in Ch4
Reply #1 - Aug 6th, 2008, 8:53pm
 
FloatTable data;
float dataMin;
float dataMax;

float plotX1;
float plotY1;
float plotX2;
float plotY2;

int yearMin;
int yearMax;
int[] years;




void setup() {
 size(720, 405);
 
 data = new FloatTable("milk-tea-coffee.tsv");
 
 years = int(data.getRowNames());
 yearMin = years[0];
 yearMax = years[years.length - 1];
 
 dataMin = 0;
 dataMax = data.getTableMax();

 plotX1 = 50;
 plotX2 = width - plotX1;
 plotY1 = 60;
 plotY2 = height - plotY1;
 
 smooth();
}




void draw() {
 background(224);
 fill(255);
 noStroke();
 rectMode(CORNERS);
 rect(plotX1, plotY1, plotX2, plotY2);
 
 strokeWeight(5);
 stroke(#5679C1);
 drawDataPoints(0);
}




void drawDataPoints(int col) {
 int rowCount = data.getRowCount();
 for (int row = 0; row < rowCount; row++) {
   if (data.isValid(row, col)) {
     float value = data.getFloat(row, col);
     float x = map(years[row], yearMin, yearMax, plotX1, plotX2);
     float y = map(value, dataMin, dataMax, plotY2, plotY1);
     point(x,y);
   }
 }
}

http://benfry.com/writing/series/FloatTable.pde

http://benfry.com/writing/series/milk-tea-coffee.tsv
 
Hello, I've pasted the code in this step and links to the files it uses. I just can't figure out why the Milk column is column 0 instead of the Year column.
Re: newb- Milk, Tea, Coffee in Ch4
Reply #2 - Aug 8th, 2008, 4:37pm
 
In that chapter (and in FloatTable.pde), column 0 is the name column, which is why there's a separate getRowNames() function. The rest of the data is floats, so the getFloat(row, col) methods refer to getting the actual data values, not the entries of the table cells.

The data starts out as text Strings, and is converted to floats. This conversion is time consuming with lots of data, so in FloatTable.pde, that conversion happens once (when the data is first loaded) because all rows and columns (except for the "name" column) are known to be floats.

Example data files later in the book (that use Table.pde instead of FloatTable.pde) might contain Strings, ints, floats, anything.. So they're converted when the getFloat(), getInt(), etc. methods are run. In those examples, there is no "name" column. This is less efficient but the best solution without getting into a more complicated description of how to efficiently cache that data once converted.

And from the length of this explanation, you might understand why I didn't get into the details of this in chapter four of the book. Wink
Re: newb- Milk, Tea, Coffee in Ch4
Reply #3 - Aug 14th, 2008, 1:57am
 
It finally sank in. Thank you very much! I think I'll go through the "Processing" book before continuing with "Visualizing Data".
Page Index Toggle Pages: 1