I'm trying to make my own legend for a chart and at the moment the bars with the colours are appearing but not the text. Here is my code:
viz.selectAll("rect").data(data).enter().append("rect")
.attr({
x: margin,
y: function (d, i) { return ((3*b)/data.length)*i; } ,
width: 30,
height: 15,
fill: function(d, i) { return color(i); }
})
.append("text").text(function (d) { return d.name; })
.attr({
"text-anchor" : "start",
x: 2 * margin + parseFloat (d3.select(this).attr("width")), //doing 2* marging so I can add some space between the text and the bar
y: parseFloat(d3.select(this).attr("y"))
});
I also tried changing both x and y to 0 in case I got the positioning wrong but that didn't work. When I tried to inspect the element using my browser, I noticed that it hasn't added the text at all. Am I doing this wrong?