I have the following data:
var dataset = [[[1, 2], [1, 4]], [[5, 6], [9, 11]], [[5, 2], [15, 20]]];
and another small array of colors:
var color = ['#1f78b4', '#a6cee3', '#33a02c', '#b2df8a', '#b15928', '#ffff99',
'#6a3d9a', '#cab2d6', '#ff7f00', '#fdbf6f', '#e31a1c', '#fb9a99'];
I want to build a point chart in D3, but for every dataset[i], the points should have different colors. After calculating the scales, and drawing the axes, I get to the last part to draw the points of the chart, and this is what I came to:
for (var n = 0; n < dataset.length; n++) {
svg.selectAll("circle")
.data(dataset[n])
.enter()
.append("circle")
.attr("id", n)
.attr("fill", color[n])
.attr("cx", function (d) {
return xScale(d[1]);
})
.attr("cy", function (d) {
return yScale(d[0]);
})
.attr("r", rad);
}
Basically what I want is for each data collection in the "dataset", the points to have different colors (for example the [[1, 2], [1, 4]] to have the #1f78b4 color, for the [[5, 6], [9, 11]] to have the next color.. and so on). But when I run the code, it draws only the first collection from the 'dataset', ignoring the other two... Did someone encounter such a problem? How can it be solved?
gfor each iteration, and select the circles in that. As an aside, while an explicit loop works here, a nested selection is more appropriate.