产生平行色带的d3和弦图 [英] d3 chord diagram that produces parallel ribbons
本文介绍了产生平行色带的d3和弦图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用和弦布局,但是我想让和弦平行(以红色覆盖层表示).
我该如何完成?我尝试了不同的排序选项,但似乎都没有产生预期的结果.
代码:
var width = 1000;
var height = 600;
var svg = d3.select("#chart")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width/2 + "," + height/2 + ")");
var matrix5x5 = [
[0, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0]
];
var range5 = ["#848484", "#848484", "#848484", "#848484", "#848484", "#848484", "848484", "848484"];
var chord = d3.layout.chord()
.padding(0.1)
.matrix(matrix5x5);
var fill = d3.scale.ordinal()
.domain(d3.range(range5.length))
.range(range5);
var innerRadius = Math.min(width, height) * .39;
var outerRadius = innerRadius * 1.1;
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));
svg.append("g")
.attr("class", "chord")
.selectAll("path")
.data(chord.chords)
.enter().append("path")
.style("fill", function(d) {
return fill(d.target.index);
})
.attr("d", d3.svg.chord().radius(innerRadius))
.style("opacity", 1);
var range5_artists = ["All", "one", "two", "three", "four", "five", "six", "seven"];
svg.selectAll("text")
.data(chord.groups)
.enter()
.append("text")
.text(function(d) {
return range5_artists[d.index];
})
.each(function(d) { d.angle = (d.startAngle + d.endAngle) / 2; })
.attr("dy", ".35em")
.attr("transform", function(d) {
return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")"
+ "translate(" + (innerRadius + 35) + ")"
+ (d.angle > Math.PI ? "rotate(180)" : "");
})
.style("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; })
.attr("font-size", "11px")
.attr("fill", function(d) {
return range5[d.index];
})
.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);
};
}
解决方案
在这里寻找的是此处.
I currently am working with a chord layout, I would like to have my chords parallel though (illustrated with red overlays).
How do I accomplish this? I tried the different sorting options and none seem to yield the desired results.
code:
var width = 1000;
var height = 600;
var svg = d3.select("#chart")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width/2 + "," + height/2 + ")");
var matrix5x5 = [
[0, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0]
];
var range5 = ["#848484", "#848484", "#848484", "#848484", "#848484", "#848484", "848484", "848484"];
var chord = d3.layout.chord()
.padding(0.1)
.matrix(matrix5x5);
var fill = d3.scale.ordinal()
.domain(d3.range(range5.length))
.range(range5);
var innerRadius = Math.min(width, height) * .39;
var outerRadius = innerRadius * 1.1;
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));
svg.append("g")
.attr("class", "chord")
.selectAll("path")
.data(chord.chords)
.enter().append("path")
.style("fill", function(d) {
return fill(d.target.index);
})
.attr("d", d3.svg.chord().radius(innerRadius))
.style("opacity", 1);
var range5_artists = ["All", "one", "two", "three", "four", "five", "six", "seven"];
svg.selectAll("text")
.data(chord.groups)
.enter()
.append("text")
.text(function(d) {
return range5_artists[d.index];
})
.each(function(d) { d.angle = (d.startAngle + d.endAngle) / 2; })
.attr("dy", ".35em")
.attr("transform", function(d) {
return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")"
+ "translate(" + (innerRadius + 35) + ")"
+ (d.angle > Math.PI ? "rotate(180)" : "");
})
.style("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; })
.attr("font-size", "11px")
.attr("fill", function(d) {
return range5[d.index];
})
.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);
};
}
解决方案
What you're looking for here is the .sortSubGroups()
function. You want to sort based on index, but this information isn't passed to the comparator. So we use a dirty trick that relies on the order of evaluation and always return 1.
Result here.
这篇关于产生平行色带的d3和弦图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文