|
<!DOCTYPE HTML> |
|
<body> |
|
<script src="http://d3js.org/d3.v4.js"></script> |
|
<script src="http://d3js.org/topojson.v1.min.js"></script> |
|
<style> |
|
.bubble { |
|
fill: rgb(52, 157, 82); |
|
fill-opacity: .5; |
|
stroke: rgb(52, 157, 82); |
|
stroke-width: .5px; |
|
} |
|
.bubble :hover { |
|
stroke: rgb(0, 68, 27); |
|
} |
|
.states { |
|
stroke: #fff; |
|
fill: none; |
|
stroke-linejoin: round; |
|
} |
|
.land { |
|
fill: #ddd; |
|
} |
|
.toolTip { |
|
position: absolute; |
|
display: none; |
|
max-width: 300px; |
|
height: auto; |
|
background-color: rgba(1,1,1,.7); |
|
border: 1px solid #111; |
|
padding:3px 8px; |
|
text-align: center; |
|
font-size:11px; |
|
color:#fff; |
|
font-family:Arial,sans-serif; |
|
line-height:1.4em; |
|
border-radius:3px; |
|
} |
|
.toolTip .county { |
|
color:#ddd; |
|
} |
|
</style> |
|
<script> |
|
var width = 960, |
|
height = 600; |
|
|
|
var path = d3.geoPath(); |
|
|
|
var radius = d3.scaleSqrt() |
|
.domain([0, 1]) |
|
.range([0, 5]); |
|
|
|
var svg = d3.select("body").append("svg") |
|
.attr("width", width) |
|
.attr("height", height); |
|
|
|
var tooltip = d3.select("body").append("div").attr("class", "toolTip"); |
|
|
|
var countyIds = d3.map(); |
|
var format = d3.format(".2"); |
|
|
|
d3.queue() |
|
.defer(d3.json, "us.json") |
|
.defer(d3.csv, "county-counts.csv") |
|
.await(ready); |
|
|
|
function ready(error, s, c) { |
|
if (error) { console.log(error) } |
|
|
|
topojson.feature(s, s.objects.counties).features.forEach(function(d, i) { |
|
countyIds.set(d.id, d); |
|
}); |
|
|
|
svg.append("path") |
|
.datum(topojson.feature(s, s.objects.nation)) |
|
.attr("class", "land") |
|
.attr("d", path); |
|
|
|
svg.append("path") |
|
.datum(topojson.mesh(s, s.objects.states, function(a, b) { return a !== b; })) |
|
.attr("class", "states") |
|
.attr("d", path); |
|
|
|
svg.append("g") |
|
.attr("class", "bubble") |
|
.selectAll("circle") |
|
.data(c |
|
.filter(function(d) {return countyIds.has(d.county)}) |
|
.sort(function(a, b) { return b.count - a.count; }) |
|
) |
|
.enter().append("circle") |
|
.attr("transform", function(d) { return "translate(" + path.centroid(countyIds.get(d.county)) + ")"; }) |
|
.attr("r", function(d) { return radius(d.count/countyIds.get(d.county).properties.population*100000); }) |
|
.on("mousemove", function(d){ |
|
tooltip |
|
.style("left", path.centroid(countyIds.get(d.county))[0] -55 + "px") |
|
.style("top", d3.event.pageY - 70 + "px") |
|
.style("display", "inline-block") |
|
.html( "<span class='county'>" + |
|
countyIds.get(d.county).properties.name + |
|
"</span></br>" + d.count + " Fertility Clinics </br>" + |
|
format(d.count/countyIds.get(d.county).properties.population*100000) + " per 100,000 people"); |
|
}) |
|
.on("mouseout", function(d){ tooltip.style("display", "none");}); |
|
} |
|
</script> |
|
</body> |