简体   繁体   English

D3在过渡后启用鼠标悬停

[英]D3 enable mouseover AFTER transition

How can I add a d3-tip / mouseover event AFTER a transition on a histogram / bar chart? 在直方图/条形图上进行过渡后,如何添加d3-tip /鼠标悬停事件?

I create a bar chart / plot: 我创建一个条形图/绘图:

canvas.call(tip);

var sampleBars = canvas.selectAll(".sampleBar")
  .data(data)
.enter().insert("rect", ".axis")
    .attr("class", "sampleBar")
    .attr("x", function(d) { return x(d.x) + 1; })
    .attr("y", function(d) { return y(d.y); })
    .attr("width", x(data[0].dx + data[0].x) - x(data[0].x) - 1)
    .attr("height", 0)
    .transition()
    .duration(2500)
    .delay(500)
    .attr("height", function(d) { return height - y(d.y); });

I want to add: 我要添加:

sampleBars
        .on('mouseover', tip.show)
        .on('mouseout', tip.hide);

And this is the tip: 这是提示:

    var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) { 
    return "<span style='color:white'>" + d3.round((d.y * 100)) 
    + "%" + "</span>" + " infected"; })

So that the mouseover event occurs after the transition is complete. 这样,鼠标悬停事件将在转换完成后发生。 But I get the error: 但是我得到了错误:

Uncaught TypeError: undefined is not a function

The error is for the line: 错误是针对该行:

.on('mouseover', tip.show)

I think there is a simple flaw in my logic. 我认为我的逻辑存在一个简单的缺陷。 I seem to be confused about how these events or attributes interact with each other. 对于这些事件或属性如何相互影响,我似乎感到困惑。

Again: I want to 'activate' the mouseover tip AFTER the transition is complete so that after the transition does its thing the tip will appear if the user puts their mouse over each bar. 再说一次:我想在过渡完成后“激活”鼠标悬停提示,以便在过渡完成后,如果用户将鼠标放在每个栏上,该提示就会出现。 I have no problem creating the mouseover event and having it work on user mouseover to display the data I want, but I am having no luck with making this work with a transition of those bars. 创建鼠标悬停事件并使其在用户鼠标悬停上工作以显示所需的数据时,我没有问题,但是通过这些栏的过渡使此工作正常运行,我一点都不走运。

Instead of adding/removing events, one approach is to simply set/unset the pointer-events attribute so that the events don't fire when you want them suppressed. 除了添加/删除事件外,一种方法是简单地设置/取消设置pointer-events属性,以便在您希望抑制事件时不触发事件。

 var myselection = d3.select('body') .append('svg').attr({height: 200, width: 200}) .append('circle') .attr( {cx: 100, cy: 100, r: 0}) .attr('pointer-events', 'none') .on('mouseover', function() { console.log('mouseover'); }) .on('mouseout', function() { console.log('mouseout'); }) myselection .transition().duration(4000).attr('r', 100) .transition().attr('pointer-events', 'auto') 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 

If you have the console window open you'll notice that mouse over/out logs nothing until the circle stops growing. 如果打开控制台窗口,您会注意到鼠标移入/移出不会记录任何东西,直到圆圈停止增长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM