I am displaying labels with my donut chart like this;
this.graphGroup
.selectAll('allLabels')
.data(data_ready)
.enter()
.append('text').style('font-size', '24px')
.text(d => {
return d.data.name; //LINE 1
})
.attr('transform',`${calculatedValue}`)
.style('text-anchor', 'start');
I want to use this answer How do I include newlines in labels in D3 charts? but adding the code after this addition of labels like this,
this.graphGroup
.selectAll('text')
.each
( function (d) {
var el = this.d3.select(this);
console.log(el);
var words = d.name.split(' ');
console.log(words);
el.text('');
for (var i = 0; i < words.length; i++) {
var tspan = el.append('tspan').text(words[i]);
if (i > 0)
tspan.attr('x', 0).attr('dy', '15');
}
});
When I use the each() on the first selection I get an error saying the selection is undefined. I tried adding the code on line 1 but I can only pass text into a d3.text().
How can I break the labels correctly?
EDIT: I also had an error saying my local variable d3 was undefined, but it worked while creating the graphGroup and all the other selections, I then imported select from d3 directly.