简体   繁体   English

D3 工具提示 + 鼠标悬停

[英]D3 Tooltip + Mouseover

I tried to apply tooptip when mouseover event happens with brushtool in D3.当鼠标悬停事件发生在D3中的brushtool时,我尝试应用toptip。 I figured out why it didn't work out and the solution for this is using a customized helper function.我弄清楚了为什么它没有成功,解决方案是使用定制的助手 function。

Accordingly, I am studying the meaning of the helper function I got from googling.因此,我正在研究我从谷歌搜索得到的助手 function 的含义。 In the meantime, I came across a code I can't under stand, which is function chaining (i assume)与此同时,我遇到了一个我无法理解的代码,即 function 链接(我假设)

What does this code mean?这段代码是什么意思? and How does it work?它是如何工作的?

   function tooltip(selection){


    selection.on('mouseover.tooltip', function(pD, pI){
        // Clean up lost tooltips
        d3.select('body').selectAll('div.tooltip').remove();
        // Append tooltip
        tooltipDiv = d3.select('body')
                       .append('div')
                       .attr('class', 'tooltip')
        var absoluteMousePos = d3.mouse(bodyNode);
        tooltipDiv.style({
            left: (absoluteMousePos[0] + 10)+'px',
            top: (absoluteMousePos[1] - 40)+'px',
            'background-color': '#d8d5e4',
            width: '65px',
            height: '30px',
            padding: '5px',
            position: 'absolute',
            'z-index': 1001,
            'box-shadow': '0 1px 2px 0 #656565'
        });

in particular, I can't get the meaning of特别是,我无法理解

selection.on('mouseover.tooltip') selection.on('mouseover.tooltip')

I understand我明白

selection.on('mouseover', function()) selection.on('鼠标悬停', function())

which means when 'mouseover' happenes, invoke the function defined.这意味着当“鼠标悬停”发生时,调用定义的 function。

But what does 'mouseover.tooltip' mean?但是“mouseover.tooltip”是什么意思?

Full code is attached as below.完整代码如下。

https://codepen.io/jotnajoa/pen/PoPEppN https://codepen.io/jotnajoa/pen/PoPEppN

Thank you in advance.先感谢您。

From the D3 docs:来自 D3 文档:

Adds or removes a listener to each selected element for the specified event typenames.为指定事件类型名称的每个选定元素添加或删除侦听器。 The typenames is a string event type, such as click, mouseover, or submit; typenames为字符串事件类型,如click、mouseover、submit等; any DOM event type supported by your browser may be used.可以使用浏览器支持的任何 DOM 事件类型。 The type may be optionally followed by a period (.) and a name;类型可以选择后跟句点 (.) 和名称; the optional name allows multiple callbacks to be registered to receive events of the same type, such as click.foo and click.bar.可选名称允许注册多个回调来接收相同类型的事件,例如 click.foo 和 click.bar。 To specify multiple typenames, separate typenames with要指定多个类型名,请使用分隔类型名
spaces, such as input change or click.foo click.bar.空格,例如输入更改或 click.foo click.bar。

selection.on('mouseover', function()) replaces the event handler selection.on('mouseover', function())替换事件处理程序

selection.on('mouseover.name', function()) adds another event handler selection.on('mouseover.name', function())添加另一个事件处理程序

But you do not need to add another event to solve your problem.但是您不需要添加另一个事件来解决您的问题。 The code seems to work using the event name without the dot(.) in this forked code pen该代码似乎可以在这个分叉的代码笔中使用没有点(。)的事件名称

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

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