我目前正在尝试在Javascript中实现树图算法。更具体地说,Squarified Treemaps中描述的算法。给出的伪代码如下所示:
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
然而我的JavaScript看起来像:
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());
}
据我所知,我的代码工作正常,但不平等是错误的方法。我假设我在实现中忽略了某些东西,或者伪代码中的不等式是不正确的?感谢
答案 0 :(得分:4)
您希望将c
添加到当前row
,这样做会改善宽高比,即何时
worst(row++[c], w) < worst(row, w)
我最近在github上提交了一段代码,它在TypeScript中实现了算法并包含了现成的JavaScript:
答案 1 :(得分:0)
如果您只对布局算法感兴趣,请查看我的squarify
npm包。它只返回布局数据,让您可以随意渲染结果。