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
Project (Read 905 times)
Project
Feb 15th, 2010, 7:50pm
 
Hello,
I am trying to figure out what could be the problem with my code. I am sort of following Ben Fry's example of locations on a map but with countries instead of his example of U.S. states. I have two classes: Data_Visualization and Loader. I have also added all the files I need for this to run. I know I have a ton of errors but I was hoping maybe I could get some help with this. Thanks.

Data_Visualization class:

PImage mapImage;
int rowCount;

Loader dataTable, locationTable;
float dataMin = MAX_FLOAT;
float dataMax = MIN_FLOAT;

void setup() {
 size(720, 360);
 mapImage = loadImage("world_outline_map_720.jpg");
 locationTable = new Loader("locations.csv");
 rowCount = locationTable.getRowCount();
 //Read the data table
 dataTable = new Loader("values.csv");

 //Finding the min and max values
 for(int row = 0; row < rowCount; row++){
   float value = dataTable.getFloat(row, 1);
   if(value > dataMax){
     dataMax = value;
   }
   if(value < dataMin){
     dataMin = value;
   }
 }

}
void draw(){
 background(255);
 image(mapImage,0,0);

 //Drawing attributes for the ellipses
 smooth();
 fill(192,0,0); //red
 noStroke();

 //Draw a data point for each location

 for(int row = 0; row < rowCount; row++){
   String abbrev = dataTable.getRowName(row);
   float x = locationTable.getFloat(abbrev, 1);
   float y = locationTable.getFloat(abbrev, 2);
   drawData(x, y, abbrev);
 }


 //Map the size of ellipse to the data value
 void drawData(float x, float y, String abbrev){
   float value = dataTable.getFloat(abbrev, 1);
   float mapped = map(value, dataMin, dataMax, 2, 40);
   ellipse(x, y, mapped, mapped); //draw the ellipse
 }

 //Loop through the rows of the location file and draw the points.
 for(int row = 0; row < rowCount; row++){
   float x = locationTable.getFloat(row, 1);
   float y = locationTable.getFloat(row, 2);
   ellipse(x,y,9,9);
 }
}


Loader class:

class Loader {
 String[] lines;
 int rowIndex;

 Loader(String filename){
   lines = loadStrings("locations.csv");
   // lines = loadStrings("values.csv");
   println("there are " + lines.length + " lines");
 }

 int getRowCount(){
   return lines.length;
 }



 float getFloat(int rowIndex, int column){
   String x = lines[rowIndex];
   String[] y = split(x,',');
   return(Float.parseFloat(y[column]));
 }

 public float getFloat(int rowName, int column){
  String a = lines[rowName];
  String[] b = split(a,',');
  return(Float.parseFloat(b[column]));
  //return parseFloat(rowName, column);
  }

 String getRowName(int row){
   return;
 }
}

I got my project to run with the ellipses matching my locations file to the countries, but now I can't get the project to run when I try to set Min and Max values for the ellipses. For example I have China as one of my countries so the map should show that ellipse as the largest. (In my values.csv file, China has the largest value).

Re: Project
Reply #1 - Feb 15th, 2010, 8:23pm
 
Without your files to work with, it's hard to debug.
What error are you getting?
On what line?

I would also suggest that, assuming there is one, you set the min and max data to the first value in the array before looping through the array - that way you don't have to muck about with MIN_FLOAT and MAX_FLOAT.
Re: Project
Reply #2 - Feb 15th, 2010, 8:37pm
 
The error I am getting right now is "unexpected token: void"
This is on the line: void drawData(float x, float y, String abbrev){

Thanks for the other suggestion.
Re: Project
Reply #3 - Feb 15th, 2010, 10:43pm
 
Quote:
void draw(){
  background(255);
  image(mapImage,0,0);

  //Drawing attributes for the ellipses
  smooth();
  fill(192,0,0); //red
  noStroke();

  //Draw a data point for each location

  for(int row = 0; row < rowCount; row++){
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev); 
  }


  //Map the size of ellipse to the data value
  void drawData(float x, float y, String abbrev){
    float value = dataTable.getFloat(abbrev, 1);
    float mapped = map(value, dataMin, dataMax, 2, 40);
    ellipse(x, y, mapped, mapped); //draw the ellipse
  }

  //Loop through the rows of the location file and draw the points.
  for(int row = 0; row < rowCount; row++){
    float x = locationTable.getFloat(row, 1);
    float y = locationTable.getFloat(row, 2);
    ellipse(x,y,9,9);
  }
}



This is your draw function. The whole thing is your draw function.
It looks like you're trying to define a new function, drawData(), in the middle of this function. This is a bad place to define a new function.
If we move what looks like the new function to outside of draw(), we get this:
Re: Project
Reply #4 - Feb 15th, 2010, 10:45pm
 
Quote:
void draw(){
  background(255);
  image(mapImage,0,0);

  //Drawing attributes for the ellipses
  smooth();
  fill(192,0,0); //red
  noStroke();

  //Draw a data point for each location

  for(int row = 0; row < rowCount; row++){
    String abbrev = dataTable.getRowName(row);
    float x = locationTable.getFloat(abbrev, 1);
    float y = locationTable.getFloat(abbrev, 2);
    drawData(x, y, abbrev); 
  }

  //Loop through the rows of the location file and draw the points.
  for(int row = 0; row < rowCount; row++){
    float x = locationTable.getFloat(row, 1);
    float y = locationTable.getFloat(row, 2);
    ellipse(x,y,9,9);
  }
}

//Map the size of ellipse to the data value
void drawData(float x, float y, String abbrev){
  float value = dataTable.getFloat(abbrev, 1);
  float mapped = map(value, dataMin, dataMax, 2, 40);
  ellipse(x, y, mapped, mapped); //draw the ellipse
}



This is now two separate functions.
As I said before, I don't have your files so I can't run this myself.
After you make this change, are there still problems?

"Ctrl + t" is your friend.
It'll neaten your code right up and let you spot problems like this.
Re: Project
Reply #5 - Feb 16th, 2010, 12:33am
 
TfGuy44 wrote on Feb 15th, 2010, 10:45pm:
"Ctrl + t" is your friend.
It'll neaten your code right up and let you spot problems like this.


Auto format is probably useful when you're starting out (if nothing else you'll hopefully see what neat code should look like), but don't rely too much on 'so-called' friends.  Much better to get into the habit of writing neat code from the start: there are some friends you can't always rely on (example).
Re: Project
Reply #6 - Feb 16th, 2010, 1:32pm
 
Unfortunately I still get errors. It now says it can't find a class or type named "Loader" and highlights the line: Loader dataTable, locationTable;
Re: Project
Reply #7 - Feb 16th, 2010, 1:42pm
 
Okay nevermind about the last post.

The error now is: The function getFloat(int,int) does not exist.

Highlights the line: float value = dataTable.getFloat(row, 1);

I have had this problem before. I can't figure out a method(which would be in my Loader class) to get the min and max values to show up. If they did the ellipses would be equal to the values I gave them depending on the country. I hope I'm explaining this well. It may be hard to understand what I mean sometimes.
Page Index Toggle Pages: 1