Combining Parent and Nested Data with d3.js

前端 未结 3 1892
臣服心动
臣服心动 2020-12-28 18:44

I have a data structure like this (assume that the data structure is non-negotiable):

data = {
    segments : [
        {x : 20, size : 10,          


        
3条回答
  •  星月不相逢
    2020-12-28 19:27

    You can use closures

    var nested = vis
      .selectAll('g')
      .data(data.segments);
    
    
    nested.enter()
      .append('g')
      .each(function(segment, i) {
        var colors = d3.select(this)
          .selectAll('rect')
          .data(segment.colors);
    
        colors.enter()
          .append('rect')
          .attr('x', function(color, j) { return pos(segment, j); })
          // OR: .attr('x', function(color, j) { return segment.x + (j * segment.size); })
          .attr('width', function(color, j) { return size(segment); })
          .attr('fill', String);
      });
    

提交回复
热议问题