Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a squarified treemap in javascript

I'm currently trying to implement a treemap algorithm in Javascript. More specifically the algorithm described in Squarified Treemaps. The pseudo code given looks like the following:

procedure squarify(list of real children, list of real row, real w)
begin
    real c = head(children);
    if worst(row, w) <= worst(row++[c], w) then
        squarify(tail(children),row++[c], w)
    else
        layoutrow(row);
        squarify(children,[], width());
    fi
end

however my JavaScript looks like:

var c = children[0];
if (worst(row, w) >= worst(row.concat(c), w)) {
    this.squarify(children.splice(1), row.concat(c), w);
} else {
    layoutrow(row);
    this.squarify(children, [], width());
}

As far as I can tell my code works correctly, but the inequality is the wrong way around. I'm assuming I'm overlooking something in my implementation, or is the inequality the wrong way around in the pseudo code? Thanks

like image 228
Floating Octothorpe Avatar asked Mar 26 '12 22:03

Floating Octothorpe


1 Answers

You want to add c to the current row when doing so will improve the aspect ratio i.e. when

worst(row++[c], w) < worst(row, w)

I've recently committed a piece of code on github that implements the algorithm in TypeScript and includes ready-to-use JavaScript:

https://github.com/nicnguyen/treemap

like image 146
nicopolyptic Avatar answered Sep 19 '22 06:09

nicopolyptic