I am creating a legend (var legend) for my d3js chart. The data is bound to the parent 'g' element, specifically a string (label) that I need to get at in the child 'text' element.
QUESTION: How to assign the parent data from 'g' element to the child text element?
Code:
var legend = svg.selectAll(".legend")
.data(color.domain().slice().reverse()) // color is array of strings with length = 6
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(-20," + i * 20 + ")"; });
legend.data(headers).enter().append("text") // headers is array of strings with length = 6
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return this.parentElement.__data__; }); // not working code here
Thanks! Full code: https://github.com/DeBraid/www.cacheflow.ca/blob/master/styles/js/d3kickchart.js