I’ve just started playing with D3 and Observable, and am working on my first chart. I’m creating a timeline that shows the overlaps between various writers’ lifetimes.
I’ve managed to muddle my way through what I considered the hardest bits, but I’ve hit a wall when trying to add labels to the chart; the code I’m using only adds labels to about the bottom 2/3 of the bars.
Here’s the code I’m using:
// add labels
svg.selectAll("text")
.data(data)
.enter()
.append("text")
.text(function(d) {return d.name})
.attr("fill", "#ffffff")
.attr("x", function(d, i) {return x(d.dob) + (dimensions.barPadding * 0.66667)})
.attr("y", function(d, i) {return ((margin.top * 2.175) + (i * (dimensions.barHeight + dimensions.barPadding)))});
Using the developed console in Firefox, the first <text>
element is the one for Bertrand Russell, so it’s not (as far as I can see) a case of the others being positioned incorrectly… they just don’t exist.
Given it’s based on the code I used to create the bars (which works just fine!), I’m not sure what’s going on here, so would really appreciate any pointers.
(Also, if there’s an easier/better way to create data labels, please feel free to call it out.)