Learning to program treemaps--help
in
Programming Questions
•
2 years ago
Hi!
I am a Processing beginner although I have some Java experience. At the moment I'm trying to implement a treemap that will visualise a set of rankings. The information is in a text file in the format: title -rank, title -rank, etc. all on one line. I read this into the program using loadStrings(). I have split the text so that title is in one array position and the corresponding rank is in the very next array position, parsed the rank into an integer and inserted the data into a hashmap. I have been using Ben Fry's simple treemap example from the book Visualizing Data as a basis. I thought I understood it, but now I'm trying to implement my own I keep getting confused In the example, the Hashmap consists of object/string pairs but my hashmap has string/integer pairs. Do I need to make an object class for the title, the title and the rank or neither?? I'm assuming I'll need a class for the title but then I'm not sure what to do about its corresponding rank... should it be a field in the title class? The rank will determine the size of the rectangle to be drawn by the way, with one being the smallest and the lower ranks increasing in rectangle size. Would the title class look like this(rank doesn't do anything cause I don't know what to with it yet):
Thanks guys!
I am a Processing beginner although I have some Java experience. At the moment I'm trying to implement a treemap that will visualise a set of rankings. The information is in a text file in the format: title -rank, title -rank, etc. all on one line. I read this into the program using loadStrings(). I have split the text so that title is in one array position and the corresponding rank is in the very next array position, parsed the rank into an integer and inserted the data into a hashmap. I have been using Ben Fry's simple treemap example from the book Visualizing Data as a basis. I thought I understood it, but now I'm trying to implement my own I keep getting confused In the example, the Hashmap consists of object/string pairs but my hashmap has string/integer pairs. Do I need to make an object class for the title, the title and the rank or neither?? I'm assuming I'll need a class for the title but then I'm not sure what to do about its corresponding rank... should it be a field in the title class? The rank will determine the size of the rectangle to be drawn by the way, with one being the smallest and the lower ranks increasing in rectangle size. Would the title class look like this(rank doesn't do anything cause I don't know what to with it yet):
- class TitleItem extends SimpleMapItem {
String title;
int rank;
TitleItem(String title, int rank){
this.title = title;
this.rank = rank;
}
void draw() {
fill(255);
rect(x, y, w, h);
fill(0);
//the text is only drawn if the w and h of the text are
//smaller than the w and h of the rect
if(w > textWidth(title) + 6) {
if (h > textAscent() + 6) {// textAscent() used to determine height of font
//above baseline
textAlign(CENTER, CENTER);
text(title, x + w/2, y + h/2);
}
}
}
Thanks guys!
1