This this a variation of this bl.ock by Mike Bostock to demostrate the appending text to a map in response to this stack overflow question. The area of each circle in this symbol map is proportional to the population of the associated state.
-
-
Save phil-pedruco/7189721 to your computer and use it in GitHub Desktop.
Symbol Map with Text
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"> | |
<style> | |
.states { | |
fill: #ccc; | |
stroke: #fff; | |
} | |
.symbol { | |
fill: steelblue; | |
fill-opacity: .8; | |
stroke: #fff; | |
} | |
</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 radius = d3.scale.sqrt() | |
.domain([0, 1e6]) | |
.range([0, 10]); | |
var path = d3.geo.path(); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
queue() | |
.defer(d3.json, "/mbostock/raw/4090846/us.json") | |
.defer(d3.json, "/mbostock/raw/3750900/us-state-centroids.json") | |
.await(ready); | |
function ready(error, us, centroid) { | |
svg.append("path") | |
.attr("class", "states") | |
.datum(topojson.feature(us, us.objects.states)) | |
.attr("d", path); | |
svg.selectAll(".symbol") | |
.data(centroid.features.sort(function(a, b) { return b.properties.population - a.properties.population; })) | |
.enter().append("path") | |
.attr("class", "symbol") | |
.attr("d", path.pointRadius(function(d) { return radius(d.properties.population); })) | |
.append("title") | |
.text(function (d) { return d.properties.name; }); | |
// Code to add in text on map | |
svg.selectAll("text") | |
.data(centroid.features).enter() | |
.append("text") | |
.attr("x", function (d) { return path.centroid(d)[0]; }) | |
.attr("y", function (d) { return path.centroid(d)[1]; }) | |
.text(function (d) { return d.properties.name; }); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment