D3比较条形图

时间:2014-01-07 06:31:42

标签: json d3.js

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数据。 Mary为2013/11和2013/12。我想绘制d3条形图来比较每个人在2013/11和2013/12月的数据,通过代表每个人在两个月中使用不同的酒吧颜色。两个月都会为每个人使用相同的颜色。

如何按名称分组并为同一个人的月份(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>

1 个答案:

答案 0 :(得分:0)

问题是你基本上复制了this example,但数据格式不同。 例如,你这样做:

var months = d3.keys(data[0]).filter(function(key) { return key !== "Name"; });

您的数据会返回["Month", "ExamCount"]而不是月份 在您的情况下,您可以使用:

var months = d3.set(data.map(function(line) { return line.Month; })).values();

返回["2013/11", "2013/12"]

还有其他例子。如果你只需要2个,为什么你有8种颜色? 整个数据绑定部分(当你为每个数据行创建一个矩形时)对于你的数据结构是错误的,你应该尝试从头开始编写它,而不是试图调整示例中的那个(它有CSV格式的数据,非常不同。)

这是一个有效的JSFiddle:http://jsfiddle.net/bxW9T/17/