Skip to content

Instantly share code, notes, and snippets.

@bradllj
Last active January 2, 2016 15:49
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 bradllj/8326068 to your computer and use it in GitHub Desktop.
Save bradllj/8326068 to your computer and use it in GitHub Desktop.
World and US States Document Count With Zoom
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.subunit { fill: #aaa; }
.q0-9 { fill:rgb(247,251,255); }
.q1-9 { fill:rgb(222,235,247); }
.q2-9 { fill:rgb(198,219,239); }
.q3-9 { fill:rgb(158,202,225); }
.q4-9 { fill:rgb(107,174,214); }
.q5-9 { fill:rgb(66,146,198); }
.q6-9 { fill:rgb(33,113,181); }
.q7-9 { fill:rgb(8,81,156); }
.q8-9 { fill:rgb(8,48,107); }
.subunit-boundary {
fill: none;
stroke: #777;
stroke-linejoin: round;
}
</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 = 960;
/* var projection = d3.geo.mercator()
.scale(100)
.translate([width / 2, height / 2]);
*/
var projection = d3.geo.mercator()
.scale(200);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var test = 0;
var countByName = d3.map();
var g = svg.append("g");
//f45082ef685072b201cf
//c389604b49cf72b10649
queue()
.defer(d3.json, "/bradllj/raw/f45082ef685072b201cf/world.json")
.defer(d3.json, "/bradllj/raw/8250265/toplocations.json")
.await(ready);
var quantize = d3.scale.quantize()
.domain([0, 2000])
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
function ready(error, world, locations) {
console.log(world)
//console.log(locations)
locations.forEach(function(data) {
//console.log(data.value.toLowerCase())
//console.log(data.value + " " +quantize(data.count));
countByName.set(data.value, data.count);
})
//console.log(quantize(countByName.get('iran')));
g.selectAll(".countries")
.data(topojson.feature(world, world.objects.countries).features)
.enter().append("path")
.attr("class", function(d) { return "subunit " + quantize(countByName.get(d.id.toLowerCase())); })
.attr("d", path);
g.selectAll(".states")
.data(topojson.feature(world, world.objects.states).features)
.enter().append("path")
.attr("class", function(d) { return "subunit " + quantize(countByName.get(d.id.toLowerCase())); })
.attr("d", path);
g.append("path")
.datum(topojson.mesh(world, world.objects.states, function(a, b) { return a !== b }))
.attr("d", path)
.attr("class", "subunit-boundary");
g.append("path")
.datum(topojson.mesh(world, world.objects.states, function(a, b) { return a == b }))
.attr("d", path)
.attr("class", "subunit-boundary");
g.append("path")
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b }))
.attr("d", path)
.attr("class", "subunit-boundary");
g.append("path")
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a == b }))
.attr("d", path)
.attr("class", "subunit-boundary");
};
// zoom and pan
var zoom = d3.behavior.zoom()
.on("zoom",function() {
g.attr("transform","translate("+
d3.event.translate.join(",")+")scale("+d3.event.scale+")");
});
svg.call(zoom)
</script>
[{"id":121,"value":"iran","count":2508},{"id":88,"value":"washington","count":1778},{"id":14,"value":"united states","count":1545},{"id":53,"value":"china","count":1513},{"id":86,"value":"new york","count":1166},{"id":224,"value":"us","count":1069},{"id":329,"value":"russia","count":824},{"id":164,"value":"israel","count":757},{"id":123,"value":"america","count":749},{"id":124,"value":"texas","count":615},{"id":161,"value":"california","count":602},{"id":188,"value":"chicago","count":577},{"id":37,"value":"europe","count":574},{"id":5,"value":"florida","count":570},{"id":326,"value":"syria","count":556},{"id":62,"value":"japan","count":555},{"id":122,"value":"american","count":554},{"id":160,"value":"san francisco","count":554},{"id":51,"value
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