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 › my first data visualization
Page Index Toggle Pages: 1
my first data visualization (Read 771 times)
my first data visualization
Nov 29th, 2009, 2:40pm
 
Hi there,

I'm teaching myself to program in Processing after a 10-year absence from coding. My current project is supposed to parse a manually downloaded XML file of delicious tags and display them as a simple bar graph.

Here is the code:

Code:
import processing.xml.*;

int plotX1;
int plotY1;
int plotX2;
int plotY2;

int dataMax;
int totalTags;
XMLElement[] tags;

void setup() {
 size(640, 400);
 
 // Initialize variables;
 plotX1 = 0;
 plotX2 = 620;
 plotY1 = 0;
 plotY2 = 390;
 
 //load the XML
 XMLElement xml = new XMLElement(this, "myTags.xml");
 
 //how many elements?
 totalTags = xml.getChildCount();
 
 //load the array
 tags = xml.getChildren();
 
 //find the largest no. of tags used
 dataMax = 0;
 for (int i = 0; i < totalTags; i++) {
   int now = tags[i].getIntAttribute("count");
   String name = tags[i].getStringAttribute("tag");
   println(name);
   if (now > dataMax) {
     dataMax = now;
   }
 }
 println(dataMax);
}
 
void draw() {
 float x = plotX1;
 float y = plotY2;
 float wide = abs(plotX1 - plotX2)/tags.length;
 for (int i = 0; i < tags.length; i++) {
   // Map value from 0 to datamax, to y2 to y1
   float high = map (tags[i].getIntAttribute("count"), 0, dataMax, plotY2, plotY1);
   fill(0);
   // Draw the rectangle.
   rect(x, y, wide, high);
   x = x + wide;
 }
}


The println command tells me that I really am getting the data out of the XML file, but the display is just a blank rectangle. What am I missing?
Re: my first data visualization
Reply #1 - Nov 29th, 2009, 3:17pm
 
its hard to tell without running the code but i just filled in some random values.

try to add background(255); at the beginning of draw
Re: my first data visualization
Reply #2 - Dec 2nd, 2009, 9:15pm
 
I noticed that I didn't call the draw() method in setup; I'm not actually clear if I'm supposed do. draw() gets called every frame refresh, right?

I added the background(255) but with no effect. Hm. I'm remembering there's this thing called "debugging" which I'm going to start reading up on...
Re: my first data visualization
Reply #3 - Dec 2nd, 2009, 10:36pm
 
dont call draw in setup. you dont do that. when i tried it with some random values, i saw some bar graphs after i added a background color to your sketch...
but if you upload and link the code to your xml i will take another look at it with original data.
Page Index Toggle Pages: 1