// Define global variables
var width_4 = 1000,
height_4 = 800,
centered;
function createNYCMap(){
// Define the projection boundaries
var projection = d3.geoMercator()
.center([-73.94, 40.70])
.scale(50000)
.translate([(width_4) / 2, (height_4) / 2]);
// Define the path
var path = d3.geoPath()
.projection(projection);
// Define the div for the tooltip
var div = d3.select("#map_4")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// Define the svg for the map
var svg = d3.select("#map_4")
.append("svg")
.attr("width", width_4)
.attr("height", height_4);
// Define the g for each neighborhood
var g = svg.append("g");
d3.json("nyc.geojson", function(json) {
g.append("g")
.attr("id", "neighborhood")
.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.on("click", clicked);
});
function clicked(d){
var x, y, k;
// IF zoomed-in state is on
if (d && centered !== d) {
// Change the center of the projection
var centroid = path.centroid(d);
x = centroid[0];
y = centroid[1];
k = 4;
centered = d;
// Show tooltip
div.transition()
.duration(200)
.style("opacity", .9);
// Write staff on tooltip
div.html("Neighborhood:" + "" + d.properties.PO_NAME.toLowerCase().toUpperCase() + "" + "
" +
"Total Number of Complaints:" + '' + "
" +
"Most Common Complaint:" + ''
)
// Place the tooltip
div.style("left", (d3.mouse(this)[0]) + "px")
.style("top", (d3.mouse(this)[1]) + "px");
// Load CSV for filling the missing info on tooltip
d3.csv("total_neight.csv", function(data) {
var districtName = d.properties.PO_NAME.toUpperCase();
var matchFound = false;
for(var i=0;i