Skip to content

Instantly share code, notes, and snippets.

@dbetebenner
Last active January 10, 2017 19:41
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/3940ee4041021fdaeabf1c4984dcd9e9 to your computer and use it in GitHub Desktop.
Save dbetebenner/3940ee4041021fdaeabf1c4984dcd9e9 to your computer and use it in GitHub Desktop.
D3 Block-a-Day: Day 8, January 8th, 2017
license:gpl-3.0
height:600
border:no

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

New Year's Resolution for 2017: Make a D3 Block a day to teach myself D3. This is Number 8. This example builds on yesterday's Wyoming school district map adding tooltips that identify the district. 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 8, January 8, 2017</title>
<style>
#wrapper {
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
path {
fill: #ccc;
stroke: #fff;
stroke-width: .5px;
}
path:hover {
fill: #879baa;
}
.hidden {
display: none;
}
div.tooltip {
background-color: #fff8dc;
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.v2.min.js"></script>
<script src="https://d3js.org/d3-array.v1.min.js"></script>
<script src="https://d3js.org/d3-geo.v1.min.js"></script>
<div id="wrapper">
<div id="chart"></div>
</div>
<script>
var width = 960,
height = 500;
const zoom = d3.zoom()
.scaleExtent([1/4, 3])
.on('zoom', function () {
d3.select('g').attr('transform', d3.event.transform)
});
const svg = d3.select('#chart')
.append('svg')
.attr('width', width)
.attr('height', height)
.call(zoom)
const g = svg.append('g')
var projection = d3.geoMercator()
.center([ -107.5534, 43.00029 ])
.scale(5000);
var path = d3.geoPath()
.projection(projection);
var tooltip = d3.select('#chart').append('div')
.attr('class', 'hidden tooltip');
d3.json("WY_Districts.json", function(error, state_districts) {
if (error) throw error;
g.selectAll("path")
.data(topojson.feature(state_districts, state_districts.objects.districts).features)
.enter()
.append("path")
.attr("d", path)
.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] + 15) +'px; top:' + (mouse[1] - 35) + 'px')
.html(d.properties.District);
})
.on('mouseout', function() {
tooltip.classed('hidden', true);
});;
});
</script>
</body>
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment