Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active October 22, 2019 08:38
Show Gist options
  • Save mbostock/4062006 to your computer and use it in GitHub Desktop.
Save mbostock/4062006 to your computer and use it in GitHub Desktop.
Chord Diagram
license: gpl-3.0
height: 960
border: no
redirect: https://observablehq.com/@d3/d3-chord-diagram
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.group-tick line {
stroke: #000;
}
.ribbons {
fill-opacity: 0.67;
}
</style>
<svg width="960" height="960"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var matrix = [
[11975, 5871, 8916, 2868],
[ 1951, 10048, 2060, 6171],
[ 8010, 16145, 8090, 8045],
[ 1013, 990, 940, 6907]
];
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
outerRadius = Math.min(width, height) * 0.5 - 40,
innerRadius = outerRadius - 30;
var formatValue = d3.formatPrefix(",.0", 1e3);
var chord = d3.chord()
.padAngle(0.05)
.sortSubgroups(d3.descending);
var arc = d3.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var ribbon = d3.ribbon()
.radius(innerRadius);
var color = d3.scaleOrdinal()
.domain(d3.range(4))
.range(["#000000", "#FFDD89", "#957244", "#F26223"]);
var g = svg.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.datum(chord(matrix));
var group = g.append("g")
.attr("class", "groups")
.selectAll("g")
.data(function(chords) { return chords.groups; })
.enter().append("g");
group.append("path")
.style("fill", function(d) { return color(d.index); })
.style("stroke", function(d) { return d3.rgb(color(d.index)).darker(); })
.attr("d", arc);
var groupTick = group.selectAll(".group-tick")
.data(function(d) { return groupTicks(d, 1e3); })
.enter().append("g")
.attr("class", "group-tick")
.attr("transform", function(d) { return "rotate(" + (d.angle * 180 / Math.PI - 90) + ") translate(" + outerRadius + ",0)"; });
groupTick.append("line")
.attr("x2", 6);
groupTick
.filter(function(d) { return d.value % 5e3 === 0; })
.append("text")
.attr("x", 8)
.attr("dy", ".35em")
.attr("transform", function(d) { return d.angle > Math.PI ? "rotate(180) translate(-16)" : null; })
.style("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; })
.text(function(d) { return formatValue(d.value); });
g.append("g")
.attr("class", "ribbons")
.selectAll("path")
.data(function(chords) { return chords; })
.enter().append("path")
.attr("d", ribbon)
.style("fill", function(d) { return color(d.target.index); })
.style("stroke", function(d) { return d3.rgb(color(d.target.index)).darker(); });
// Returns an array of tick angles and values for a given group and step.
function groupTicks(d, step) {
var k = (d.endAngle - d.startAngle) / d.value;
return d3.range(0, d.value, step).map(function(value) {
return {value: value, angle: value * k + d.startAngle};
});
}
</script>
@raja1985
Copy link

Hi, I'm trying to play with this (http://mbostock.github.io/d3/talk/20111116/bundle.html) example with my own data. which has csv file contents as below:
source_proj ,issue_link, destination_proj ,dest_issuetype
[Inactive] Eikon Collab MM - Insider iPhone,Bug to Story,[Inactive] Eikon Collab MM - Insider iPhone,Story
[Inactive] Eikon Collab MM - Insider iPhone,jira_subtask_link,[Inactive] Eikon Collab MM - Insider iPhone,Acceptance Test
[Inactive] Eikon Collab MM - Insider iPhone,jira_subtask_link,[Inactive] Eikon Collab MM - Insider iPhone,Task

an so on.

can you guide me how to implement this graph with these four attribute. how to have relationship with them?

source_proj ,issue_link, destination_proj ,dest_issuetype

will this graph suit my requirement?

@phoenixzw
Copy link

Hi, I use the Chord Diagram to visualize my own data, but I find a bug. This is the description figure:
https://lh3.googleusercontent.com/tn2BlSPfXgKgZ8FpswcwSeM7oTzwUGUpcjacVU_W70Y=w536-h873-no
From this figure, we see that the display has errors. I just copy the original code and replace the matrix array. Could someone help me solve this issue? Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment