I’m using this tutorial to learn some basic d3.
I’m at the “binding data” section, which is so far proving quite confusing.
var dataset = [],
i = 0;
for(i=0; i<5; i++){
dataset.push(Math.round(Math.random()*100));
}
alert("Data: " + dataset)
var sampleSVG = d3.select("#viz")
.append("svg:svg")
.attr("width", 400)
.attr("height", 75);
sampleSVG.selectAll("rect")
.data(dataset)
.enter().append("svg:rect")
.style("stroke", "gray")
.style("fill", "white")
.attr("height", 40)
.attr("width", 75)
.attr("x", function(d, i){return i*80})
.attr("y", 20);
My questions are:
- We have created a dataset of 5 random numbers. Why are these not reflected in the widths of the rectangles?
.append("svg:svg")doesn’t refer to any rectangles, so how can weselectAll("rect")afterwards if they don’t even exist?- In the anonymous function, what does
drefer to? - In the anonymous function, what does
irefer to? What is it multiplying by 80? - Does it automatically loop through all points in the dataset? In the final code chunk, there doesn’t seem to be any iteration, so I’m guessing it just does this for every data element?
So confused!
1 Answer