Created
January 14, 2014 17:48
-
-
Save ejs06003/8422535 to your computer and use it in GitHub Desktop.
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> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>d3: NYC Tract</title> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.min.js"></script> | |
<style type="text/css"> | |
.d3-tip { | |
line-height: 1; | |
font-weight: bold; | |
padding: 12px; | |
background: rgba(224, 228, 235, .9); | |
font: 12px sans-serif; | |
color: black; | |
border-radius: 2px; | |
} | |
/* Creates a small triangle extender for the tooltip */ | |
.d3-tip:after { | |
box-sizing: border-box; | |
display: inline; | |
font-size: 10px; | |
width: 100%; | |
line-height: 1; | |
color: rgba(224, 228, 235, .9); | |
content: "\25BC"; | |
position: absolute; | |
text-align: center; | |
} | |
/* Style northward tooltips differently */ | |
.d3-tip.n:after { | |
margin: -1px 0 0 0; | |
top: 100%; | |
left: 0; | |
} | |
.canvas{ | |
border: 2px solid black; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
//Width and height | |
var margin = {top: 45, right: 20, bottom: 50, left: 20}, | |
w = 1000 - margin.left - margin.right, | |
h = 750 - margin.top - margin.bottom; | |
//Map projection | |
var projection = d3.geo.mercator() | |
.center([-73.9400, 40.6700]) | |
.scale(50000) | |
.translate([(w) / 2, (h)/2]); | |
//Define path | |
var path = d3.geo.path() | |
.projection(projection); | |
//colors | |
var color = d3.scale.linear() | |
.domain([1, 250000]) | |
.range(["#f1c40f", "#ff0000"]) | |
.interpolate(d3.interpolateHcl); | |
//tool tip | |
var tip = d3.tip() | |
.attr('class', 'd3-tip') | |
.offset([-10, 0]) | |
.html(function(d) {return "GeoID: " + d.properties.GeoID;}); | |
//Create SVG element | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h) | |
.attr("class","canvas"); | |
svg.call(tip); | |
// Shape Data | |
d3.json("nyctract.json", function(json) { | |
//Json Data for tracts | |
svg.selectAll("path") | |
.data(json.features) | |
.enter() | |
.append("path") | |
.attr("d", path) | |
.attr("transform", "translate(" + 20 + "," + 30 + ")") | |
.style("stroke", "white") | |
.style("stroke-width", 1) | |
/*.style("fill", "grey")*/ | |
.style('fill', function(d) {return color( d.properties.medhhincom);}) | |
.on('mouseover', tip.show) | |
.on('mouseout', tip.hide); | |
}); | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment