我正在处理一个和弦图表,现在我只能选择文本标签和和弦连接到的灰色边框。
我想选择单独的和弦,但是,当我添加鼠标功能时,它会在图表中随机选择一个和弦。

//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);
};
}发布于 2019-08-20 03:55:11
我遇到了同样的问题,这是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);
};
}发布于 2021-02-04 05:32:38
下面的代码适用于我在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应该是chord索引,而chord源和目标索引指的是chord组。
https://stackoverflow.com/questions/20954623
复制相似问题