I am trying to change the stroke of an svg element that also has d3.tip called upon it.
The relevant part of my code looks as follows:
map.call(tip);
map.on("mouseover", function(){ d3.select(this).style({stroke:"#7bd362"}); tip.show; });
map.on("mouseout",tip.hide);
I am able to make my code do one event: have its stroke changed on mouseover, or show a tooltip. But I cannot make the two events happen simultaneously.
Has anyone had success with d3 tip before and additional mouse events before?
If you are looking for a solution with D3 v6 (I'm using 6.4.0)
I had to pass event also
This was what worked for me
.on("mouseover", function(e,d) { tip.show(e,d); })
Link to d3.tip-for-d3.v6: https://github.com/bumbeishvili/d3.tip-for-d3.v6
I ended up needing to pass the data object in to the tip.show() function.
The final code:
map.on("mouseover", function(d){
tip.show(d);
})
.on("mouseout", function(d){
tip.hide(d);
});
I haven't tried it, but this may also work:
map.on("mouseover", tip.show).on("mouseout", tip.hide);
My solution:
Do the extra mouseover
work in d3.tip().html
like so ...
let tip = d3.tip().html(function(d) {
// some extra 'mouseover' code ...
// some html generating code ...
return 'some_html_code'
});
Then do the mouseout
work in the main D3 code like so ...
svg.append('g')
.selectAll("rect")
.data(function(d) { return d }).enter()
.append("rect")
.on('mouseover', tip.show)
.on('mouseout', function(d, i) {
tip.hide();
// some extra 'mouseout' code ...
});
This works in the latest version of d3.tip
, because the tip.show
function requires access to this
, but the tip.hide
function doesn't actually use any of its arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With