var data = [
{"Month":"2013/12","Name":"John","EC":344},
{"Month":"2013/12","Name":"Smith","EC":249},
{"Month":"2013/12","Name":"Mary","EC":161},
{"Month":"2013/11","Name":"John","EC":128},
{"Month":"2013/11","Name":"Smith","EC":125},
{"Month":"2013/11","Name":"Mary","EC":117}
];
我有json數據爲3人約翰,史密斯&瑪麗2013/11和2013/12月。我想繪製d3柱狀圖,以便比較2013/11和2013/12每個月的每個人的數據,並用兩個月的不同柱狀顏色代表每個人。兩個月內每個人都會使用同一套顏色。D3比較條形圖
如何根據姓名進行分組,並在同一人的月份(11 & 12)中繪製條形圖(EC)。我只得到一個酒吧,但其他酒吧並沒有提出每個人。我的代碼在jsfiddle http://jsfiddle.net/4gY8H/。你能幫我弄清楚我錯過了什麼嗎?
var data=[{"Name":"Katte","Month":"2013/11","ExamCount":"30"},{"Name":"Borsh","Month":"2013/11","ExamCount":"130"},{"Name":"Broer","Month":"2013/11","ExamCount":"320"},{"Name":"John","Month":"2013/11","ExamCount":"60"},
{"Name":"Katte","Month":"2013/12","ExamCount":"130"},{"Name":"Borsh","Month":"2013/12","ExamCount":"1130"},{"Name":"Broer","Month":"2013/12","ExamCount":"3120"},{"Name":"John","Month":"2013/12","ExamCount":"610"}];
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.ordinal()
.range(["rgba(11,94,215,.8)", "rgba(245, 143, 8, 0.8)", "rgba(0, 167, 188, 0.8)", "rgba(223, 40, 35, 0.8)", "rgba(153, 44, 150, 0.8)", "rgba(124, 189, 42, 0.8)", "rgba(37, 47, 71, 0.8)","rgba(153, 0, 51, 0.8)", "rgba(153, 51, 51, 0.8)", "rgba(92, 0, 92, 0.8)"]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var months = d3.keys(data[0]).filter(function(key) { return key !== "Name"; });
console.log(months);
data.forEach(function(d) {
d.month = months.map(function(name) { return {name: name, value: +d[name]}; });
});
x0.domain(data.map(function(d) { return d.Name; }));
x1.domain(months).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(data, function(d) { return d3.max(d.month, function(d) { return d.value; }); })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Exam Count");
var person = svg.selectAll(".person")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x0(d.Name) + ",0)"; });
person.selectAll("rect")
.data(function(d) { return d.month; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function(d) { return x1(d.name); })
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", function(d) { return color(d.name); });
var legend = svg.selectAll(".legend")
.data(months.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
</script>
喜歡的東西[這](http://nvd3.org/ghpages/multiBar.html)? –
像這樣http://bl.ocks.org/mbostock/882152,但我不知道如何繪製與我有的數據相同。 – Shanthi
我有點修復它。它顯示爲堆疊的酒吧,但不知道如何爲每個月按人名分組的單獨酒吧。但不知道如何爲每個人獲得每個月2個酒吧。我的代碼在這裏更新。 http://jsfiddle.net/bxW9T/ – Shanthi