Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active February 9, 2016 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbostock/9748706 to your computer and use it in GitHub Desktop.
Save mbostock/9748706 to your computer and use it in GitHub Desktop.
Coastal Graph Distance II
license: gpl-3.0
We can make this file beautiful and searchable if this error is corrected: No tabs found in this TSV file in line 0.
id
53073
53055
53057
53029
53009
53061
53031
53035
53033
53045
53027
53053
53067
53049
53069
41007
41057
23029
23009
41041
23027
23011
23013
23015
41039
23005
23023
41019
23031
41011
33017
33015
41015
25009
25017
25025
25021
25023
25005
25001
44007
6015
44001
44003
9011
44005
9001
44009
9007
9009
36079
25007
6023
25019
36119
36087
36103
34003
36005
36059
34013
36061
34017
36081
36047
34039
36085
34023
34025
34005
34029
42101
42045
6045
34007
34015
10003
34033
34001
24015
24025
24005
34011
24029
24510
10001
34009
24035
24003
24011
24033
51059
11001
10005
24041
51153
51013
6055
6097
24009
24019
24017
51179
24045
6095
24037
24047
51099
6041
24039
51193
51057
51159
6013
51001
51133
51097
51101
6001
51103
6075
51119
6081
51127
51073
51131
51115
51036
6085
51095
51199
51149
6087
51181
51700
51093
51650
51175
51710
51810
51800
6053
51550
37073
37053
37029
37091
37139
37143
37041
37015
37055
37177
37187
6079
37013
37095
37049
37137
37103
37031
6083
37133
6111
6037
37141
37129
37019
45051
6059
45043
45015
6073
45029
45053
45013
13029
13051
13179
13191
13127
1003
1097
13039
12033
12113
12091
12131
12089
28059
22103
28047
12065
28045
12031
12005
12123
12129
12109
22051
12045
22071
48245
48201
22087
22113
22045
22023
12037
22101
22057
22075
48071
22109
12035
48167
48039
12075
12127
48239
48321
48469
12017
12009
48057
12053
48391
12101
48007
48409
12103
12057
48355
12061
12081
48273
12111
12115
48261
12085
12015
12099
12071
48489
12021
48061
12011
12086
12087
12029
45019
15001
15007
15009
15009
15009
15009
15003
15007
2016
2013
2130
2060
2070
2164
2150
2110
2280
2232
2100
2220
2270
2050
2170
2020
2261
2122
2282
2185
2188
2180
2201
2201
2201
2201
2201
2201
2201
2201
2201
2201
2201
2201
2201
2201
2201
2201
72125
72003
72097
72065
72055
72133
72027
72111
72091
72013
72145
72031
72061
72075
72143
72011
72079
72023
72109
72005
72059
72021
72123
72115
72017
72127
72057
72153
72113
72071
72151
72137
78030
72089
72087
72095
72119
72103
72053
72037
72069
72147
78010
72051
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #222;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="//d3js.org/queue.v1.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.albersUsa()
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var color = d3.scale.ordinal()
.domain(d3.range(9).reverse())
.range(["#ffffd9","#edf8b1","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
queue()
.defer(d3.json, "/mbostock/raw/4090846/us.json")
.defer(d3.tsv, "coastal-counties.tsv")
.await(ready);
function ready(error, us, coastals) {
if (error) throw error;
var counties = topojson.feature(us, us.objects.counties),
neighbors = topojson.neighbors(us.objects.counties.geometries),
coastals = d3.set(coastals.map(function(d) { return d.id; })),
nexts = [],
nexts2 = [],
distance = 0;
counties.features.forEach(function(county, i) {
if (coastals.has(county.id)) nexts.push(county);
county.distance = Infinity;
county.neighbors = neighbors[i].map(function(j) { return counties.features[j]; });
});
while (nexts.length) {
nexts.forEach(function(county) {
if (county.distance > distance) {
county.distance = distance;
county.neighbors.forEach(function(neighbor) { nexts2.push(neighbor); });
}
});
nexts = nexts2, nexts2 = [], ++distance;
}
var county = svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(d3.nest()
.key(function(d) { return d.distance; })
.entries(counties.features)
.map(function(e) { return {type: "FeatureCollection", features: e.values, distance: +e.key}; }))
.enter().append("path")
.attr("d", path);
d3.timer(function(elapsed) {
county.style("fill", function(d) { return d3.hsl(d.distance * 10 - elapsed / 10, 1, .5); });
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment