我正在尝试重现此(捆绑布局):http ://bl.ocks.org/mbostock/7607999 ,这很好。
但是,当我悬停一个节点时,我想在每个链接上添加一个工具提示(工具提示将解释为什么项目链接在一起)。我完全不知道如何做到这一点。我已经尝试了各种代码,但由于我不太了解它,这很难。任何想法?
我希望在每个链接上显示工具提示(例如,在中间)(但我担心与突出显示的链接相关的工具提示不会很明显/易读),或者聚合每个链接的文本工具提示在图表下方某处突出显示 div 中的链接。
这是我的代码:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
font: 300 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
fill: #bbb;
}
.node:hover {
fill: #000;
}
.link {
stroke: steelblue;
stroke-opacity: .4;
fill: none;
pointer-events: none;
}
.node:hover,
.node--source,
.node--target {
font-weight: 700;
}
.node--source {
fill: #2ca02c;
}
.node--target {
fill: #d62728;
}
.link--source,
.link--target {
stroke-opacity: 1;
stroke-width: 2px;
}
.link--source {
stroke: #d62728;
}
.link--target {
stroke: #2ca02c;
}
/* test_tooltip addition
div.tooltip {
position: absolute;
text-align: center;
width: 60px;
height: 28px;
padding: 2px;
font: 12px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
*/
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<div>
<script>
// test_tooltip addition
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.attr("x", 200)
.attr("y", 200)
.attr("width", 400)
.attr("height", 100)
.attr("fill", "aliceblue")
.style("position", "absolute")
.style("z-index", "10")
.style("opacity", 0)
//.style("visibility", "hidden")
.text("a simple tooltip");
// end of addition
var diameter = 480, // diameter = 960 ou 480
radius = diameter / 2,
innerRadius = radius - 120;
var cluster = d3.layout.cluster()
.size([360, innerRadius])
.sort(null)
.value(function(d) { return d.size; });
var bundle = d3.layout.bundle();
var line = d3.svg.line.radial()
.interpolate("bundle")
.tension(.85)
.radius(function(d) { return -d.y; })
.angle(function(d) { return d.x / 180 * Math.PI; });
var svg = d3.select("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.append("g")
.attr("transform", "translate(" + radius + "," + radius + ")");
var link = svg.append("g").selectAll(".link"),
node = svg.append("g").selectAll(".node");
d3.json("test.json", function(error, classes) {
var nodes = cluster.nodes(packageHierarchy(classes)),
links = packageLinks(nodes); // links = packageImports(nodes);
link = link
.data(bundle(links))
.enter().append("path")
.each(function(d) { d.source = d[0], d.target = d[d.length - 1]; })
.attr("class", "link")
.attr("d", line);
// .attr("d", line); //original
/*
// test_tooltip addition
.attr("d", line)
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div .html(d.RG)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
// test_tooltip addition
*/
node = node
.data(nodes.filter(function(n) { return !n.children; }))
// skip if URL not desired
.enter().append('a')
.attr("xlink:href", function(d){return d.url;})
.append("text")
// skip until there
// .enter().append("text")
.attr("class", "node")
.attr("dy", ".31em")
// put the parents on the left rather than on the right
.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + ((-d.y) - 8) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
//.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + (d.y + 8) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
.style("text-anchor", function(d) { return d.x < 180 ? "end" : "start"; })
.text(function(d) { return d.key /*+ " truc"; */})
.on("mouseover", mouseovered)
.on("mouseout", mouseouted);
});
function mouseovered(d) {
node
.each(function(n) { n.target = n.source = false; });
link
.classed("link--target", function(l) { if (l.target === d) return l.source.source = true; })
.classed("link--source", function(l) { if (l.source === d) return l.target.target = true; })
.filter(function(l) { return l.target === d || l.source === d; })
.each(function() { this.parentNode.appendChild(this); });
node
.classed("node--target", function(n) { return n.target; })
.classed("node--source", function(n) { return n.source; });
}
function mouseouted(d) {
link
.classed("link--target", false)
.classed("link--source", false);
node
.classed("node--target", false)
.classed("node--source", false);
}
d3.select(self.frameElement).style("height", diameter + "px");
// Lazily construct the package hierarchy from class names.
function packageHierarchy(classes) {
var map = {};
function find(name, data) {
var node = map[name], i;
if (!node) {
node = map[name] = data || {name: name, children: []};
if (name.length) {
node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
node.parent.children.push(node);
node.key = name.substring(i + 1);
}
}
return node;
}
classes.forEach(function(d) {
find(d.name, d);
});
return map[""];
}
// Return a list of imports/links for the given array of nodes.
function packageLinks(nodes) { //function packageImports(nodes) {
var map = {},
links = []; // imports = [];
// Compute a map from name to node.
nodes.forEach(function(d) {
map[d.name] = d;
});
// For each import/link, construct a link from the source to target node.
nodes.forEach(function(d) {
if (d.links) d.links.forEach(function(i) { //if (d.imports) d.imports.forEach(function(i) {
links.push({source: map[d.name], target: map[i]}); //imports.push({source: map[d.name], target: map[i]});
});
});
return links; //return imports;
}
/*
// test_tooltip addition
d3.select("body")
.append("svg:circle")
.attr("stroke", "black")
.attr("fill", "aliceblue")
.attr("r", 50)
.attr("cx", 52)
.attr("cy", 52)
.on("mouseover", function(){return tooltip.style("visibility", "visible");})
.on("mousemove", function(){return tooltip.style("top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px");})
.on("mouseout", function(){return tooltip.style("visibility", "hidden");});
// test_tooltip addition
*/
</script>
</div>