- 添加提示框元素
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("visibility","hidden");
- 为点添加交互事件
nodes.on("mouseover", function(d) {
tooltip.html("关系人:" + d.name + "<br />亲密度:" + d.values)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY + 10) + "px")
.style("visibility", "visible");
});
nodes.on("mousemove", function(d) {
tooltip.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY + 10) + "px");
});
nodes.on("mouseout", function() {
tooltip.style("visibility","hidden");
});
- 为线添加交互事件
edges.on("mouseover", function(d) {
tooltip.html(d.source.name + " > " + d.target.name + "<br />亲密度:" + d.target.values)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY + 10) + "px")
.style("visibility", "visible");
});
edges.on("mousemove", function(d) {
tooltip.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY + 10) + "px");
});
edges.on("mouseout", function() {
tooltip.style("visibility", "hidden");
});