您需要使用前面矩形的高度來計算每個矩形的垂直位置。檢查問題是否有解決方案可能很有用。
// 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