Skip to content

Instantly share code, notes, and snippets.

@threestory
Created November 13, 2015 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save threestory/ed0f322d7bb2e3be8ded to your computer and use it in GitHub Desktop.
Save threestory/ed0f322d7bb2e3be8ded to your computer and use it in GitHub Desktop.
California Counties
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.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>California Counties: Mercator projection</title>
<script type="text/javascript" src="//d3js.org/d3.v3.min.js"></script>
<script type="text/javascript" src="http://use.typekit.com/brf5jpj.js"></script>
<script src="//use.typekit.net/drk2sev.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
<style type="text/css">
body {
margin: 0;
background-color: #48494B;
font-family: "proxima-nova", "Source Sans Pro", sans-serif;
}
#container {
width: 800px;
margin-left: 30px;
margin-right: auto;
margin-top: 30px;
padding: 30px;
background-color: white;
box-shadow: 3px 3px 7px #222;
}
h1 {
font-size: 24px;
margin: 0;
}
p {
font-size: 16px;
margin: 15px 0 10px 0;
}
a {
color: #799DCB;
text-decoration: none;
transition: color .3s, background .3s, border .3s;
}
a:hover {
color: #48494b;
background: #e7e8e9;
}
svg {
background-color: white;
padding-left: 20px;
}
path {
fill: #799dcb;
stroke: #fff;
}
path:hover {
fill:#48494b;
cursor:pointer;
}
#tooltip {
width: 150px;
height: auto;
padding: 5px;
background-color: #fff;
color: #000;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 3px 3px 8px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 3px 3px 8px rgba(0, 0, 0, 0.4);
box-shadow: 3px 3px 8px rgba(0, 0, 0, 0.4);
pointer-events: none;
position: absolute;
}
#tooltip.hidden {
display: none;
}
#tooltip p {
margin: 0;
font-size: 14px;
line-height: 18px;
}
</style>
</head>
<body>
<body>
<div id="container">
<h1>Counties of California</h1>
<p>Source: <a href="https://www.census.gov/geo/maps-data/data/cbf/cbf_counties.html" target="new">U.S. Census Bureau</a></p>
</div>
<div id="tooltip" class="hidden">
<p>County: <span id="county">County Name</span></p>
</div>
<script type="text/javascript">
//Width and height
var w = 760;
var h = 600;
//Define map projection
var projection = d3.geo.mercator()
.center([ -120, 37 ])
.translate([ w/2, h/2 ])
.scale([ w*3.3 ]);
//Define path generator
var path = d3.geo.path()
.projection(projection);
//Create SVG
var svg = d3.select("#container")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load in GeoJSON data
d3.json("cb_2014_us_county_5m.json", function(json) {
console.log(json);
//Bind data and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.on("mouseover", function(d){
var xPosition = w/2 + 150;
var yPosition = h/2;
// var xPosition = parseFloat(path.centroid(this).attr("cx"));
// var yPosition = parseFloat(path.centroid(this).attr("cy"));
d3.select("#tooltip")
.style("left", xPosition + "px")
.style("top", yPosition + "px");
d3.select("#county")
.text(d.properties.NAME);
d3.select("#tooltip")
.classed("hidden", false);
})
.on("mouseout", function(){
d3.select("#tooltip").classed("hidden", true);
});
});
</script>
</body>
</html>
@gregdevs
Copy link

gregdevs commented Sep 7, 2016

Hi, nice work.

I am working on creating a couple of state maps.
Can you provide some guidance on how to combine a state shape file and a csv of the county id's so they are merged into one file? I've been trying with the topojson CLI to no luck. https://github.com/mbostock/topojson/wiki

-Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment