score:1

Accepted answer

Since you're using D3 v3 you can use the third argument, which is the parent's index, to set your classes:

.attr("class", function(d,i,p){
    return "class_" + p;
});

Check the snippet:

var data = [[1,1,1,1],[1,1,1,1],[1,1,1,1]];
var svg = d3.select("body");
var g = svg.append("g")
var parentElement = g.selectAll(".parent")
    .data(data)
    .enter().append("g")
    .attr("class", "parent");

parentElement.selectAll(".circle")
    .data(function(d,i) { return d; })
    .enter().append("circle")
    .attr("class", function(d,i,p){
  console.log("circle_" + p);
  return "circle_" + p;
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

PS: this will not work on D3 v4 (see here).


Related Query