Skip to content

Instantly share code, notes, and snippets.

@yaph
Forked from mbostock/.block
Last active August 29, 2015 13:57
Show Gist options
  • Save yaph/9748487 to your computer and use it in GitHub Desktop.
Save yaph/9748487 to your computer and use it in GitHub Desktop.
Fork of mbostock's coastal counties map

This map shows the graph distance of each county from the Pacific or Atlantic coast; it is a recreation of a map posted to /r/dataisbeautiful using TopoJSON. Coastal counties are dark blue, while counties nine or more counties away from the coast are light yellow. (I opted not to reuse the original’s cycling color scale.)

See also the underlying graph.

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>
.counties {
fill: #ccc;
}
.county-borders {
fill: none;
stroke: #ccc;
stroke-width: .5px;
stroke-linejoin: round;
stroke-linecap: round;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://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 colors = ['#fff9ee', '#fff6ed', '#ffedde', '#ffe3d0', '#ffdac3', '#ffd0b5', '#ffc5a8', '#ffbb9b', '#ffb18e', '#ffa581', '#ff9b74', '#ff8f67', '#ff825a', '#ff764d', '#ff663f', '#ff5731', '#ff4222', '#ff270f', '#fa0000', '#ed0001', '#e00001', '#d40002', '#c80002', '#ba0002', '#af0002', '#a30002', '#970002', '#8c0001', '#800000', '#000000'];//.reverse();
var color = d3.scale.ordinal()
.domain(d3.range(colors.length).reverse())
.range(colors);
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) return console.error(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;
}
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(counties.features)
.enter().append("path")
.style("fill", function(d) { return color(Math.min(colors.length, d.distance)); })
.attr("d", path);
svg.append("path")
.attr("class", "county-borders")
.datum(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b; }))
.attr("d", path);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment