javascript - 使用 mouseover 和 mouseout 时如何在多个折线图中显示每个变量名称?(d3.js)
问题描述
我想在鼠标悬停该行时显示每个变量名称,它是文本,我使用 mouseover 和 mouseout 方法来触发它,但它没有在折线图上显示任何作为变量名称的文本,而且它也不会弹出在控制台上显示任何错误消息。
var margin = {top: 10, right: 80, bottom: 30, left: 60},
width = 960 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
svg.selectAll(".line")
.data(sumstat)
.enter()
.append("path")
.attr("fill", "none")
.attr("stroke", function(d){ return color(d.key) })
.attr("stroke-width", 1.5)
.attr("d", function(d){
return d3.line()
.x(function(d) { return x(d.Year); })
.y(function(d) { return y(+d.Price); })
(d.values)
})
.on("mouseover", function(d, i) {
svg.append("text")
.attr("class", "title-text")
.style("fill", function(d){ return color(d.key) }) //it will return the different colors based on different variable names
.text(d.key) //it will return the different variable names
.attr("text-anchor", "middle")
.attr("x", (width-margin)/2)
.attr("y", 5);
})
.on("mouseout", function(d) {
svg.select(".title-text").remove();
})
我希望变量名称,例如作为文本的城市名称,当我使用鼠标悬停这条线时会显示在折线图上,如果鼠标移动到同一折线图中的另一条线,那么它将显示当前变量前一个变量名的名称将消失。因此,我认为在这种情况下它可能可以使用“mouseover”和“mouseout”方法,但是现在当鼠标悬停每一行时它不能显示任何内容并且也不会弹出任何错误消息。
解决方案
推荐阅读
- python - 如何将神经网络转换为卷积神经网络?
- python - sklearn 惊喜 .predict 方法中 r_ui(真实评级)参数的用途是什么?
- r - 需要计算 R # 列中预先指定的单词
- regex - 使用正则表达式从字符串中提取电子邮件地址
- c++ - 使用 C++ 添加两个数组的运算符重载 (+)
- c++ - 为什么在 C++ 中使用链表来完成?(像堆栈一样处理它们)
- java - 如何在我的 antlr4 语法中实现将写入域但不耦合域服务的方法?
- python - 如何在否定的lookbehind catch term regex之间捕获未知数量的单词?
- html - CSS 'overflow-wrap' 在 React.js 中不起作用
- sql - 产品与库存和用户之间的关系