Skip to content

Instantly share code, notes, and snippets.

@vickygisel
Last active December 19, 2016 19:37
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 vickygisel/5b3ad030cb32a4e9c286de17ef4c3333 to your computer and use it in GitHub Desktop.
Save vickygisel/5b3ad030cb32a4e9c286de17ef4c3333 to your computer and use it in GitHub Desktop.
ky
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
.caption {
font-weight: bold;
}
.key path {
display: none;
}
.key line {
stroke: #000;
shape-rendering: crispEdges;
}
.county {
fill: #eee;
}
.county-border {
fill: none;
stroke: #fff;
}
.state-border {
fill: none;
stroke: #333;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.geo.projection.v0.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 500,
projection = 0;
var color = d3.scale.threshold()
.domain([30, 60, 120, 360])
.range(['#f0f9e8','#bae4bc','#7bccc4','#43a2ca','#0868ac']);
// A position encoding for the key only.
var x = d3.scale.linear()
.domain([0, 390])
.range([0, 240]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(13)
.tickValues(color.domain());
projection = d3.geo.transverseMercator()
.center([2.5, -38.5])
.rotate([66, 0])
.scale((height * 56.5) / 33)
.translate([(width / 2), (height / 2)]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var g = svg.append("g")
.attr("class", "key")
.attr("transform", "translate(40,40)");
g.selectAll("rect")
.data(color.range().map(function(d, i) {
return {
x0: i ? x(color.domain()[i - 1]) : x.range()[0],
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1],
z: d
};
}))
.enter().append("rect")
.attr("height", 8)
.attr("x", function(d) { return d.x0; })
.attr("width", function(d) { return d.x1 - d.x0; })
.style("fill", function(d) { return d.z; });
g.call(xAxis).append("text")
.attr("class", "caption")
.attr("y", -6)
.text("Population per square mile");
d3.json("provincia.json", function(error, ky) {
if (error) throw error;
var counties = topojson.feature(ky, ky.objects.counties);
svg.append("g")
.attr("class", "county")
.selectAll("path")
.data(counties.features)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) { return color(d.density = d.properties.population / d.properties.area); })
.append("title")
.text(function(d) { return d.properties.name + ": " + d.density.toFixed(0) + "/mi.²"; });
svg.append("path")
.datum(topojson.mesh(ky, ky.objects.counties, function(a, b) { return a !== b; }))
.attr("class", "county-border")
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(ky, ky.objects.counties, function(a, b) { return a === b; }))
.attr("class", "state-border")
.attr("d", path);
});
</script>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment