d3js - 可排序组条形图

时间:2013-05-08 08:53:32

标签: javascript d3.js jquery-ui-sortable bar-chart

完全披露:我不是编程的新手,但我是d3和javascript的新手。

我正在尝试合并Grouped Bar Chart ExampleSortable Bar Chart Example。我共有51组3个变量。以下是我可以用来运行代码的截断形式的数据集:

State,Response,Predicted,Difference
1,0.0526,0.0983,0.0456
2,0.1161,0.1093,0.0068
5,0.0967,0.1035,0.0067
4,0.0998,0.0942,0.0055
6,0.0888,0.0957,0.0069

我希望能够通过选中一个框来按Response变量对数据进行排序。现在我可以让x轴标签相应地移动,但我不能让它们与它们一起移动。为了达到这一点,我根据我的数据重命名了change()函数中的变量。我尝试将transition.selectAll(".state")函数保存为state2,然后使用state2.selectAll(".rect")修改矩形的x坐标,但我意识到这不会让我无处可去。

这是我现在的代码(主要是从上面链接的示例中复制而来)。相关功能结束了。

var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 1000 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom,
    code = "";

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.category10();

var xAxis = d3.svg.axis()
    .scale(x0)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickFormat(d3.format(".0%"));

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 + ")");

d3.csv("data.csv", function(error, data) {
  var ageNames = d3.keys(data[0]).filter(function(key) { return key !== "State"; });

  data.forEach(function(d) {
    d.ages = ageNames.map(function(name) { return {name: name, value: +d[name]}; });
  });

  x0.domain(data.map(function(d) { return d.State; }));
  x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]);
  y.domain([0, d3.max(data, function(d) { return d3.max(d.ages, 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("Prevalence");

  var state = svg.selectAll(".state")
      .data(data)
    .enter().append("g")
      .attr("class", "g")
      .attr("transform", function(d) { return "translate(" + x0(d.State) + ",0)"; });

  state.selectAll("rect")
      .data(function(d) { return d.ages; })
    .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); });

  d3.select("input").on("change", change);

  var sortTimeout = setTimeout(function() {
    d3.select("input").property("checked", true).each(change);
  }, 2000);

  var legend = svg.selectAll(".legend")
      .data(ageNames.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; });

  function change() {
    clearTimeout(sortTimeout);

    // Copy-on-write since tweens are evaluated after a delay.
    var x2 = x0.domain(data.sort(this.checked
        ? function(a, b) { return b.Response - a.Response; }
        : function(a, b) { return d3.ascending(a.State, b.State); })
        .map(function(d) { return d.State; }))
        .copy();

    var transition = svg.transition().duration(750),
        delay = function(d, i) { return i * 50; };

    var state2 = transition.selectAll(".state")
        .delay(delay)
        .attr("x", function(d) { return x2(d.State); });

    transition.select(".x.axis")
        .call(xAxis)
      .selectAll("g")
        .delay(delay);
  }
})

非常感谢任何帮助。到目前为止,我没有找到任何搜索SO和Google的内容。

2 个答案:

答案 0 :(得分:1)

我假设你想在排序时保持分组。您的群组包含在g元素中,因此您需要做的就是调整群组的坐标。也就是说,移动组的代码看起来像

svg.selectAll("g.g")
   .transition().duration(750)
   .delay(delay)
   .attr("transform", function(d) { return "translate(" + x2(d.State) + ",0)"; });

答案 1 :(得分:0)

我尝试使用堆积条形图。要对堆积图表进行排序,请找到  Stacked Bar Chart

       function change() {
        // Copy-on-write since tweens are evaluated after a delay.
        var x0 = x.domain(data.sort(this.checked
            ? function(a, b) { return b.noncomplete - a.noncomplete; }
            : function(a, b) { return d3.ascending(a.moduleName, b.moduleName); })
            .map(function(d) { return d.moduleName; }))
            .copy();

        var transition = svg.transition().duration(750),
            delay = function(d, i) { return i * 60; };

        transition.selectAll(".moduleName")
            .delay(delay)
            .attr("transform",function(d, i) { return "translate(" + (x0(d.moduleName)) + ",0)"; } ); 

     transition.select(".x.axis")
            .call(xAxis)
          .selectAll("g")
            .delay(delay);
      }