D3 chord diagram - selecting individual chords

818 views Asked by At

I am working with a chord diagram, right now I am only able to select the text label and the grey border that the chords connect to.

I would like to select individual chords however, when I add my mouse function, it selects a random one in the diagram.

enter image description here

//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);
    };
}
2

There are 2 answers

0
jfroy On

The following works fine for me with d3 version 6.5. Note the differences in the event handler function signature, and in the filtering condition:

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);
  };
}

Parameter i in OP should be the chord index, whereas the chord source and target indexes refer to chord groups.

0
Haris ur Rehman On

I ran into the same issue and it was selector problem in fade function. The function should be like below. Note the 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);
    };
}