Creating a legend (var legend
) for my d3js chart involves binding data to the parent 'g' element, specifically a string (label) that is needed in the child 'text' element.
Any ideas on how to assign the parent data from the 'g' element to the child text element?
Code:
var legend = svg.selectAll(".legend")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(-20," + i * 20 + ")"; });
legend.data(headers).enter().append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return this.parentElement.__data__; });
If you have any insights or suggestions, please share. Full code available at: https://github.com/DeBraid/www.cacheflow.ca/blob/master/styles/js/d3kickchart.js