// move elements to front
// http://tributary.io/tributary/3922684
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
var width = 960,
height = 500,
active = d3.select(null); //zoom
// set projection and map position
// tweek these settings for a nice display
var projection = d3.geo.albers() // popular alternative is geo.mercator()
.center([0, 40.0]) //[30, 45.0]
.scale(900) // 275
.rotate([97.8717, 0])
.translate([width / 2, height / 2])
.precision(.1);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "chart");
var graticule = d3.geo.graticule();
var dataset;
var destination = [-75.1667 , 39.9500]; // Philly lat lon
var map = svg.append("g")
.attr("class", "map");
var
vizGroup = svg.append("g").attr("class", "viz"),
countryGroup = map.append("g"),
stateGroup = map.append("g")
;
// plot world, states, and people data
queue()
.defer(d3.json, "world-50m.json")
.defer(d3.json, "us.json")
.defer(d3.csv, "people.csv")
.await(visualize);
function visualize(error, world, us, people){
if (error) return console.log(error);
dataset = people;
// draw backgound to for reset function
map.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", reset);
// draw graticules
map.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);
// draw world
countryGroup
.selectAll("path")
.data(topojson.feature(world, world.objects.countries).features)
.enter().insert("path", ".graticule")
.attr("class", "boundary")
.attr("d", path);
// draw states
stateGroup
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
// .attr("d", path)
.attr("class", "state-boundary")
.attr("d", path);
// create a group for each person record
var groups = vizGroup.selectAll("g")
.data(dataset)
.enter()
.append("g")
.attr("class", function (d) {
return d.name;
})
.on("mouseover", function() {
d3.select(this).attr("class", "highlight");
// move to front
var sel = d3.select(this);
sel.moveToFront();
})
.on("mouseout", function (d) {
d3.select(this).attr("class", function () {
return d.name;});
})
;
// draw arcs from each record origin to destination
groups.append("path")
.attr("class", "arc")
.attr("d", function (d) {
var coordDepart = [ d.longitude, d.latitude ];
var coordArrive = destination;
return path({
type: "LineString",
coordinates: [coordDepart,coordArrive]
});
})
.call(d3.helper.tooltip(function(d, i){
return ""+
"