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 & HelpPrograms › help a beginner draw data from tsv
Page Index Toggle Pages: 1
help a beginner draw data from tsv (Read 943 times)
help a beginner draw data from tsv
Sep 4th, 2008, 10:11pm
 
I am tring to teach myself how to create my own data visulaization and I started from Ben Fry's book and its examples. I had it all working and I tihnk i understand what's going on but I must be stumbling over a minor syntax issue. I have a table of values amongst which are strings of text that describe the entities. I have a for each row sort of loop which draws each entity but upon trying to make a conditional search whcih returns a different color value, I am not getting the right result. I added a print line function which reinforces the fact that I am referencing the right column ...still it's not working.

go here for my applet http://friedmanstudios.ca/chimera_obscura/processing/animalValues/applet/
Re: help a beginner draw data from tsv
Reply #1 - Sep 5th, 2008, 11:14am
 
Code:
  if (diet == "carnivore"){
[...]
else if (diet == "herbivore") {

Common mistake in Java programming, that even professional programmers do from time to time!
It should be:
Code:
  if (diet.equals("carnivore")){
[...]
else if (diet.equals("herbivore")) {
Re: help a beginner draw data from tsv
Reply #2 - Sep 5th, 2008, 11:27am
 
how is it not working? what are you expecting to see and what are you seeing? we don't have the animal data so we don't know what we should be seeing.

make the eclipses bigger and more spread out whilst debugging it, make them easier to see.

you also don't need to reload the font every frame -

add "PFont font;" under int rowCount; to make it global.

change the PFont font = loadFont("Swiss721BT-Light-12.vlw"); in setup to be just
font = loadFont("Swiss721BT-Light-12.vlw");
to load the font into the new global variable.

and then remove the LoadFont line in drawData.
Re: help a beginner draw data from tsv
Reply #3 - Sep 5th, 2008, 11:29am
 
(oddly, PhiLho, i tried that just with

String diet = "carnivore";
if (diet == "carnivore") {
println "carnivore";
}

and it printed. so i figured == was overloaded for strings in java)

[edited name. oops]
Re: help a beginner draw data from tsv
Reply #4 - Sep 5th, 2008, 11:32am
 
"in processing", not "in java"
Re: help a beginner draw data from tsv
Reply #5 - Sep 5th, 2008, 5:29pm
 
@koogs:
I can be wrong, but I think Java (1.5?) optimize strings in some cases: if it finds the same exact literal string in several places, it just put them in the same object. So you still do a comparison of objects, but it happens to be the same, hence it works!
It is roughly as it you declared a final static string C = "carnivore"; and did the comparisons with this object.
Now, if I rewrite your example as:

 String diet = "carn";
 diet += "ivore";
 if (diet == "carnivore") {
   println("carnivore");
 }

it no longer works. Note that diet = "carn" + "ivore"; is still optimized by Java!
Re: help a beginner draw data from tsv
Reply #6 - Sep 5th, 2008, 6:08pm
 
thanks for all the replies...it's great. one quick question - should I be going with the if (diet.equals("carnivore")) or is there some problem with strings being overloaded...? what does that mean anyways?

as for my table - I don't have it in front of me - I constructed it myself and it lists 70 or so species of wildlife each one to a row. the column headings are about 16 and they range from length and weight (float values) to common and scientific names and this like diet type (string values). The diet column entries are just that - "carnivore", "herbivore", saprovore" "omnivore"  etc etc...
Re: help a beginner draw data from tsv
Reply #7 - Sep 5th, 2008, 6:57pm
 
diet.equals("carnivore") is guaranteed to work, the other way works sometimes.

(thanks PhiLho. that is very odd.)
Re: help a beginner draw data from tsv
Reply #8 - Sep 5th, 2008, 7:46pm
 
yep it worked! thanks.

how does one learn about these sorts of things...i looked through the Processing book and Visualizing data and I couldnt find any clues...no doubt I will run into more situations like these.

also has anyone seen anything like Flare or Prefuse for processing - http://flare.prefuse.org/ - and by this I guess I mean a toolkit or library (I just started programming so I don't know its official term) that makes it a little easier to create visualizations?
Re: help a beginner draw data from tsv
Reply #9 - Sep 5th, 2008, 7:50pm
 
oh and thanks for the font tip. I had trouble with the placement in the code as per the example in VD and so I just started going trigger happy with it. it seems to be running faster now...
Re: help a beginner draw data from tsv
Reply #10 - Sep 6th, 2008, 9:50am
 
Actually, I could test your applet locally, I just got the TSV file at http://friedmanstudios.ca/chimera_obscura/processing/animalValues/animalValues-06.txt Smiley

And you are right, the == (equality) page is even a bit misleading, since it states == can be used to compare String types.
If fry or REAS reads this, I suggest to clarify this point in this page.
Re: help a beginner draw data from tsv
Reply #11 - Sep 6th, 2008, 9:08pm
 
this probably demands a new post -

I am trying to move the graph into a force directed scheme - I got it working but there are some things which aren't immediately clear to me. In my original TSV file i have the various attributes as I mentioned before and I am not sure how to pass this info onto the Node constructor...I tried adding "String diet" to almost everywhere I found a "String commong" so that back in my Node class I could specify how to "dress" nodes conditionally based on the diet value...needless to say it didn't work. any ideas?

Node addNode (String common) {
 Node n = new Node(common);
 if (nodeCount == nodes.length){
   nodes = (Node[]) expand (nodes);
 }
 nodeTable.put(common, n);
 nodes[nodeCount++] = n;
 return n;
}

and the link to the new sketch:
www.friedmanstudios.ca/chimera_obscura/processing/animalValuesFDL/applet
Re: help a beginner draw data from tsv
Reply #12 - Sep 7th, 2008, 9:52am
 
Not sure what you mean by "it didn't work", the applet seems to run fine.
And as I wrote elsewhere, you should do this in two steps: load the data in memory (it isn't so big), then process it, knowing its exact size (no need to expand, etc.).
Not sure if I answered your request, though.
Re: help a beginner draw data from tsv
Reply #13 - Sep 7th, 2008, 10:31am
 
yes, it "works" but all it is doing is taking the String which is taken from place in the original TSV. I want to transfer more data - other attributes that will have an impact on each nodes rendering. I guess I'm just stumbling over the fact that I am borrowing others' code without compeltely understanding what's going on. The Node constructor only originally had one argument - for a label...say I want a few attributes to be sent from the original TSV over to the array of nodes...what's the best method?
Page Index Toggle Pages: 1