treemap
in
Programming Questions
•
2 years ago
Hello,
I want to build a simple treemap without hierachies. The treemap must show how many faults a user made in a learning programme. There could be for example five users and they made this faults: 10, 3, 4, 1, 7. So the biggest rectangle is for the user with 10 faults.
Now I found the code of another treemap in the book "Visualizing Data" from Ben Fry. And I asked myself, if I can use this one to solve my problem. The other problem is, I´m a beginner in Processing and I don´t know how to go on.
The code from Ben Fry is like this:
import treemap.*;
Treemap map;
void setup()
{
size (500, 500);
smooth();
strokeWeight(0.25f);
PFont font = createFont("Serif", 13);
textFont(font);
WordMap mapData = new WordMap();
String[] lines = loadStrings("equator.txt");
for(int i=0; i<lines.length; i++)
{
mapData.addWord(lines[i]);
}
mapData.finishAdd();
map = new Treemap(mapData, 0, 0, width, height);
noLoop();
}
void draw()
{
background(255);
map.draw();
}
class WordItem extends SimpleMapItem
{
String word;
WordItem(String word)
{
this.word = word;
}
void draw()
{
fill(255);
rect(x, y, w, h);
fill(0);
if(w>textWidth(word)+6)
{
if(h>textAscent()+6)
{
textAlign(CENTER, CENTER);
text(word, x+w/2, y+h/2);
}
}
}
}
class WordMap extends SimpleMapModel
{
HashMap words;
WordMap()
{
words = new HashMap();
}
void addWord(String word)
{
WordItem item = (WordItem) words.get(word);
if (item == null)
{
item = new WordItem(word);
words.put(word, item);
}
item.incrementSize();
}
void finishAdd()
{
items = new WordItem[words.size()];
words.values().toArray(items);
}
}
The library could be downloaded from http://benfry.com/writing/treemap/library.zip
The data file can be found here http://benfry.com/writing/treemap/equator.txt
Is it possible to use this code to solve my problem? How can I go on?
Could anybody help me? Thanks so much!
I want to build a simple treemap without hierachies. The treemap must show how many faults a user made in a learning programme. There could be for example five users and they made this faults: 10, 3, 4, 1, 7. So the biggest rectangle is for the user with 10 faults.
Now I found the code of another treemap in the book "Visualizing Data" from Ben Fry. And I asked myself, if I can use this one to solve my problem. The other problem is, I´m a beginner in Processing and I don´t know how to go on.
The code from Ben Fry is like this:
import treemap.*;
Treemap map;
void setup()
{
size (500, 500);
smooth();
strokeWeight(0.25f);
PFont font = createFont("Serif", 13);
textFont(font);
WordMap mapData = new WordMap();
String[] lines = loadStrings("equator.txt");
for(int i=0; i<lines.length; i++)
{
mapData.addWord(lines[i]);
}
mapData.finishAdd();
map = new Treemap(mapData, 0, 0, width, height);
noLoop();
}
void draw()
{
background(255);
map.draw();
}
class WordItem extends SimpleMapItem
{
String word;
WordItem(String word)
{
this.word = word;
}
void draw()
{
fill(255);
rect(x, y, w, h);
fill(0);
if(w>textWidth(word)+6)
{
if(h>textAscent()+6)
{
textAlign(CENTER, CENTER);
text(word, x+w/2, y+h/2);
}
}
}
}
class WordMap extends SimpleMapModel
{
HashMap words;
WordMap()
{
words = new HashMap();
}
void addWord(String word)
{
WordItem item = (WordItem) words.get(word);
if (item == null)
{
item = new WordItem(word);
words.put(word, item);
}
item.incrementSize();
}
void finishAdd()
{
items = new WordItem[words.size()];
words.values().toArray(items);
}
}
The library could be downloaded from http://benfry.com/writing/treemap/library.zip
The data file can be found here http://benfry.com/writing/treemap/equator.txt
Is it possible to use this code to solve my problem? How can I go on?
Could anybody help me? Thanks so much!
1