我正在制作D3树状图。我添加了一个应显示在框中的工具提示(请参阅:http://bl.ocks.org/Caged/6476579)。它以文本形式显示,但没有出现在框中。我想念什么?
<HTML>
<HEAD>
<script type="text/javascript" src="http://d3js.org/d3.v2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<style type="text/css">
rect {
fill: none;
stroke: #fff;
}
rect:hover {
opacity: 0.5;
}
text {
font-family: "Times New Roman", Times, serif;
font-size: 12px;
}
</style>
<style>
body {
font: 10px sans-serif;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
<BODY>
<div id="body"></div>
<script type="text/javascript">
var pathJson = {
"name": "Sample data",
"children": [{
"name": "Title 1",
"size": 1,
"children": [{
"name": "Title 1-1",
"size": 1,
"children": [{
"name": "1-1-1",
"size": 1
}, {
"name": "1-1-2",
"size": 1
}, {
"name": "1-1-3",
"size": 1
}, {
"name": "1-1-4",
"size": 1
}]
}, {
"name": "Title 1-2",
"size": 1,
"children": [{
"name": "1-2-1",
"size": 1
}, {
"name": "1-2-2",
"size": 1
}, {
"name": "1-2-3",
"size": 1
}]
}, {
"name": "Title 1-3",
"size": 1,
"children": [{
"name": "1-3-1",
"size": 1
}]
}]
}]
}
var w = 600 - 80,
h = 500 - 100,
x = d3.scale.linear().range([0, w]),
y = d3.scale.linear().range([0, h]),
color = d3.scale.category10(),
root,
node;
var treemap = d3.layout.treemap()
.round(false)
.size([w, h])
.sticky(true)
.padding([10, 0, 0, 0])
.value(function(d) {
return d.size;
});
var svg = d3.select("#body").append("div")
.attr("class", "chart")
.style("width", w + "px")
.style("height", h + "px")
.append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(.5,.5)");
var tip = d3.tip()
.offset([20, 0])
.html(function(d) {
return "<strong>Project:</strong> <span style='color:red'>" + d.name + "</span>";
})
svg.call(tip);
node = root = pathJson;
var nodes = treemap.nodes(root)
.filter(function(d) {
return !d.children;
});
var cell = svg.selectAll("g")
.data(nodes)
.enter().append("svg:g")
.attr("class", "cell")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
})
.on("click", function(d) {
return zoom(node == d.parent ? root : d.parent);
})
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
cell.append("svg:rect")
.attr("width", function(d) {
return d.dx - 1;
})
.attr("height", function(d) {
return d.dy - 1;
})
.style("fill", function(d) {
return color(d.parent.name);
});
cell.append("svg:text")
.attr("x", function(d) {
return d.dx / 2;
})
.attr("y", function(d) {
return d.dy / 2;
})
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) {
return d.name;
})
.style("opacity", function(d) {
d.w = this.getComputedTextLength();
return d.dx > d.w ? 1 : 0;
});
d3.select(window).on("click", function() {
zoom(root);
});
d3.select("select").on("change", function() {
treemap.value(this.value == "size" ? size : count).nodes(root);
zoom(node);
});
function size(d) {
return d.size;
}
function count(d) {
return 1;
}
function zoom(d) {
//alert(d.name);
var kx = w / d.dx,
ky = h / d.dy;
x.domain([d.x, d.x + d.dx]);
y.domain([d.y, d.y + d.dy]);
var t = svg.selectAll("g.cell").transition()
.duration(d3.event.altKey ? 7500 : 750)
.attr("transform", function(d) {
return "translate(" + x(d.x) + "," + y(d.y) + ")";
});
t.select("rect")
.attr("width", function(d) {
return kx * d.dx - 1;
})
.attr("height", function(d) {
return ky * d.dy - 1;
})
t.select("text")
.attr("x", function(d) {
return kx * d.dx / 2;
})
.attr("y", function(d) {
return ky * d.dy / 2;
})
.style("opacity", function(d) {
return kx * d.dx > d.w ? 1 : 0;
});
//.style("font-size", function(d) { return kx * d.dx > d.w ? "20px" : "12px";});
node = d;
d3.event.stopPropagation();
}
</SCRIPT>
</BODY>
</htmL>
最佳答案
一切都很好,但是您错过了将类添加到d3.tip()
的操作,如下所示
.attr('class', 'd3-tip')
var tip = d3.tip()
.attr('class', 'd3-tip') // <---- missing this
.offset([20, 0])
.html(function(d) {
return "<strong>Project:</strong> <span style='color:red'>" + d.name + "</span>";
});
关于javascript - Javascript工具提示未显示在样式定义的框中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30656715/