I'm trying to add text tooltips to each of my bars on my bar chart on mouseover. I am new to d3, so I'm having trouble with adding text. What I've tried is a combination of different strategies I've found online, but I'm still not getting a tip to show up. Not sure what I'm missing or where I went wrong. Any help is appreciated.
Thank you!
bar.enter()
.append("g")
.attr("class", "bar-container")
.append("rect")
.attr("class", "bar")
.attr('fill','#4EC7BD')
.attr("x", function(d) { return x(d.id); })
.attr("y", function(d) { return y(eval('d.'+scope.metric)); })
.attr("height", function(d) { return height - y(eval('d.'+scope.metric)); })
.attr("width", x.rangeBand())
.on('click', function(d){
scope.showDetails(d, eval('d.'+scope.metric))
})
// start tooltip
.on("mouseover", function(d){
console.log("D",scope.metric);
var xPos = parseFloat(d3.select(this).attr("x"));
var yPos = parseFloat(d3.select(this).attr("y"));
var height = parseFloat(d3.select(this).attr("height"))
d3.select(this)
.append("text")
.attr("class", "d3tip")
.attr("x",xPos)
.attr("y",yPos +height/2)
.text("test");
})
.on("mouseout",function(){
d3.select(".tooltip").remove();
});
// end tooltip
// removed data:
bar.exit().remove();
// updated data:
bar
.transition()
.duration(750)
.attr("y", function(d) { return y(eval('d.'+scope.metric)); })
.attr("height", function(d) { return height - y(eval('d.'+scope.metric)); });