A simple simple way to represent percentages on a map. Data: Percentage of residents who rent.
Last active
January 28, 2016 19:30
-
-
Save mtaptich/8f3c3a187656be8d5ae9 to your computer and use it in GitHub Desktop.
Grid Map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <body> | |
| <style type="text/css"> | |
| .county-border { | |
| fill: none; | |
| stroke: #34495e; | |
| stroke-opacity: .35; | |
| } | |
| .land { | |
| fill: #ecf0f1; | |
| } | |
| </style> | |
| <script src="//d3js.org/d3.v3.min.js"></script> | |
| <script src="//d3js.org/topojson.v1.min.js"></script> | |
| <script src="http://d3js.org/queue.v1.min.js"></script> | |
| <script> | |
| var margin = { top: 20, right: 20, bottom: 20, left: 20 }, | |
| width = 900 - margin.left - margin.right, | |
| height = 970 - margin.top - margin.bottom, | |
| percentage = d3.format("%");; | |
| var svg = d3.select("body").append("svg") | |
| .attr("width", width + margin.left + margin.right) | |
| .attr("height", height + margin.top + margin.bottom) | |
| .append("g") | |
| .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
| var path = d3.geo.path() | |
| .projection(null); | |
| queue() | |
| .defer(d3.json, "california.json") | |
| .await(ready); | |
| function ready(error, ca){ | |
| svg.selectAll("path") | |
| .data(topojson.feature(ca, ca.objects.county).features) | |
| .enter().append("path") | |
| .attr('class', 'land') | |
| .attr("d", path) | |
| .each(function(d,i){ | |
| // Generate a percentage | |
| var goal = +d.properties.rent / (+d.properties.rent + +d.properties.own) | |
| var pos = path.bounds(d), | |
| center = path.centroid(d); | |
| svg.append('rect') | |
| .attr('x', center[0] - (pos[1][0] - pos[0][0])/8) | |
| .attr('y', center[1] - (pos[1][1] - pos[0][1])/8) | |
| .attr('width', (pos[1][0] - pos[0][0])/4) | |
| .attr('height', (pos[1][1] - pos[0][1])/4) | |
| .attr('rx', 0) | |
| .attr('ry', 0) | |
| .style('fill', '#000') | |
| .style('opacity', '0.2'); | |
| svg.append('rect') | |
| .attr('x', center[0] - (pos[1][0] - pos[0][0])/8) | |
| .attr('y', center[1] - (pos[1][1] - pos[0][1])/8) | |
| .attr('width', (pos[1][0] - pos[0][0])/4 * goal) | |
| .attr('height', (pos[1][1] - pos[0][1])/4) | |
| .attr('rx', 0) | |
| .attr('ry', 0) | |
| .style('fill', 'red') | |
| .style('opacity', '0.6'); | |
| svg.append('text') | |
| .attr('x', center[0] - (pos[1][0] - pos[0][0])/8) | |
| .attr('y', center[1] - (pos[1][1] - pos[0][1])/8) | |
| .text(percentage(goal)) | |
| }) | |
| svg.append("path") | |
| .datum(topojson.mesh(ca, ca.objects.county, function(a, b) { return a !== b; })) | |
| .attr("class", "county-border") | |
| .attr("d", path); | |
| } | |
| d3.select(self.frameElement).style("height", (height) + "px"); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment