chart = {
const width = 960,
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [-50, -20, width, height])
.style("background", "#ffeacf");
var projection = d3
.geoMercator()
.fitSize([width - 50, height - 50], zips);
var path = d3.geoPath().projection(projection);
var path2 = d3.geoPath().projection(projection);
svg .append('text')
.attr('x','100')
.attr('y','30')
.attr('font-size','.85em')
.attr('fill','#DC7358')
.style("font", "30px roboto")
.style("font-weight", "bold")
.text("NYC STUDENT CRIME")
svg.append('text')
.attr("class","descript")
.attr('x','100')
.attr('y','45')
.attr('font-size','.85em')
.attr('fill','#C06953')
.style("font", "14px roboto")
.text("In relation to public school locations")
svg.append('text')
.attr("class","descript")
.attr('x','100')
.attr('y','80')
.attr('font-size','.85em')
.attr('fill','#C06953')
.style("font", "10px roboto")
.text("BOROUGH:")
svg.append('text')
.attr("class","descript")
.attr('x','100')
.attr('y','110')
.attr('font-size','.85em')
.attr('fill','#C06953')
.style("font", "10px roboto")
.text("AREA (In Sq Km):")
svg.append('text')
.attr("class","descript")
.attr('x','100')
.attr('y','140')
.attr('font-size','.85em')
.attr('fill','#C06953')
.style("font", "10px roboto")
.text("ZIP:")
svg.append('text')
.attr("class","descript")
.attr('x','100')
.attr('y','170')
.attr('font-size','.85em')
.attr('fill','#C06953')
.style("font", "10px roboto")
.text("POPULATION:")
var g = svg.append("g").attr("id", "paths");
g.selectAll("path")
.data(zips.features)
.enter()
.append("path")
.attr('class','outlines')
.attr("d", path)
.attr("fill", d => color(d.properties.POPULATION))
.style('fill-opacity','.5')
.style("stroke", "#ccc")
var c = svg.selectAll("circle")
.data(zips.features)
.enter()
.append("circle")
.attr('cx',function(d) {return path.centroid(d)[0]})
.attr('cy',function(d) {return path.centroid(d)[1]})
.attr('r',1)
.style("fill", "black")
.style('fill-opacity','1')
svg.selectAll('text')
.data(zips.features)
.enter()
.append('text')
.attr('x','0')
.attr('y',function(d,i){return i*3.3})
.attr('font-size','.25em')
.attr('text-anchor','end')
.text(function(d){return d.properties.PO_NAME})
.on('mouseover',addStroke)
.on('mouseleave',removeStroke)
var l = svg.selectAll('line')
.data(zips.features)
.enter()
.append('line')
.attr('x1',2)
.attr('y1',function(d,i){return i*3.3})
.attr('x2',function(d) {return path.centroid(d)[0]})
.attr('y2',function(d) {return path.centroid(d)[1]})
.attr('stroke-width','.4')
.attr('stroke-opacity','.1')
.style('stroke','rgb(0,0,0)')
.on('mouseover',addStroke)
.on('mouseleave',removeStroke)
function addStroke(event,d) {
d3.select(this).style("stroke-width", ".8")
d3.select(this).style("stroke", "black")
d3.select(this).style("stroke-opacity", "1")
}
function removeStroke(event,d) {
d3.selectAll('text.labels').remove()
d3.select(this).style("stroke-width", ".4")
d3.select(this).style("stroke", "black")
d3.select(this).style("stroke-opacity", ".1")
svg
.append('text')
.attr('class','labels')
.attr('x','100')
.attr('y','155')
.attr('font-size','.85em')
.style("font", "10px roboto")
.attr('fill','#DC7358')
.text(d.properties.PO_NAME)
svg
.append('text')
.attr('class','labels')
.attr('x','100')
.attr('y','185')
.attr('font-size','.55em')
.style("font", "10px roboto")
.attr('fill','#DC7358')
.text(d.properties.POPULATION)
}
g.selectAll("path2")
.data(nycBoroughs.features)
.enter()
.append("path")
.attr('class','outlines')
.attr("d", path)
.attr("fill", "white")
.attr('fill-opacity','0.01')
.style("stroke-width", ".5")
.style("stroke", "black")
.style("stroke-opacity", ".5")
.on('mouseover',addFill)
.on('mouseleave',removeFill)
svg.selectAll('text')
.data(nycBoroughs.features)
.enter()
.append('text')
.attr('x',function(d) {return path.centroid(d)[0]})
.attr('y',function(d) {return path.centroid(d)[1]})
.attr('font-size','2em')
.text(function(d){return d.properties.boro_name})
function addFill(event,d) {
d3.select(this).style("fill", "#DC7358")
d3.select(this) .style('fill-opacity','.75')
svg
.append('text')
.attr('class','labels')
.attr('x','100')
.attr('y','95')
.attr('font-size','.85em')
.style("font", "10px roboto")
.attr('fill','#DC7358')
.text(d.properties.boro_name)
svg
.append('text')
.attr('class','labels')
.attr('x','100')
.attr('y','125')
.attr('font-size','.55em')
.attr('fill','#DC7358')
.style("font", "10px roboto")
.text((d.properties.shape_area)/10000000)
}
function removeFill(event,d) {
d3.selectAll('text.labels').remove()
d3.select(this).style("fill", "white")
d3.select(this) .style('fill-opacity','.01')
d3.select(this).style("stroke-width", ".5")
d3.select(this).style("stroke", "black")
d3.select(this).style("stroke-opacity", ".5")
}
var c = svg.selectAll('circle')
.attr('cx',190)
.attr('cy',690)
.attr('r',180)
.style("fill", 'none')
.attr('stroke-width','.4')
.attr('stroke-opacity','.1')
.style('stroke','#DC7358')
.style('stroke-dasharray',"8")
var c = svg.selectAll("circle")
.data(nycSchools.features)
.enter()
.append("circle")
.attr('cx',function(d) {return path.centroid(d)[0]})
.attr('cy',function(d) {return path.centroid(d)[1]})
.attr('r',1)
.style("fill", "white")
.style('fill-opacity','1')
.style("stroke-width", "1")
.style("stroke", "white")
var c = svg.selectAll("circle")
.data(nycCrimes.features)
.enter()
.append("circle")
.attr('cx',function(d) {return path.centroid(d)[0]})
.attr('cy',function(d) {return path.centroid(d)[1]})
.attr('r',3)
.style("fill", "red")
.style('fill-opacity','.8')
.on('mouseover',addBox)
.on('mouseout',removeBox)
var tooltip = svg.append("g");
function addBox(event, d) {
var centroid = path.centroid(d);
tooltip.call(
callout,
`${d.properties.schools}
${d.properties.address}
${d.properties.schools_in}`
);
tooltip.attr("transform", "translate(" + centroid + ")");
}
function removeBox(event, d) {
tooltip.call(callout,null)
}
return svg.node();
}