2014-07-04 42 views
0

我有一个包含一组圆个从对象数组d绘制的SVG图形:通过迭代已经绘制的对象来更新d3js对象的集合?

svg.selectAll(".circ").data(d) 
      .enter().append("circle") 
      .attr("cy", function (d) { 
       return d.y 
      }) 
      .attr("cx", function (d) { 
       return d.x 
      }) 
      .attr("r", elemSize/2) 
      .style("fill", function (d) { 
       return d.color 
      }).attr("class", function (d, i) { 
       return "containerCell circ"; 
      }) 

我还阵列称为状态包含新的填充颜色,即具有与inital相同的尺寸/指数d

在d3js什么表情,我可以用走了过来已经绘制的元素和更新从一个存储在状态其填充颜色?也许我需要对已有的对象使用一些迭代方法?

回答

0

找到答案我自己。

// select all circles of interest 
var select = svg.selectAll(".circ"); 
     if (!select.empty()) { // if selection got something 
      select.each(function (d, i) { 
       // d is data, i is index and we can modify elements of SVG as follows: 
       d3.select(this).style("fill", statusColor(states[i])); 
      }); 
     }