Skip to content

Instantly share code, notes, and snippets.

@dbetebenner
Last active January 27, 2017 01:36
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 dbetebenner/2aafa5fc85dc38afce619f2128e711cb to your computer and use it in GitHub Desktop.
Save dbetebenner/2aafa5fc85dc38afce619f2128e711cb to your computer and use it in GitHub Desktop.
D3 Block-a-Day: Day 21, January 21st, 2017
license:gpl-3.0
height:600
border:no
Raw

D3 Block-a-Day: Day 21, January, 21th 2017

New Year's Resolution for 2017: Make a D3 Block a day to teach myself D3. This is Number 21. This example extends the January 11 Example Rhode Island click-to-zoom example to be a full chloropleth with the colors indicating number of tested students. The topoJSON files used to construct this visualization were created by me using shape files provided by the United States Census Bureau. The Center for Assessment makes these state and national school district topoJSON files available for anyone to use in their own development.

<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>D3 Block-a-Day: Day 11, January 11, 2017</title>
<style>
.background {
fill: none;
pointer-events: all;
}
#districts {
fill: #ccc;
}
#districts .active {
fill: orange;
}
#district-borders {
fill: none;
stroke: white;
stroke-width: 1px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
.hidden {
display: none;
}
div.tooltip {
background-color: #ffffff;
padding: 7px;
text-shadow: #f5f5f5 0 1px 0;
font: 13px Helvetica Neue;
border: 2.5px solid;
border-color: black;
border-radius: 3px;
opacity: 0.9;
position: absolute;
box-shadow: rgba(0, 0, 0, 0.3) 0 2px 10px;
}
</style>
</head>
<body>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 600,
centered;
var projection = d3.geoMercator()
.center([ -71.5, 41.7 ])
.scale(24000);
var path = d3.geoPath()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", clicked);
var tooltip = d3.select('body').append('div')
.attr('class', 'hidden tooltip');
var g = svg.append("g");
d3.json("RI_Districts.topojson", function(error, state_districts) {
if (error) throw error;
g.append("g")
.attr("id", "districts")
.selectAll("path")
.data(topojson.feature(state_districts, state_districts.objects.districts).features)
.enter().append("path")
.attr("d", path)
.on("click", clicked)
.on('mousemove', function(d) {
var mouse = d3.mouse(svg.node()).map(function(d) {
return parseInt(d);
});
tooltip.classed('hidden', false)
.attr('style', 'left:' + (mouse[0] + 10) +'px; top:' + (mouse[1] - 20) + 'px')
.html(d.properties.name);
})
.on('mouseout', function() {
tooltip.classed('hidden', true);
});;
g.append("path")
.datum(topojson.mesh(state_districts, state_districts.objects.districts, function(a, b) { return a !== b; }))
.attr("id", "district-borders")
.attr("d", path);
});
function clicked(d) {
var x, y, k;
if (d && centered !== d) {
var centroid = path.centroid(d);
x = centroid[0];
y = centroid[1];
k = 4;
centered = d;
} else {
x = width / 2;
y = height / 2;
k = 1;
centered = null;
}
g.selectAll("path")
.classed("active", centered && function(d) { return d === centered; });
g.transition()
.duration(750)
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")")
.style("stroke-width", 1.5 / k + "px");
}
</script>
Display the source blob
Display the rendered blob
Raw
{"type":"Topology","transform":{"scale":[0.00007909330933093269,0.00009230583058305855],"translate":[-71.907258,41.095833999999996]},"objects":{"districts":{"type":"GeometryCollection","geometries":[{"type":"Polygon","arcs":[[0,1,2,3,4,5,6]],"properties":{"name":"Foster-Glocester Regional School District","state":"44"}},{"type":"Polygon","arcs":[[7,8,9,10,11,12]],"properties":{"name":"Exeter-West Greenwich Regional School District","state":"44"}},{"type":"Polygon","arcs":[[13,2,3,4]],"properties":{"name":"Foster Elementary School District","state":"44"}},{"type":"Polygon","arcs":[[0,1,-14,5,6]],"properties":{"name":"Glocester Elementary School District","state":"44"}},{"type":"Polygon","arcs":[[14,15,16,17,18,19]],"properties":{"name":"Jamestown School District","state":"44"}},{"type":"Polygon","arcs":[[20,21,22]],"properties":{"name":"Little Compton School District","state":"44"}},{"type":"Polygon","arcs":[[23,24,25,26,27]],"properties":{"name":"Barrington School District","state":"44"}},{"type":"Polygon","arcs":[[-26,28,29,30]],"properties":{"name":"Bristol-Warren Regional School District","state":"44"}},{"type":"Polygon","arcs":[[31,-7,32]],"properties":{"name":"Burrillville School District","state":"44"}},{"type":"Polygon","arcs":[[33,34,35]],"properties":{"name":"Central Falls School District","state":"44"}},{"type":"Polygon","arcs":[[36,-12,37,38,39]],"properties":{"name":"Chariho Regional School District","state":"44"}},{"type":"Polygon","arcs":[[40,41,42,43,-8,44,-4]],"properties":{"name":"Coventry School District","state":"44"}},{"type":"Polygon","arcs":[[45,46,47,48,49,-42,50]],"properties":{"name":"Cranston School District","state":"44"}},{"type":"Polygon","arcs":[[51,-34,52,53]],"properties":{"name":"Cumberland School District","state":"44"}},{"type":"Polygon","arcs":[[54,55,56,-9,-44]],"properties":{"name":"East Greenwich School District","state":"44"}},{"type":"Polygon","arcs":[[57,58,-24,59,-48,60]],"properties":{"name":"East Providence School District","state":"44"}},{"type":"Polygon","arcs":[[61,62,-46,63,64]],"properties":{"name":"Johnston School District","state":"44"}},{"type":"Polygon","arcs":[[65,-53,-36,66,67,68]],"properties":{"name":"Lincoln School District","state":"44"}},{"type":"Polygon","arcs":[[69,70,71,-17]],"properties":{"name":"Middletown School District","state":"44"}},{"type":"Polygon","arcs":[[72,-20,73,74]],"properties":{"name":"Narragansett School District","state":"44"}},{"type":"Polygon","arcs":[[75]],"properties":{"name":"New Shoreham School District","state":"44"}},{"type":"Polygon","arcs":[[-72,76,-18]],"properties":{"name":"Newport School District","state":"44"}},{"type":"Polygon","arcs":[[77,78,-15,-73,79,-10,-57]],"properties":{"name":"North Kingstown School District","state":"44"}},{"type":"Polygon","arcs":[[-68,80,81,-62,82]],"properties":{"name":"North Providence School District","state":"44"}},{"type":"Polygon","arcs":[[83,84,-66,85,-32]],"properties":{"name":"North Smithfield School District","state":"44"}},{"type":"Polygon","arcs":[[-67,-35,86,-58,87,-81]],"properties":{"name":"Pawtucket School District","state":"44"}},{"type":"Polygon","arcs":[[88,-27,-31,89,-23,90,-70,-16,-79]],"properties":{"name":"Portsmouth School District","state":"44"}},{"type":"Polygon","arcs":[[-82,-88,-61,-47,-63]],"properties":{"name":"Providence School District","state":"44"}},{"type":"Polygon","arcs":[[-2,-64,-51,-41,-3]],"properties":{"name":"Scituate School District","state":"44"}},{"type":"Polygon","arcs":[[-86,-69,-83,-65,-1]],"properties":{"name":"Smithfield School District","state":"44"}},{"type":"Polygon","arcs":[[-11,-80,-75,91,-38]],"properties":{"name":"South Kingstown School District","state":"44"}},{"type":"Polygon","arcs":[[-30,92,-21,-90]],"properties":{"name":"Tiverton School District","state":"44"}},{"type":"Polygon","arcs":[[-49,-60,-28,-89,-78,-56,93]],"properties":{"name":"Warwick School District","state":"44"}},{"type":"Polygon","arcs":[[-50,-94,-55,-43]],"properties":{"name":"West Warwick School District","state":"44"}},{"type":"Polygon","arcs":[[-40,94]],"properties":{"name":"Westerly School District","state":"44"}},{"type":"Polygon","arcs":[[95,-54,-85]],"properties":{"name":"Woonsocket School District","state":"44"}}]}},"arcs":[[[4010,9081],[188,-819]],[[4198,8262],[-1464,-22]],[[2734,8240],[294,-1398]],[[3028,6842],[-1543,-32]],[[1485,6810],[-63,1408]],[[1422,8218],[-38,803]],[[1384,9021],[2626,60]],[[1517,6069],[3042,59]],[[4559,6128],[305,-639]],[[4864,5489],[184,-1111]],[[5048,4378],[-1330,275]],[[3718,4653],[-97,420],[-2157,-200]],[[1464,4873],[53,1196]],[[1422,8218],[1312,22]],[[6267,4426],[304,1075]],[[6571,5501],[934,-380]],[[7505,5121],[23,-503]],[[7528,4618],[-629,-623]],[[6899,3995],[-763,-170]],[[6136,3825],[131,601]],[[8676,4848],[1177,210]],[[9853,5058],[99,-851],[-1149,173]],[[8803,4380],[-127,468]],[[6852,6931],[600,438]],[[7452,7369],[423,-152]],[[7875,7217],[-564,-889]],[[7311,6328],[-26,76]],[[7285,6404],[-433,527]],[[7875,7217],[961,-775]],[[8836,6442],[-205,-266]],[[8631,6176],[-1320,152]],[[3805,9939],[205,-858]],[[1384,9021],[-19,862],[2440,56]],[[6485,8687],[160,-49]],[[6645,8638],[-289,-136]],[[6356,8502],[129,185]],[[1384,3477],[80,1396]],[[3718,4653],[227,-857],[-713,-1602]],[[3232,2194],[-982,-223]],[[2250,1971],[-21,1306],[-845,200]],[[3028,6842],[1546,29]],[[4574,6871],[144,1]],[[4718,6872],[23,-735]],[[4741,6137],[-182,-9]],[[1517,6069],[-32,741]],[[4395,7516],[1213,166]],[[5608,7682],[1081,-223]],[[6689,7459],[-2,-315]],[[6687,7144],[-1335,-255]],[[5352,6889],[-634,-17]],[[4574,6871],[-179,645]],[[5679,9987],[970,12],[-4,-1361]],[[6485,8687],[-1177,882]],[[5308,9569],[371,418]],[[4741,6137],[417,6]],[[5158,6143],[397,-312]],[[5555,5831],[-691,-342]],[[6734,8247],[509,46]],[[7243,8293],[209,-924]],[[6852,6931],[-165,213]],[[6689,7459],[45,788]],[[5206,8373],[288,-342]],[[5494,8031],[114,-349]],[[4395,7516],[-197,746]],[[4198,8262],[1008,111]],[[5274,9185],[34,384]],[[6356,8502],[-224,-23]],[[6132,8479],[-573,-64]],[[5559,8415],[-285,770]],[[7505,5121],[1016,-581]],[[8521,4540],[-665,-279]],[[7856,4261],[-328,357]],[[5779,4398],[488,28]],[[6136,3825],[-1107,-1529],[-831,143]],[[4198,2439],[1581,1959]],[[2888,683],[687,1249],[845,215],[1019,-1500],[-1702,-630],[-849,666]],[[7856,4261],[-957,-266]],[[5555,5831],[1103,175]],[[6658,6006],[-87,-505]],[[5779,4398],[-731,-20]],[[6132,8479],[59,-207]],[[6191,8272],[-697,-241]],[[5206,8373],[353,42]],[[3805,9939],[976,19]],[[4781,9958],[527,-389]],[[5274,9185],[-1264,-104]],[[6645,8638],[598,-345]],[[6734,8247],[-543,25]],[[6658,6006],[627,398]],[[8631,6176],[45,-1328]],[[8803,4380],[-282,160]],[[4198,2439],[-966,-245]],[[8836,6442],[958,-326],[59,-1058]],[[5158,6143],[194,746]],[[2250,1971],[-782,-1013],[-1468,1301],[1384,1218]],[[4781,9958],[898,29]]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment