d3.tip的多个鼠标悬停事件



我正在尝试更改svg元素的笔划,该元素也调用了d3.tip。

我的代码的相关部分如下所示:

    map.call(tip);
    map.on("mouseover", function(){ d3.select(this).style({stroke:"#7bd362"}); tip.show; });
    map.on("mouseout",tip.hide);

我可以让我的代码做一件事:在鼠标悬停时更改其笔划,或者显示工具提示。但我不能让这两件事同时发生。

之前有人成功地使用d3提示和其他鼠标事件吗?

我最终需要将数据对象传递给tip.show()函数。

最终代码:

map.on("mouseover", function(d){ 
  tip.show(d); 
 })
 .on("mouseout", function(d){
  tip.hide(d);          
});

我还没有尝试过,但这可能也有效:

map.on("mouseover", tip.show).on("mouseout", tip.hide);

我的解决方案:

额外的mouseoverd3.tip().html中工作吗。。。

let tip = d3.tip().html(function(d) {
    // some extra 'mouseover' code ...
    // some html generating code ...
    return 'some_html_code'
});

然后像这样在主D3代码中执行mouseout工作。。。

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 ...
        });

这在最新版本的d3.tip中有效,因为tip.show函数需要访问this,但tip.hide函数实际上不使用任何参数。

如果您正在寻找D3 v6的解决方案(我使用的是6.4.0)

我必须通过活动也

这就是对我有效的

.on("mouseover", function(e,d) { tip.show(e,d); })

链接到d3.tip-for-d3.v6:https://github.com/bumbeishvili/d3.tip-for-d3.v6

对我来说,这件事起到了的作用

rect.filter(function(d) { return d in data; })
        .on("click", function(d){
          var year = d/10000;
          year = Math.floor(year);
          var monthInt = d/100;
          var val = 0,id;
          for(var itr=0; itr<$rootScope.dom_elements.length; ++itr) {
             if(dom_element_to_append_to == $rootScope.dom_elements[itr].primary) {
                val = itr;
                break;
            }
        }
        monthInt = Math.floor(monthInt % 100);
        for (var itr = 0; itr<12; ++itr) {
           id = month[itr] + "" + varID;
           $('#' + id).css("z-index",0);
           $('#' + id).css("stroke","#000");
           $('#' + id).css("stroke-width", "2.5px");
       }
       id = month[monthInt-1] + "" + varID;
       currentPathId = id;
       $('#' + id).css("stroke","orange");
       $('#' + id).css("position","relative");
       $('#' + id).css("z-index",1000);
       $('#' + id).css("stroke-width", "4.5px");
       $rootScope.loadDayHourHeatGraph(year, monthInt , val, isCurrency);
   })
        .attr("fill", function(d) { return color(Math.sqrt(data[d] / Comparison_Type_Max )); })
        .on('mouseover', function(d) {
          tip.show(d);
          var year = d/10000;
          year = Math.floor(year);
          var monthInt = d/100;
          monthInt = Math.floor(monthInt % 100);
          var id = month[monthInt-1] + "" + varID;
          if(id!=currentPathId) {
            $('#' + id).css("stroke","orange");
            $('#' + id).css("position","relative");
            $('#' + id).css("z-index",-1000);
            $('#' + id).css("stroke-width", "4.5px");
        }
    })
        .on('mouseout', function(d) {
          tip.hide(d);
          var year = d/10000;
          year = Math.floor(year);
          var monthInt = d/100;
          monthInt = Math.floor(monthInt % 100);
          var id = month[monthInt-1] + "" + varID;
          if(id != currentPathId) {
            $('#' + id).css("z-index",-1000);
            $('#' + id).css("stroke","#000");
            $('#' + id).css("stroke-width", "2.5px");
        }
    });

最新更新