I've made a map of the US with some major cities drawn on the map, and voronoi areas created based on those cities. Now what I want to do is have some relevant information displayed when I hover over an area. Unfortunately, I think the way I approached getting the data to make the polygons (see highlighted line) is making it difficult or impossible for me to get to the other information. Any suggestions? Copies of relevant files are attached, as well.
// Create the projection type
var projection = d3.geo.albersUsa()
.translate([w/2, h/2])
.scale([1080]);
// Define default path generator
var path = d3.geo.path()
.projection(projection);
// CANVAS STUFF
// Create the SVG element
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h);
// Append the background to the SVG
svg.append("rect")
.attr("class", "background")
.attr("width", w)
.attr("height", h)
// Append a group element to the SVG (holds everything from the map)
var g = svg.append("g");
// VORONOI STUFF
// Creating the voronoi layout
var voronoi = d3.geom.voronoi()
.clipExtent([[0, 0], [w, h]]);
d3.csv("us-cities1.csv", function(d) {
return [projection([+d.lon, +d.lat])[0], projection([+d.lon, +d.lat])[1]];
}, function(error, rows) {
vertices = rows;
console.log(vertices);
drawV(vertices);
}
);
function polygon(d) {
return "M" + d.join("L") + "Z";
}
function drawV(d) {
svg.append("g")
.selectAll("path")
.data(voronoi(d), polygon)
.enter().append("path")
.attr("class", "test")
.attr("d", polygon);
svg.selectAll("circle")
.data(d)
.enter().append("circle")
.attr("class", "city")
.attr("transform", function(d) { return "translate(" + d + ")"; })
.attr("r", 2);
}
// MAP STUFF
// Loading state boundaries GeoJSON data
d3.json("us-states.json", function(error, us) {
g.append("g")
.attr("id", "states")
.selectAll("path")
.data(us.features)
.enter().append("path")
.attr("d", path);
});
// JQUERY
$(document).ready(function () {
$("path.test").hover
(function () {
$("#label").addClass("states");
}, function() {
$("#label").removeClass("states");
}
);
});