在 svg 中为行添加悬停工具提示
Add hover tooltip for line in svg
我正在尝试为 SVG 中的两行添加简单的悬停文本。我已将其作为 'title' 包含在下面的代码中,但是当我将鼠标悬停在任何一行上时都没有任何反应。因为他们交叉,所以我并不特别关心当你将鼠标悬停在十字路口时它说的是什么。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
//Width and height
var w = 500;
var h = 300;
var padding = 30;
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)
.attr("background-color", "green")
svg.append("rect")
.attr("width", w)
.attr("height", h)
.attr("fill", "green");
svg.append("line")
.attr("x1", 30)
.attr("y1", 40)
.attr("x2", 80)
.attr("y2", 90)
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("title", "This is a blue line");
svg.append("line")
.attr("x1", 30)
.attr("y1", 90)
.attr("x2", 80)
.attr("y2", 40)
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr("title", "This is a red line");
</script>
</body>
</html>
对于 SVG,您需要一个 title 子元素,而不是 title 属性。
svg.append("line")
.attr("x1", 30)
.attr("y1", 40)
.attr("x2", 80)
.attr("y2", 90)
.attr("stroke", "blue")
.attr("stroke-width", 2)
.append("title").text("This is a blue line");
我正在尝试为 SVG 中的两行添加简单的悬停文本。我已将其作为 'title' 包含在下面的代码中,但是当我将鼠标悬停在任何一行上时都没有任何反应。因为他们交叉,所以我并不特别关心当你将鼠标悬停在十字路口时它说的是什么。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
//Width and height
var w = 500;
var h = 300;
var padding = 30;
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)
.attr("background-color", "green")
svg.append("rect")
.attr("width", w)
.attr("height", h)
.attr("fill", "green");
svg.append("line")
.attr("x1", 30)
.attr("y1", 40)
.attr("x2", 80)
.attr("y2", 90)
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("title", "This is a blue line");
svg.append("line")
.attr("x1", 30)
.attr("y1", 90)
.attr("x2", 80)
.attr("y2", 40)
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr("title", "This is a red line");
</script>
</body>
</html>
对于 SVG,您需要一个 title 子元素,而不是 title 属性。
svg.append("line")
.attr("x1", 30)
.attr("y1", 40)
.attr("x2", 80)
.attr("y2", 90)
.attr("stroke", "blue")
.attr("stroke-width", 2)
.append("title").text("This is a blue line");