1

全面披露:我对编程并不陌生,但我对 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 时一无所获。

4

2 回答 2

1

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

svg.selectAll("g.g")
   .transition().duration(750)
   .delay(delay)
   .attr("transform", function(d) { return "translate(" + x2(d.State) + ",0)"; });
于 2013-05-08T10:49:12.220 回答
0

我尝试使用堆叠条形图。要对堆积图进行排序,请查找 堆积条形图

       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);
      } 
于 2015-03-12T10:40:10.107 回答