【发布时间】:2026-02-15 14:25:01
【问题描述】:
可能的重复:D3 text on mouseover
但不确定我的问题发生了什么并且非常卡住。
我正在构建具有多种布局的数据可视化。我目前正在尝试制作一个中间居中文本的饼图,每当有人将鼠标悬停在弧上时,它就会在中心显示它的文本。
function GUP_PieRender() {
var svg = d3.select(targetDOMelement).append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var g = svg.selectAll(".arc")
.data(pie(dataset))
.enter().append("g")
.attr("class", "arc")
.on("mouseover", function(d) { d3.select("text").text(d.data.ResearchArea)}); //Problem area
g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color(d.data.ResearchArea); });
g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.style("text-anchor", "middle");
}
它所做的是在另一个具有文本的 D3 条形图布局中显示文本。所以我一定是过早地调用 mouseover 事件并将其附加到其中的最后一个文本元素?
我可以得到补救吗?
谢谢。
【问题讨论】:
标签: javascript d3.js