Skip to content

Instantly share code, notes, and snippets.

@darrenjaworski
Last active February 15, 2017 13:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenjaworski/ab598d3e59b66825f043 to your computer and use it in GitHub Desktop.
Save darrenjaworski/ab598d3e59b66825f043 to your computer and use it in GitHub Desktop.
Textured choropleth

These textures are a lot of fun. Here is a choropleth using a few predefined textures on some random data.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.county-border {
fill: none;
stroke: #333;
}
.state-border {
fill: none;
stroke: #333;
}
svg {
display: block;
margin: auto;
font: 10px sans-serif;
}
body {
margin: 0;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="http://riccardoscalco.github.io/textures/textures.min.js"></script>
<script>
var width = 960;
var height = 500;
var fill = d3.scale.threshold()
.domain([0, 20, 75, 200, 500])
.range( [
textures.lines(),
textures.lines().size(4).strokeWidth(1),
textures.lines().size(8).lighter(),
textures.lines().thicker(),
textures.lines().thinner().strokeWidth(1),
textures.lines().heavier(8).thinner(1.5)
] );
var path = d3.geo.path()
.projection(null);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("/darrenjaworski/raw/8916260d4866ad2646e2/ok-counties.json", function(error, ok) {
if (error) throw error;
var counties = topojson.feature(ok, ok.objects.counties);
fill.range().forEach(function(d){
svg.call( d )
})
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(counties.features)
.enter()
.append("path")
.attr('class', 'county')
.attr('fill', function(){ return fill( Math.random() * 600 ).url(); })
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(ok, ok.objects.counties, function(a, b) { return a !== b; }))
.attr("class", "county-border")
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(ok, ok.objects.counties, function(a, b) { return a === b; }))
.attr("class", "state-border")
.attr("d", path);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment