简体   繁体   English

.on('mouseover')on d3

[英].on ('mouseover') on d3

I have a clarification question regarding the .on('mouseover') method in d3. 我对d3中的.on('mouseover')方法有一个澄清的问题。 The code I have is: 我的代码是:

svg.selectAll('text')
            .data(dataset)
            .enter()
            .append('text')
            .attr('x',function(d){
              return xScale(d[0]);
            })
            .attr('y',function(d){
              return yScale(d[1]);
            })
            .text(function(d) {
                return d[0] + ',' + d[1];
              })
            .style('visibility','hidden');
            .on('mouseover',...)

What kind of function shall I put instead of the ... in the code to make the style of a single text tag become visible when hovering with the mouse? 我应该在代码中放置哪种功能而不是... ,以使单个文本标签的样式在鼠标悬停时visible

I have checked different solutions but none of them work. 我已经检查了不同的解决方案,但是它们都不起作用。 (One is: d3 - trigger mouseover event ) (一个是: d3-触发mouseover事件

Moreover, I was wondering if what I thought about d3 workflow is corrected (I started to learn d3 only yesterday so be patient..): .selectAll iterates through what it is given to it inside the .data argument. 此外,我想知道我对d3工作流程的想法是否得到纠正(我昨天才开始学习d3,所以要耐心..) .selectAll遍历.data参数中赋予它的内容。 At each iteration a text object(?) is created at the given position and with a given label. 在每次迭代中,都会在给定位置和给定标签下创建一个文本对象(?)。 What does the .style refer to? .style指的是什么? The single object .selectAll iterates through? 单个对象.selectAll迭代遍历? So are there multiple .style for each object iterated? 那么,每个对象都有多个.style进行迭代吗? And how to modify that object? 以及如何修改该对象? Would d3.select(this).style('visibility','visible') be enough? d3.select(this).style('visibility','visible')是否足够? (Looking at the link above it does not seem so...) (看上面的链接似乎并不...)

You would need the following code: 您将需要以下代码:

.on("mouseover", function() { d3.select(this).style("visibility", "visible"); });

That said, hidden elements never get mouse events, so this code will never be triggered. 也就是说,隐藏元素永远不会获得鼠标事件,因此永远不会触发此代码。 You'll have to use another element that does receive events to trigger the text appearing. 您必须使用另一个确实会接收事件的元素来触发文本的显示。 See eg this question . 参见例如 这个问题 As pointed out in the comments, you can use the pointer-events property to make non-visible elements receive mouse events. 如注释中所指出的,您可以使用pointer-events属性使不可见元素接收鼠标事件。

This is what happens behind the scenes when you run this code: 这是运行此代码的幕后发生的事情:

  • svg.selectAll('text') selects all text elements. svg.selectAll('text')选择所有text元素。
  • .data(dataset) binds the data in dataset to those selected elements. .data(dataset)将数据dataset的数据绑定到那些选定的元素。
  • .enter() chooses the enter selection. .enter()选择回车选择。 The enter selection contains all data items for which no matching DOM element was found, ie everything in dataset that has no matching text element. enter选择包含未找到匹配的DOM元素的所有数据项,即dataset没有匹配的text元素的所有数据项。
  • .append('text') appends a new text element for each of the items in the enter selection. .append('text')为enter选择中的每个项目附加一个新的text元素。

Everything else happens for each item in the enter selection as well. 输入选择中的每个项目也会发生其他所有事情。 So you're setting the attributes, styles, etc for each of the added elements. 因此,您要为每个添加的元素设置属性,样式等。 If you pass a function when setting an attribute, it's evaluated with the data element that is bound to the particular DOM element ( d ). 如果在设置属性时传递函数,则会使用绑定到特定DOM元素( d )的数据元素来评估它。

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

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