d3 - 将鼠标悬停在图例上以突出显示相应的数据

时间:2014-01-20 13:17:43

标签: javascript d3.js

使用d3成功创建了热图。

这是FIDDLE

我对使用d3的mouseover事件有一些基本的想法。但现在我想向前迈进一步。

这就是我要找的东西。当我将鼠标悬停在图例上时,我希望图表中突出显示悬停的图例各自的数据。

有人可以帮我实现吗?

1 个答案:

答案 0 :(得分:9)

您没有将数据绑定到图例,这使得此任务变得更加困难,但您仍然可以相当轻松地完成此任务。我们的想法是将填充颜色定义的类分配给rect元素,然后在鼠标悬停处理程序中进行相应的选择。代码看起来像这样。

// for the rectangles
.attr("class", function(d) {  
  return "hour bordered " + "color-" + colorScale(d.value).substring(1);
})

// for the legend
.on("mouseover", function(d, i) {
  svg.selectAll("rect.color-" + colors[i].substring(1)).style("stroke", "blue");
})
.on("mouseout", function(d, i) {
  svg.selectAll("rect.color-" + colors[i].substring(1)).style("stroke", "white");
});

完整示例here

相关问题