I am a newbie and trying to play around with my data. I first visualize these circles as following:
circle = svg.selectAll('circle')
.data(dataFile)
.enter()
.append("circle")
.attr("stroke", "black")
.attr("id", function(d,i){return "id_" + i.toString();})
.attr("class", function(d){return "Color_" + d.Cyl;})
.attr("fill", function(d) {return color(d.Cyl);})
.attr("cx", function(d) { return Scale.xScale(d.Weight); })
.attr("cy", function(d) { return Scale.yScale(d.DealerCost); })
.attr("r", r)
.call(drag);
I then use nest() function to group my data.
var data = d3.nest()
.key(function(d) { return d.Cyl;})
.rollup(function(d) {
return d3.sum(d, function(g) {return Number(g.value); });
}).entries(csv);
Finally I try to translate circles based on some logics and this works perfectly.
for(j=0; j<data.length;j++)
{
svg
.selectAll(".Color_" + data[j].key)
.transition()
.delay(function(d,i) { return 100; })
.duration(1000)
.ease("linear")
.attr("cx", function(d,i) { return Scale.xScale(data[j].key})
.attr("cy", function(d,i) { return height - (i/val);})
}
However, some experts told me if I use For loop with d3.selection there is a high possibility that I am doing something wrong. So I tried to convert the last part of my code to something like the code below but it does not work. Any idea?
svg
.selectAll(function(d){return ".Color_" + d.key;})
.transition()
.delay(function(d,i) { return 100; })
.duration(1000)
.ease("linear")
.attr("cx", function(d,i) { return Scale.xScale(d.key})
.attr("cy", function(d,i) { return height - (i/val);})
}