javascript - 将鼠标悬停在链接上时显示文本

标签 javascript d3.js

我试图在链接上的鼠标悬停上显示文本,但我看不到任何文本。我只能像下面所附的代码一样更改颜色。

也欢迎任何有关工具提示的帮助,因为我也尝试过。

Here is the code to the jsfiddle .

最佳答案

试试这个方法。

link.on('mouseover', function(d, i) {
    d3.select(this).style("stroke", "red");
    if (!d3.select("#link-label-" + i).node()) {
        svg.append("text")
            .attr("id", "link-label-" + i)
            .attr("font-family", "Arial, Helvetica, sans-serif")
            .attr("x", function() {
                if (d.target.x > d.source.x) {
                    return (d.source.x + (d.target.x - d.source.x) / 2);
                } else {
                    return (d.target.x + (d.source.x - d.target.x) / 2);
                }
            })
            .attr("y", function() {
                if (d.target.y > d.source.y) {
                    return (d.source.y + (d.target.y - d.source.y) / 2);
                } else {
                    return (d.target.y + (d.source.y - d.target.y) / 2);
                }
            })
            .attr("fill", "Black")
            .style("font", "normal 12px Arial")
            .attr("dy", ".35em")
            .text(function() {
                return d.source.name + " - " + d.target.name;
            });
    }
}).on('mouseout', function(d, i) {
    d3.select(this).style("stroke", d.target.group == 2?"black":"#9ecae1");
    d3.select("#link-label-" + i).remove();
});

已更新fiddle

关于javascript - 将鼠标悬停在链接上时显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32989051/

相关文章:

javascript - 使用 d3.js 单击时的爆炸饼图

javascript - 位掩码:如何确定是否只设置了一位

javascript - 了解事件循环

javascript - 如何功能检测输入事件级别 1 或 2 支持?

javascript - 返回带有 Node 的原始 html(只是 html 到 var 中)

javascript - 尽管 MFP 服务器已关闭,但仍返回访问 token

javascript - 如果两个刻度具有相同的值,d3 轴不显示刻度标签

javascript - 停止 d3 圆包标签重叠

javascript - 使用 d3 更新我的条形图时出错

javascript - 使用 D3 创建一个表,其中两行是兄弟