首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >D3和弦图表-选择单个和弦

D3和弦图表-选择单个和弦
EN

Stack Overflow用户
提问于 2014-01-07 00:33:47
回答 2查看 726关注 0票数 2

我正在处理一个和弦图表,现在我只能选择文本标签和和弦连接到的灰色边框。

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

代码语言:javascript
复制
//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);
    };
}
EN

回答 2

Stack Overflow用户

发布于 2019-08-20 03:55:11

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

代码语言:javascript
复制
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);
    };
}
票数 0
EN

Stack Overflow用户

发布于 2021-02-04 05:32:38

下面的代码适用于我在d3版本6.5上的工作。注意事件处理函数签名和过滤条件的区别:

代码语言:javascript
复制
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应该是chord索引,而chord源和目标索引指的是chord组。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20954623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档