Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple mouseover events with d3.tip

Tags:

tooltip

d3.js

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?

like image 453
Union find Avatar asked Nov 02 '14 05:11

Union find


3 Answers

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

like image 177
stallingOne Avatar answered Oct 15 '22 02:10

stallingOne


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);
like image 16
Union find Avatar answered Oct 15 '22 03:10

Union find


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.

like image 1
Chris Conlan Avatar answered Oct 15 '22 02:10

Chris Conlan