But I spent a lot of time thrashing about yesterday, due mostly to two silly errors.
Put the script in the body
First, I’d tried to make a truly simple example, making just an SVG with a little rectangle.
<title>Put script in body</title>
var svg = d3.select("body").append("svg")
.attr("height", h).attr("width", h);
svg.append("rect").attr("x", 0).attr("y", 0)
But this gives nothing. You need to move the script from the
head to the
body, as it is here. Then it works.
I don’t really understand this. Perhaps I should go back to my reading.
[Update: I think I’ve figured this out. If you put the script in the
head, the code gets run before the
body exists, and so there’s no
body in which to append the SVG. Conclusion: Put the link to your script at the very bottom of the html file.]
In D3, you bind data to a bunch of objects, and attributes of the objects can be controlled by features of the data. I write code sort of like this:
.attr("x", (d) -> start[d])
.attr("width", (d) -> end[d] - start[d])
.attr("height", (d) -> hInner)
.attr("fill", (d) -> color[d])
My most common mistake so far: I forget the
.enter() part. You don’t get a error message, but the objects don’t get created.