I've been working with a D3 svg chart with built in tool tips (using the d3-tip library I believe). The original code can be seen here: http://bl.ocks.org/Caged/6476579
I've successfully been able to customize it for the most part, but I need to add a second mouseover effect; so that when you mouseover a bar it not only opens the tool tip, but also another tip or layer in the upper right corner of the chart.
I tried to do something like the following, but it doesn't seem to work. Am I just missing something in my syntax? Or will this strategy not work at all?
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Frequency2:</strong> <span style='color:red'>" + d.frequency2 + "</span>";
})
//adding second tool tip
var tip2 = d3.tip()
.attr('class', 'd3-tip')
.offset([-40, 0])
.html(function(d) {
return "<strong>Other Variables:</strong> <span style='color:red'>" + d.frequency2 + "</span>";
})
and then
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency2); })
.attr("height", function(d) { return height - y(d.frequency2); })
.on('mouseover', tip.show, tip2.show)
.on('mouseout', tip.hide, tip2.hide);
The chart still works, but now neither tool tip shows.
What you're doing right now...
... will not work. In
selection.on
, the third argument is the capture:Thus, the solution is wrapping both
tip
andtip2
in another function:Here is a demo (hover over the circle):