2014-07-04 46 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])); 
      }); 
     }