将 d3 数据更新到子节点

updating d3 data to the child nodes

我正在创建 d3 条形图。我有一个 json 值数组

      bars = this.svg.selectAll("g")
                    .data(data)
                    .enter()
                      .append('g')
                      .attr('transform',function(d, i) {
                        return "translate(0," + i * 30 + ")";
                        });

          bars.append('rect')
            .attr('height',20)
            .attr('fill',this.color)
            .attr('width',0)
            .attr("x",100)
            .transition()
              .attr('width',function(d){
                console.log('rect',d);
                return widthScale(d.value)
                })
              .duration(1000)

一段时间后我得到一个新的值列表,所以我更新数组并传递它。

             this.svg.selectAll("g")
                    .data(data)
                    .attr('asd',function(d){
                      console.log("My Data",d);
                      return 1;
                    });

在我的控制台日志中,我得到的数据非常好,但是当我尝试从矩形访问时

   bars.selectAll('rect')
            .transition()
              .attr('fill',this.color)
              .attr('width',function(d){
                  console.log(d.value);
                  return widthScale(d.value)
                })
              .duration(1000)

我正在接受旧价值观。我做错了什么?

您将数据绑定到 g 元素,然后选择 rect 元素。如果要传播数据,请使用 .select("rect")

this.svg.selectAll("g").data(data).select("rect")