简体   繁体   English

将d3数据更新到子节点

[英]updating d3 data to the child nodes

I am creating a d3 barchart. 我正在创建d3条形图。 I have an array of json values 我有一个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)

After some time i get a new list of values so i update the array and pass it. 一段时间后,我得到了一个新的值列表,因此我更新了数组并通过了它。

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

Here in my console log i am getting data perfectly fine but when i try to access from the rectangles 在我的控制台日志中,我得到的数据很好,但是当我尝试从矩形访问时

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

I am getting old values. 我越来越老了。 What am I doing wrong? 我究竟做错了什么?

You're binding the data to the g elements and selecting the rect elements afterwards. 您将数据绑定到g元素,然后选择rect元素。 If you want to propagate the data, use .select("rect") : 如果要传播数据,请使用.select("rect")

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM