-1
将鼠标悬停在该城市的圆上时,以下代码会生成一个带城市名称的文本框。如何删除鼠标悬停效果并在地图最初加载时将其永久保留?谢谢。如何在D3中保留文本框
// Place Capital Cities the Map
svgContainer.selectAll("circle")
.data(capitalCities)
.enter().append("circle")
.attr("id", function(d, i) { return d.name.toLowerCase(); })
.attr("cx", function(d, i) { return projectionType(d.coordinates)[0]; })
.attr("cy", function(d, i) { return projectionType(d.coordinates)[1]; })
.attr("r", "10")
.attr("stroke", "black")
.attr("stroke-width", "2px")
.style("fill", "#FFFF00")
.text("City Name:" + "<br />" + cityName);
// Mouse Over a City tells us the name
d3.selectAll("circle").on("mouseover", function() {
// Select the circle being moused over
circle = d3.select(this);
// Extract the name of the city from the bound data
cityName = circle.data()[0].name;
// Update and place the tooltip with the city name.
div.html("City Name:" + "<br />" + cityName)
.style("left", (d3.event.pageX + 9) + "px")
.style("top", (d3.event.pageY - 43) + "px")
.style("display", "inline");
})
// Mouse Out of a City removes the text
d3.selectAll("circle").on("mouseout", function() {
// Make the tooltip invisible
div.style("display", "none");
})
});
您可以只追加一个文本元素,https://www.dashingd3js.com/svg-text-element – TimCodes
感谢您回复TimCodes。我编辑了上述帖子,以反映我进行更改的位置,但现在根本不显示任何文本。 – heisenberg