javascript - 在 D3 中鼠标悬停时触发两个单独的事件

标签 javascript d3.js

我有一个 D3 条形图,关联的数据点在每个条形顶部显示为文本。我想仅在鼠标悬停时显示文本,并使栏具有不同的填充颜色。因此,本质上,在鼠标悬停时,必须将栏样式设置为具有不同的填充颜色,并且文本不透明度应变为 1(从“0”)。

我在鼠标悬停时无法影响两个单独的事件。为了使用 d3.select(this).attr(index_value),我为这两个元素赋予了一个 index_value 属性。但我的鼠标悬停功能不起作用。我不知道为什么。这是我的相关代码部分。

条形图

svg.selectAll(".bar")
    .data(data)
  .enter().append("rect")
    .attr('data-value', function(d){return d[region]})
    .attr("x", function(d) { return x(d.year); })
    .attr("width", x.rangeBand())
    .attr("y", function(d) { return y(d[region]); })
    .attr("height", function(d) { return height - y(d[region]); })
    .attr("fill", color)
    .attr("index_year", function(d, i) { return "index-" + d.year; })
    .attr("class", function(d){return "bar " + "bar-index-" + d.year;})
    .attr("color_value", color)
    .on('mouseover', synchronizedMouseOver)
    .on("mouseout", synchronizedMouseOut);

文字叠加

svg.selectAll(".bartext")
   .data(data)
   .enter()
   .append("text")
   .attr("text-anchor", "middle")
   .attr("x", function(d,i) {
        return x(d.year)+x.rangeBand()/2;
    })
    .attr("y", function(d,i) {
        return height - (height - y(d[region])) - yTextPadding;
    })
    .text(function(d){
         return d3.format(prefix)(d3.round(d[region]));
    })
    .attr("index_year", function(d, i) { return "index-" + d.year; })
    .attr("class", function(d){return "bartext " + "label-index-" + d.year;})
    .on("mouseover", synchronizedMouseOver)
    .on("mouseout", synchronizedMouseOut);

以及鼠标悬停功能

var synchronizedMouseOver = function() {
      var bar = d3.select(this);
      console.log(bar);
      var indexValue = bar.attr("index_year");

      var barSelector = "." + "bar " + "bar-" + indexValue;
      var selectedBar = d3.selectAll(barSelector);
      selectedBar.style("fill", "#f7fcb9");

      var labelSelector = "." + "bartext " + "label-" + indexValue;
      var selectedLabel = d3.selectAll(labelSelector);
      selectedLabel.style("opacity", "1");

      };

最佳答案

这可以通过简化你的听众来实现。您不需要向矩形和文本添加监听器。只需将它们添加到矩形中即可。以下是简化的监听器:

function synchronizedMouseOver(d) {
    var bar = d3.select(this)
        .style("fill","red");

    var text = d3.select(".label-index-" + d.year)
        .style("opacity","1");
};

function synchronizedMouseOut(d) {
    var bar = d3.select(this)
        .style("fill",color);

    var text = d3.select(".label-index-" + d.year)
        .style("opacity","0");        
};

这里的两个 friend 是 thisd,分别是矩形的 DOM 元素及其数据节点。

这是一个FIDDLE与您想要的行为。

关于javascript - 在 D3 中鼠标悬停时触发两个单独的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23436522/

相关文章:

jquery - 销售管道图

javascript - 如何在 javascript 中进行漂亮而简单的文件上传?

javascript - 为什么我在 typescript 中没有收到有关 StrictNullChecks 的警告

javascript - Typescript - 在函数返回 block 中使用 then 的值

javascript - 使用数据点击打开 html

javascript - 基本 D3.js : how to bind data with other attributes to elements?

javascript - 在 jquery 中上下移动元素

javascript - 为什么我不能使用 map 来收集 Javascript 中对象数组的属性?

javascript - 构造一个介于开始日期和结束日期之间的日期数组。 (d3.js)

javascript - 使用 D3 为 svg 填充背景图像