【问题标题】:How to display a text when mouseover a node in D3 force layout如何在 D3 强制布局中将鼠标悬停在节点上时显示文本
【发布时间】:2014-06-28 09:10:23
【问题描述】:

当我将鼠标移到D3.js 中的 Force-Directed Graph 中的一个节点上时,我试图显示文本。我的问题是,当我将鼠标移到任何节点上时,会显示这些节点的所有文本。这是我的代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>

.node {
stroke: #fff;
stroke-width: 1.5px;
}

.nodeDetail {
stroke: #fff;
stroke-width: 1.5px;
}

.link {
stroke: #999;
stroke-opacity: .6;
}

node .text {
font: 12px sans-serif;
pointer-events: none;
}

</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script src="d3/d3.v3.min.js charset=UTF-8"></script>
<script>
var graph = {
"nodes":[
{"name":"Myriel","group":1},
{"name":"Napoleon","group":1},
{"name":"Mlle.Baptistine","group":1},
{"name":"Mme.Magloire","group":1},
{"name":"CountessdeLo","group":1}
],
"links":[
{"source":1,"target":0,"value":1},
{"source":2,"target":0,"value":8},
{"source":3,"target":0,"value":10},
{"source":3,"target":2,"value":6},
{"source":4,"target":0,"value":1}
]
};
var width = 960,
height = 500;

var color = d3.scale.category20();

var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

var drawGraph = function(graph) {
force
  .nodes(graph.nodes)
  .links(graph.links)
  .start();

var link = svg.selectAll(".link")
  .data(graph.links)
  .enter().append("line")
  .attr("class", "link")
  .style("stroke-width", function(d) { return Math.sqrt(d.value); });

var gnodes = svg.selectAll('g.gnode')//('g.gnode')
 .data(graph.nodes)
 .enter()
 .append('g')
 .classed('gnode', true);

var node = gnodes.append("circle")
  .attr("class", "node")
  .attr("r", 5)
  .style("fill", function(d) { return color(d.group); })
  .on("mouseover", function(d)
 {
    d3.select(this).transition()
    .duration(750)
    .attr("r", 15);
    return labels.style("visibility", "visible");
 })
.on("mouseout", function()
 {
 d3.select(this).transition()
.duration(750)
.attr("r", 5);
 return labels.style("visibility", "hidden");//})
 })
 .call(force.drag);

 var labels = gnodes.append("text")
  .text(function(d) { return d.name; })
  .style("visibility", "hidden");

 /*gnodes.append("text")
 .text(function(d) { return d.name; })
 .style("visibility", "hidden");*/

 force.on("tick", function() {
 link.attr("x1", function(d) { return d.source.x; })
    .attr("y1", function(d) { return d.source.y; })
    .attr("x2", function(d) { return d.target.x; })
    .attr("y2", function(d) { return d.target.y; });

 gnodes.attr("transform", function(d) { 
    return 'translate(' + [d.x, d.y] + ')'; 
}); 
});
};

drawGraph(graph);
</script>
</body>
</html>

我尝试执行以下代码,但也没有用:

.on("mouseover", function(d)
 {
    d3.select(this).transition()
    .duration(750)
    .attr("r", 15);
     d3.select(this).append(text)
    .style("visibility", "visible");
 })

当我将鼠标移到特定节点上时,如何显示与特定节点相关的文本?谁能帮我解决这个问题。提前谢谢你。

【问题讨论】:

标签: javascript jquery css d3.js force-layout


【解决方案1】:

自己在这里学习 d3 - 也非常感谢 juvian

我发现了一种更透明的编码方式

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

选择您悬停的对象(组),选择其中的文本并将样式更改为可见。这也避免了创建一个标签变量的需要,这会使数组索引有点混乱。

【讨论】:

    【解决方案2】:

    这应该会有所帮助:

    .on("mouseover", function(d)
     {
         d3.select(labels[0][d.index]).style("visibility","visible")
     })
    .on("mouseout", function(d)
     {
         d3.select(labels[0][d.index]).style("visibility","hidden")
     })
    

    【讨论】:

    • 非常感谢朱维安。它现在正在工作。我需要你的一段代码来将它应用到类似于我上面的代码的东西上。再次感谢您。
    • 请再问一个问题。您的代码中的 d.index 是什么?如果您不介意,请您简单解释一下吗?
    • @user2864315 github.com/mbostock/d3/wiki/Selections#wiki-d3_event 它说 d 是基准,不知道那是什么,但似乎你鼠标悬停的节点的数据,你可以做一个 console.log(d) 为我确实知道它的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 2015-06-06
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多