Mike 这是制作图表的快速方法。
创建列并附加标题
// Group Columns
var columns = groupColumn.selectAll("g")
.data(consumption[0].fruits)
.enter()
.append("g")
.attr("class", function(d,i) { return "column - " + i });
// Append year
columns.append("text")
.attr("transform", function(d,i) { return "translate(" + i*(width/2) + "," + height/2 + ")"; })
.text(function(d) {return d.year });
创建行和追加字段
// Append row
var groupRow = columns.append("g")
.attr("class", "fruits")
.attr("transform", function(d,i) { return "translate(" + i*(width/2) + "," + height/2 + ")"; })
// Append fields of row
var rows = groupRow.selectAll("circle")
.data(function(d) {return d3.entries(d).filter(function(d) { return d.key !== "year"; })})
.enter()
.append("circle")
.attr("cx",function(d,i){ return (60 * i) + 30 } )
.attr("cy",50)
.attr("r",25)
.attr("fill", function(d){
if(d.key === "apples"){ return "red" }
else if (d.key === "oranges"){ return "orange" }
else if (d.key === "pears"){ return "green" }
})
将文本附加到字段
var text = groupRow.selectAll("text")
.data(function(d) {return d3.entries(d).filter(function(d) { return d.key !== "year"; })})
.enter()
.append("text")
.attr("dx", function(d,i){ return (60 * i) + 20 })
.attr("dy", 55)
.attr("fill", "white")
.text(function(d) {return d.value});
查看代码http://jsfiddle.net/jmeza/37PYJ/3/
例子不完整,我给你第一步……