您需要使用前面矩形的高度来计算每个矩形的垂直位置。检查问题是否有解决方案可能很有用。
// Generate random rectangles, with the vertical position set to zero
var padding = 5;
var num_rectangles = 10;
var rectangles = [];
for (var k = 0; k < num_rectangles; k += 1) {
rectangles.push({
x: 50 * Math.random(),
y: padding,
width: Math.max(50 * Math.random(), 20),
height: Math.max(50 * Math.random(), 20)
});
}
// Update the vertical position of the item j as the sum of the heigths
// of the rectangles 0, ..., j - 1
for (var j = 1; j < num_rectangles; j += 1) {
rectangles[j].y = rectangles[j - 1].y + rectangles[j - 1].height + padding;
}
然后就像往常一样画矩形。我写了一个小例子http://jsfiddle.net/FuepP/2/
你考虑过[treemap layout](https://github.com/mbostock/d3/wiki/Treemap-Layout)吗?通过正确设置尺寸,您应该能够得到相当接近的东西。 – 2013-05-11 11:50:41