D3显示鼠标悬停时的工具提示超时

时间:2016-06-28 13:31:16

标签: d3.js mouseevent settimeout transition mouseenter

我想在每次鼠标进入矩形时将工具提示添加到barchar的rects,并且鼠标在几毫秒后在rect上。当鼠标离开任何矩形时,我也想删除它们。

我尝试将它与d3事件绑定并使用setTimeout / clearTimeout。

似乎工作除非当工具提示的行正在转换时快速移动鼠标通过不同的条,

问题在这种情况下 工具提示从条移动到条鼠标进入/离开 忽略超时。工具提示的文本似乎确实做了一些奇怪的事情。

让我告诉你工具提示在超时之前是如何出现的:

gif showing the problem

DEMO and FULL CODE at CODEPEN

用于绑定绘制工具提示的函数的代码:

... 
var bar = svg.select(".bars")
            .selectAll(".barchart-group")
            .data(data);
... 
var come = bar.enter()
            .append("rect")
            .style("stroke", undefined)
            .style("fill", "hsla(34, 82%, 48%, 0.79)");
... 
        //bind events to new bars
        come.on("mouseover", delayTooltip)
            .on("mouseout", removeBarTooltip);
... 

tootlip相关功能的代码:

function delayTooltip(d, i) {
  // this here is every rect of the bar chart
    this.hoverTimeout = window.setTimeout(addBarTooltip.bind(this), 1000, d, i);
}

function addBarTooltip(d, i) {
  // **** PROBLEM **** 
  // this executes whithout waiting the set timeout
    var firstAnims = 500;
    var thisbar = d3.select(this);
    var tooltip = d3.select(this.parentNode);
    var gRoot = d3.select(this.parentNode.parentNode);
    //Lets define some points of interest
    var p0 = [+thisbar.attr("x") + (barWidth - 1) / 2, +thisbar.attr("y")];
    var p1 = [p0[0], +p0[1] - 9];
    var p2 = [p0[0], +p0[1] - 13];
    //relevant points to draw tooltips
    var line = gRoot.select(".linePointers");
    var text = gRoot.select(".textHelpers");
    line.append("polyline")
        .style("stroke-dasharray", "2,1")
        .style("stroke", "black")
        .attr("points", [p0, p0])
        .transition("line-Y-axis")
        .duration(firstAnims)
        .attrTween("points", function(d, i, a) {
            return d3.interpolate([p0, p0], [p0, p1]);
        });

    text.append("text")
        .attr("dy", ".2em")
        .attr("text-anchor", "middle")
        .attr("x", p2[0])
        .attr("y", p1[1])
        .text(d.value)
        .style("font-size", ".85em")
        .style("opacity", 0)
        .transition("text-Y-axis")
        .delay(firstAnims)
        .duration(300)
        .style("opacity", 1)
        .attr("y", p2[1]);

}

function removeBarTooltip() {
    window.clearTimeout(this.hoverTimeout);
    var firstAnims = 200;
    var thisbar = d3.select(this);
    var tooltip = d3.select(this.parentNode);
    var gRoot = d3.select(this.parentNode.parentNode);
    //relevant points to draw tooltips
    var p0 = [+thisbar.attr("x") + (barWidth - 1) / 2, +thisbar.attr("y")];
    var p1 = [p0[0], +p0[1] - 9];
    var p2 = [p0[0], +p0[1] - 13];
    //Lets make a group for each part of our tooltip
    var line = gRoot.select(".linePointers")
        .selectAll("polyline");
    var text = gRoot.select(".textHelpers")
        .selectAll("text");

    line.transition("line-Y-axis")
        .duration(firstAnims)
        .attrTween("points", function(d, i, a) {
            return d3.interpolate([p0, p1], [p0, p0]);
        })
        .remove();

    text.style("opacity", 0.7)
        .transition("text-Y-axis")
        .delay(firstAnims)
        .duration(300)
        .style("opacity", 0)
        .attr("dy", p0[1] - p2[1])
        .remove();
}

我更愿意尽可能避免添加任何其他库。

2 个答案:

答案 0 :(得分:0)

尝试foxToolTip.js

具有hover和mouseout的延迟和转换持续时间选项

https://github.com/MichaelRFox/foxToolTip.js

查看我的bl.ock:http://bl.ocks.org/MichaelRFox/59cdc1c3478fb0362448cf87fdab30d0

使用0.5秒延迟显示悬停在文本元素上的工具提示

答案 1 :(得分:0)

问题出在removeBarTooltip()。让我们首先看看这两条线,我在那里定义了一个tootlip线的点:

var p0 = [+thisbar.attr("x") + (barWidth - 1) / 2, +thisbar.attr("y")];
var p1 = [p0[0], +p0[1] - 9];

稍后我正在执行此功能:

[...]
line.transition("line-Y-axis")
    .duration(firstAnims)
    .attrTween("points", function(d, i, a) {
        return d3.interpolate([p0, p1], [p0, p0]);
    })
    .remove();

所发生的事情是,由于var p0值,根据鼠标所在的条形,tootlip线从一个柱移动到另一个柱。

为了解决这个问题,我将attrTween的行转换更改为:

line.transition("line-Y-axis")
    .duration(firstAnims)
    .attrTween("points", function(d, i, a) {
        var p0current = a.split(",");
        var p1current = p0current.splice(2);
        return d3.interpolate([p0current, p1current], [p0current, p0current]);
    })
    .remove();

使用参数a获取points属性的实际值,并仅基于它插入实际的movemevent。

DEMO and FULL code at CODEPEN.