I'm trying to add tooltips to some SVG labels in the D3 Tree layout. Here is the function where the labels are rendered with a transition:
buildLabels() {
const labelSelection = d3.select('svg g.labels')
.selectAll('text.label')
.data(this.nodes);
labelSelection.exit()
.remove();
labelSelection.enter()
.append('text')
.style('fill', 'none')
.style('stroke', 'none')
.style('stroke-width', '0')
.attr('transform', (d: any) => // ...)
.style('fill-opacity', 0)
.transition()
.duration(450)
.ease(d3.easeCircleIn)
.attr('transform', (d: any) => {
// ...
})
.attr('class', 'label')
.style('stroke', '#393D3E')
.style('fill', '#393D3E')
.style('fill-opacity', 1)
.style('stroke-width', '.4')
.style('text-anchor', (d: any) => d.parent ? 'start' : 'end')
.text(d => d.name);
}
I've tried adding
.append('title')
.text(d => d.name)
after the .text, but I get a long console error of
core.js:4061 ERROR TypeError: labelSelection.enter(...).append(...).style(...).style(...).style(...).attr(...).style(...).transition(...).duration(...).ease(...).attr(...).attr(...).style(...).style(...).style(...).style(...).style(...).text(...).append is not a function
If I change the function to:
labelSelection.enter()
.append('text')
.text(d => d.name)
.append('title')
.text(d => d.name);
I get the DOM that I'm expecting, which is
<text>
Node name
<title>Node name</title>
</text>
however, none of the nodes look correct and do not have the positions they're supposed to. Of course the transitions are all removed as well.
My question is, is there another way I can add a title that isn't clunky, or how I can get around the error above. Thanks!