Skip to content

Instantly share code, notes, and snippets.

@pbogden
Last active December 17, 2015 00:33
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 pbogden/e7d79013ddf4a606ce18 to your computer and use it in GitHub Desktop.
Save pbogden/e7d79013ddf4a606ce18 to your computer and use it in GitHub Desktop.
Final - Part 1

Using the code in http://bl.ocks.org/mbostock/4090848 plot a red circle on DC

Step 1. Change

d3.json("/mbostock/raw/4090846/us.json", function(error, us) {

to

d3.json("https://gist.githubusercontent.com/mbostock/4090846/raw/us.json", function(error, us) {

Step 2. Add the following

var loc = projection([-77.0164, 38.9047]);
svg.append("circle")
    .attr("cx", loc[0])
    .attr("cy", loc[1])
    .attr("r", 10)
    .style("fill", "red");
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.land {
fill: #222;
}
.county-boundary {
fill: none;
stroke: #fff;
stroke-width: .5px;
}
.state-boundary {
fill: none;
stroke: #fff;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.albersUsa()
.scale(1000)
.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);
d3.json("https://gist.githubusercontent.com/mbostock/4090846/raw/us.json", function(error, us) {
if (error) throw error;
svg.insert("path", ".graticule")
.datum(topojson.feature(us, us.objects.land))
.attr("class", "land")
.attr("d", path);
svg.insert("path", ".graticule")
.datum(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b && !(a.id / 1000 ^ b.id / 1000); }))
.attr("class", "county-boundary")
.attr("d", path);
svg.insert("path", ".graticule")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "state-boundary")
.attr("d", path);
var loc = projection([-77.0164, 38.9047]);
svg.append("circle")
.attr("cx", loc[0])
.attr("cy", loc[1])
.attr("r", 10)
.style("fill", "red");
});
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