2

我正在使用和弦图,现在我只能选择和弦连接的文本标签和灰色边框。

我想选择单个和弦但是,当我添加鼠标功能时,它会在图表中选择一个随机的。

在此处输入图像描述

//works
svg.append("g")
        .selectAll("path")
        .data(chord.groups)
        .enter().append("path")
        .style("fill", function(d) {
            return fill(d.index);
        })
        .style("stroke", function(d) {
            return fill(d.index);
        })
        .attr("d", d3.svg.arc().innerRadius(innerRadius).outerRadius(outerRadius))
        .on("mouseover", fade(.1))
        .on("mouseout", fade(1));

//doesn't work w/ mouseover
svg.append("g")
        .attr("class", "chord")
        .selectAll("path")
        .data(chord.chords)
        .enter().append("path")
        .style("fill", function(d) {
            //console.log(d.target.subindex)
            return fill(d.target.subindex);
        })
        .attr("d", d3.svg.chord().radius(innerRadius))
        //.style("opacity", 1)
        .on("mouseover", fade(.1))
        .on("mouseout", fade(1)); 

function fade(opacity) {
    return function(g, i) {
        svg.selectAll("g.chord path")
                .filter(function(d) {                   
                    return d.source.index != i && d.target.index != i;
                 })
                .transition()
                .style("opacity", opacity);
    };
}
4

2 回答 2

0

我遇到了同样的问题,这是fade功能中的选择器问题。该功能应如下所示。注意svg.selectAll("path.chord")

function fade(opacity) {
    return function(g, i) {
        svg.selectAll("path.chord")
            .filter(function(d) {                   
                return d.source.index != i && d.target.index != i;
             })
            .transition()
            .style("opacity", opacity);
    };
}
于 2019-08-19T19:55:11.190 回答
0

以下对我来说适用于 d3 6.5 版。请注意事件处理函数签名和过滤条件的差异:

function fade(opacity) {
  return function (ev, d) {
    svg.selectAll("g.chord path")
      .filter(function(cd) {                   
        return cd.source.index != d.source.index || cd.target.index != d.target.index;
      })
      .transition()
      .style("opacity", opacity);
  };
}

OP中的参数i应该是和弦索引,而和弦源和目标索引是指和弦

于 2021-02-03T21:32:37.883 回答