尽管存在数据,但工具提示未显示
Tooltip is not displaying despite data being present
我有这个 d3 项目,我在其中创建了条形图。我有几个条形图,我希望在将鼠标悬停在特定条形图上时显示数据。但是,由于某些原因,尽管数据和 css 存在,但它不显示内容。我将 rects 元素创建为条形和 div 元素,其中包含数据的工具提示 class。请帮忙。
这是我的代码:
chart.selectAll("rect")
.data(json.data)
.enter()
.append("rect")
.attr("x", (d) => xScale(parseDate(formatDate(d[0]))))
.attr("y", (d) => yScale(d[1]))
.attr("height", (d) => height - yScale(d[1]))
.attr("width", xScale.bandwidth())
.attr("class", "rect-col")
.append("div")
.attr("class", "tooltip")
.text((d) => d[0] + d[1])
.on("mouseenter", function (d, i) {
//d3.select(this).style("fill", "#EE9A2F");
d3.select(this).style("visibility", "visible");
})
.on("mouseleave", function (d, i) {
//d3.select(this).style("fill", "#EECF10");
d3.select(this).style("visibility", "visible");
});
我的css:
.tooltip {
text-align: center;
width: 60px;
height: 28px;
padding: 2px;
font: 12px sans-serif;
background: green;
border: 0px;
border-radius: 8px;
pointer-events: none;
color: red;
visibility: hidden;
}
link 到我的代码笔
codepen
简单地说:您不能将<div>
附加到SVG <rect>
。
此处的解决方案是为 div...
创建一个选择
const tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip");
... 然后,在矩形的 "mouseenter"
侦听器内:
tooltip.style("visibility", "visible")
.text(d[0] + d[1])
这是更新后的 CodePen,您会在图表下方看到工具提示:https://codepen.io/anon/pen/LaZgvp?editors=0010. This, probably, will led to your next question: "how to position the tooltip over/next to the respective rectangle?" The answer to that question, certainly a duplicate, can be found with a simple search (for instance: and )。
我有这个 d3 项目,我在其中创建了条形图。我有几个条形图,我希望在将鼠标悬停在特定条形图上时显示数据。但是,由于某些原因,尽管数据和 css 存在,但它不显示内容。我将 rects 元素创建为条形和 div 元素,其中包含数据的工具提示 class。请帮忙。
这是我的代码:
chart.selectAll("rect")
.data(json.data)
.enter()
.append("rect")
.attr("x", (d) => xScale(parseDate(formatDate(d[0]))))
.attr("y", (d) => yScale(d[1]))
.attr("height", (d) => height - yScale(d[1]))
.attr("width", xScale.bandwidth())
.attr("class", "rect-col")
.append("div")
.attr("class", "tooltip")
.text((d) => d[0] + d[1])
.on("mouseenter", function (d, i) {
//d3.select(this).style("fill", "#EE9A2F");
d3.select(this).style("visibility", "visible");
})
.on("mouseleave", function (d, i) {
//d3.select(this).style("fill", "#EECF10");
d3.select(this).style("visibility", "visible");
});
我的css:
.tooltip {
text-align: center;
width: 60px;
height: 28px;
padding: 2px;
font: 12px sans-serif;
background: green;
border: 0px;
border-radius: 8px;
pointer-events: none;
color: red;
visibility: hidden;
}
link 到我的代码笔 codepen
简单地说:您不能将<div>
附加到SVG <rect>
。
此处的解决方案是为 div...
创建一个选择const tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip");
... 然后,在矩形的 "mouseenter"
侦听器内:
tooltip.style("visibility", "visible")
.text(d[0] + d[1])
这是更新后的 CodePen,您会在图表下方看到工具提示:https://codepen.io/anon/pen/LaZgvp?editors=0010. This, probably, will led to your next question: "how to position the tooltip over/next to the respective rectangle?" The answer to that question, certainly a duplicate, can be found with a simple search (for instance: