【发布时间】:2017-01-30 02:10:25
【问题描述】:
我使用这个example 创建了一个堆积条形图。该图表可以工作并呈现,但我无法添加鼠标悬停标签。
我试过这个...
DATE.selectAll("rect")
.data(function(d) { return d.ages; })
.enter().append("rect")
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.y1); })
.attr("height", function(d) { return y(d.y0) - y(d.y1); })
.style("fill", function(d) { return color(d.name); });
.append("svg:title")
.text(functino(d){return "foo"});
但是在添加.append("svg:title... 之后,图形会停止渲染。如果我删除 .style("fill... 线,图形会呈现,但它没有堆叠,也没有鼠标悬停功能。
我也尝试过使用工具提示路由。 (Source)
.on("mouseover", function() { tooltip.style("display", null); })
.on("mouseout", function() { tooltip.style("display", "none"); })
.on("mousemove", function(d) {
var xPosition = d3.mouse(this)[0] - 15;
var yPosition = d3.mouse(this)[1] - 25;
tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
tooltip.select("text").text(d.y);
});
// Prep the tooltip bits, initial display is hidden
var tooltip = svg.append("g")
.attr("class", "tooltip")
.style("display", "none");
tooltip.append("rect")
.attr("width", 30)
.attr("height", 20)
.attr("fill", "white")
.style("opacity", 0.5);
tooltip.append("text")
.attr("x", 15)
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.attr("font-size", "12px")
.attr("font-weight", "bold");
但还是不走运。有我需要加载的库吗?不知道发生了什么。
【问题讨论】:
标签: d3.js tooltip bar-chart stacked-chart