Skip to content

Instantly share code, notes, and snippets.

@mapsam
Last active December 20, 2015 21:09
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 mapsam/6195407 to your computer and use it in GitHub Desktop.
Save mapsam/6195407 to your computer and use it in GitHub Desktop.
D3: .datum() test

This example shows how adding geographic data with the .datum() function creates one large svg element instead of multiple elements. The hover effect highlights the element, which happens to be the entire extent. Using .data() and .enter() will allow you to interact with each element separately because each is created in a separate iteration. See how to interact with topojson files.

<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
.states {
fill: #e5e5e5;
stroke: #fff;
stroke-width:2px;
}
.states:hover {
fill: steelblue;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script>
var width = 960,
height = 500;
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
var projection = d3.geo.albersUsa()
.scale(1000)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
queue()
.defer(d3.json, "/svmatthews/raw/6083585/us.json")
.await(makeMyMap);
function makeMyMap(error, us){
svg.append('path')
.datum(topojson.feature(us, us.objects.usStates))
.attr('d', path)
.attr('class', 'states');
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment