你想要做的是堆积条形图的基础:http://bl.ocks.org/mbostock/3886208
不过,也有你需要考虑几件事情。 Y和X轴必须是成比例的,所以你必须仔细考虑:
并为每个方格选择一个高度,宽度和“值” - 每个方格。
我做了一个示范小提琴这里:http://jsfiddle.net/sa5RK/
它假定几件事情(为简单!):
- 各记号是一个价值
高度&宽度盒子设置为
var boxheight = 6;
高度/ wi DTH可以基于框和数据的大小是动态值
var margin = {top: 20, right: 20, bottom: 80, left: 40},
width = boxheight * data.length;
var max = d3.max(data, function(d){return d.a + d.b + d.c});
var height = max * boxheight;
我希望这是足以让你开始!
另一个可以帮助你的关键是了解每个数据如何加入选择可以设置为一个变量,然后在里面选择它将允许你访问外部数据。例如。下面,每个数据组(原始数组中的值) - >具有类型(A,B或C) - >具有矩形
var groups = svg.selectAll(".group")
.data(data)
.enter().append("g")
.attr("transform", function(d,i){return "translate(" + x(i) + ", 0)"})
.attr("class", "group")
var types = groups.selectAll(".type")
.data(function(d){return d.offsets})
.enter().append("g")
.attr("transform", function(d){ return translate(0,y(d.y1))})
.attr("class", "type")
.attr("fill", function(d){return color(d.type)})
types.selectAll("rect")
.data(function(d){return d3.range(0,d.value)})
.enter().append("rect")
.attr("height", boxheight-0.5)
.attr("width", boxheight-0.5)
.attr("y", function(d){ return boxheight * d })
你会更好使用帆布比任SVG或HTML。 – 2013-04-07 07:39:10