Skip to content

Instantly share code, notes, and snippets.

@matt-bernhardt
Last active August 29, 2015 14:14
Show Gist options
  • Save matt-bernhardt/a97d083a95099ab69c69 to your computer and use it in GitHub Desktop.
Save matt-bernhardt/a97d083a95099ab69c69 to your computer and use it in GitHub Desktop.
Better Departmental Chord Diagram

Please, let this work. Now with faked CSV in the right format.

[[0, 112, 212, 130, 178, 182, 40, 64, 0, 196, 14, 4, 92, 82, 156, 8, 54, 46, 4, 28, 0, 0, 0, 28, 0, 0, 0, 0, 36, 16, 16, 0, 12, 4, 0, 0, 0, 56], [112, 0, 132, 26, 0, 30, 0, 88, 0, 12, 20, 8, 24, 0, 8, 0, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 6, 4, 0, 0, 2, 0, 0, 2], [212, 132, 0, 92, 38, 46, 18, 58, 0, 42, 4, 94, 70, 8, 8, 0, 80, 4, 0, 42, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 272, 0, 0, 0, 0, 0, 38], [130, 26, 92, 0, 138, 126, 6, 80, 0, 26, 30, 2, 22, 8, 10, 0, 46, 6, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 6, 10, 6, 2, 0, 0, 2, 0, 0, 2], [178, 0, 38, 138, 0, 258, 26, 182, 0, 126, 70, 2, 322, 68, 2, 0, 182, 46, 2, 26, 0, 0, 0, 0, 0, 0, 0, 0, 16, 2, 46, 4, 0, 0, 0, 0, 0, 44], [182, 30, 46, 126, 258, 0, 44, 108, 0, 48, 48, 18, 34, 46, 30, 0, 42, 26, 2, 6, 0, 0, 0, 2, 0, 0, 0, 0, 14, 30, 4, 2, 8, 0, 0, 2, 8, 10], [40, 0, 18, 6, 26, 44, 0, 14, 0, 32, 16, 4, 6, 0, 14, 0, 8, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 14], [64, 88, 58, 80, 182, 108, 14, 0, 0, 158, 18, 2, 82, 2, 4, 0, 108, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 4, 6, 0, 0, 0, 0, 0, 66], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [196, 12, 42, 26, 126, 48, 32, 158, 0, 0, 8, 0, 128, 152, 6, 0, 30, 26, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 16, 0, 0, 0, 0, 0, 0, 102], [14, 20, 4, 30, 70, 48, 16, 18, 0, 8, 0, 38, 46, 2, 10, 0, 8, 0, 14, 8, 0, 0, 0, 0, 0, 0, 0, 0, 4, 32, 12, 0, 4, 0, 0, 0, 4, 0], [4, 8, 94, 2, 2, 18, 4, 2, 0, 0, 38, 0, 16, 0, 28, 0, 20, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 36, 0], [92, 24, 70, 22, 322, 34, 6, 82, 0, 128, 46, 16, 0, 118, 0, 0, 228, 2, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 6, 30, 72, 20, 0, 0, 0, 0, 4, 14], [82, 0, 8, 8, 68, 46, 0, 2, 0, 152, 2, 0, 118, 0, 2, 12, 6, 44, 0, 20, 0, 0, 0, 0, 2, 0, 0, 0, 4, 2, 6, 2, 0, 0, 0, 0, 0, 28], [156, 8, 8, 10, 2, 30, 14, 4, 0, 6, 10, 28, 0, 2, 0, 0, 2, 4, 6, 0, 0, 0, 0, 8, 0, 0, 0, 0, 10, 80, 0, 0, 0, 0, 0, 0, 0, 2], [8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0], [54, 2, 80, 46, 182, 42, 8, 108, 0, 30, 8, 20, 228, 6, 2, 0,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, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 4, 36, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [56, 2, 38, 2, 44, 10, 14, 66, 0, 102, 0, 0, 14, 28, 2, 0, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 2, 0, 0, 0, 0, 0, 0]]
name latitude longitude population color
Excelsior 37.7244 -122.421 0.083884 #E41A1C
Crocker Amazon 37.7107 -122.4372 0.058439 #FFFF33
Marina 37.8021 -122.4369 0.40364 #FF7F00
Nob Hill 37.793 -122.416 0.33259 #FF7F00
North Beach 37.8045 -122.4076 0.28784 #FF7F00
Pacific Heights 37.7924 -122.4352 0.33382 #FF7F00
Presidio Heights 37.7868 -122.4538 0.19965 #FF7F00
Russian Hill 37.8014 -122.4182 0.34165 #FF7F00
Visitacion Valley 37.7144 -122.4113 0.064774 #999999
Diamond Heights 37.7423 -122.4423 0.074148 #984EA3
Glen Park 37.7378 -122.4316 0.1226 #984EA3
Inner Sunset 37.7584 -122.4654 0.18995 #984EA3
Lakeshore 37.7225 -122.4885 0.092602 #984EA3
Outer Mission 37.7239 -122.4439 0.10242 #984EA3
West Of Twin Peaks 37.7373 -122.4589 0.12473 #984EA3
Bayview 37.73 -122.3855 0.10309 #377EB8
Bernal Heights 37.7399 -122.4169 0.20043 #377EB8
Castro 37.7624 -122.4348 0.34092 #377EB8
Haight 37.7692 -122.4463 0.29559 #377EB8
Mission 37.7589 -122.4153 0.42583 #377EB8
Noe 37.7493 -122.433 0.25158 #377EB8
Parkside 37.7411 -122.4892 0.092271 #377EB8
Potrero 37.7583 -122.393 0.26353 #377EB8
South Of Market 37.7764 -122.3994 0.55316 #377EB8
Twin Peaks 37.752 -122.45 0.13105 #377EB8
Western Addition 37.7804 -122.4332 0.41192 #377EB8
Inner Richmond 37.7802 -122.4652 0.21194 #4DAF4A
Outer Richmond 37.778 -122.4928 0.18916 #4DAF4A
Outer Sunset 37.7553 -122.4938 0.12363 #4DAF4A
Seacliff 37.7841 -122.5009 0.091937 #4DAF4A
Golden Gate Park 37.7689 -122.4828 0.15941 #4DAF4A
Chinatown 37.7941 -122.407 0.24273 #F781BF
Downtown 37.7835 -122.4158 0.44235 #F781BF
Financial District 37.7915 -122.3988 0.49915 #F781BF
Ocean View 37.7178 -122.4622 0.076638 #A65628
<head>
<title>Better Chord Diagram</title>
</head>
<body>
<script src="http://d3js.org/d3.v2.min.js?2.8.1"></script>
<script>
var width = 720,
height = 720,
outerRadius = Math.min(width, height) / 2 - 10,
innerRadius = outerRadius - 24;
var formatPercent = d3.format(".1%");
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var layout = d3.layout.chord()
.padding(.04)
.sortSubgroups(d3.descending)
.sortChords(d3.ascending);
var path = d3.svg.chord()
.radius(innerRadius);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("id", "circle")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
svg.append("circle")
.attr("r", outerRadius);
d3.csv("departments.csv", function(cities) {
d3.json("department_matrix.json", function(matrix) {
// Compute the chord layout.
layout.matrix(matrix);
// Add a group per neighborhood.
var group = svg.selectAll(".group")
.data(layout.groups)
.enter().append("g")
.attr("class", "group")
.on("mouseover", mouseover);
// Add a mouseover title.
group.append("title").text(function(d, i) {
return cities[i].name + ": " + formatPercent(d.value) + " of origins";
});
// Add the group arc.
var groupPath = group.append("path")
.attr("id", function(d, i) { return "group" + i; })
.attr("d", arc)
.style("fill", function(d, i) { return cities[i].color; });
// Add a text label.
var groupText = group.append("text")
.attr("x", 6)
.attr("dy", 15);
groupText.append("textPath")
.attr("xlink:href", function(d, i) { return "#group" + i; })
.text(function(d, i) { return cities[i].name; });
// Remove the labels that don't fit. :(
groupText.filter(function(d, i) { return groupPath[0][i].getTotalLength() / 2 - 16 < this.getComputedTextLength(); })
.remove();
// Add the chords.
var chord = svg.selectAll(".chord")
.data(layout.chords)
.enter().append("path")
.attr("class", "chord")
.style("fill", function(d) { return cities[d.source.index].color; })
.attr("d", path);
// Add an elaborate mouseover title for each chord.
chord.append("title").text(function(d) {
return cities[d.source.index].name
+ " → " + cities[d.target.index].name
+ ": " + formatPercent(d.source.value)
+ "\n" + cities[d.target.index].name
+ " → " + cities[d.source.index].name
+ ": " + formatPercent(d.target.value);
});
function mouseover(d, i) {
chord.classed("fade", function(p) {
return p.source.index != i
&& p.target.index != i;
});
}
});
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment