Published
Edited
Dec 7, 2021
Fork of starter map
Insert cell
# NYC interactive map
Insert cell
zips = FileAttachment("zip_test4.geojson").json()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 960,
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [-60, 0, width, height]);

// Use Mercator projection
var projection = d3
.geoMercator()
.fitSize([width - 50, height - 50], zips);

var path = d3.geoPath().projection(projection);
var path2 = d3.geoPath().projection(projection);

//add line from label to PO
svg.selectAll('line')
.data(post_office.features)
.enter()
.append('line')
.attr('x1',function(d) {return path.centroid(d)[0]})
.attr('y1',function(d) {return path.centroid(d)[0]})
.attr('x2',52)
.attr('y2',function(d) {return path.centroid(d)[1]})
.attr('stroke-width','0.4')
.attr('stroke-opacity','0.08')
.style('stroke','rgb(0,0,0)')

var g = svg.append("g").attr("id", "paths");
g.selectAll("path") //d3 geopath
//svg
//.selectAll('path')
.data(zips.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("path") //appends path to data
.attr('class','outlines')
.attr("d", path) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "grey")
.style('fill-opacity','.5')
.style("stroke-width", "1")
.style("stroke", "#ccc")


//insert parks
g.selectAll("path") //d3 geopath
//svg
//.selectAll('path')
.data(nycp.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("path") //appends path to data
.attr('class','outlines')
.attr("d", path) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "green")
.style('fill-opacity','.2')
.style("stroke-width", "0.2")
.style("stroke", "green")
.on('mouseover',addFill)
.on('mouseleave',removeFill)


//insert Subway line
g.selectAll("path2") //d3 geopath
//svg
//.selectAll('path')
.data(SubwayLine.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("path") //appends path to data
.attr('class','outlines')
.attr("d", path) //The d attribute defines a path to be drawn, only applies to appended elements
//.style("fill", "blue")
.style('fill-opacity','0')
.style("stroke-width", "0.5")
.style("stroke", "blue")


//insert Subway stations
var c = svg.selectAll("circle") //d3 geopath
//svg
//.selectAll('path')
.data(Subway_sts.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr('cx',function(d) {return path.centroid(d)[0]})
.attr("cy", function(d) {return path.centroid(d)[1]})
.attr('r',2)
.attr('fill-opacity','0.5')
.attr('fill','blue')
.on('mouseover',addBox)
.on('mouseout',removeBox)


//insert post office
c.enter().append('circle')
//svg
//.selectAll('path')
.data(post_office.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr('cx',function(d) {return path.centroid(d)[0]})
.attr("cy",function(d) {return path.centroid(d)[1]})
.attr('r',3)
//.style("fill", "red")
.attr('fill-opacity','0.90')
.attr('fill','yellow')
.on('mouseover',addRack)
.on('mouseout',removeBox)

//add PO text
svg.selectAll('text')
.data(post_office.features)
.enter()
.append('text')
.attr('x','52')
.attr('y',function(d,i){return i*3.2+10})
.attr('font-size','0.15em')
.attr('text-anchor','end')
.text(function(d){return d.properties.name})



//Add park labels and park fill
function addFill(event,d) {
d3.select(this).style("fill", "black")
d3.select(this).style('fill-opacity','0.8')

svg
.append('text')
.attr('class','labels')
.attr('x','100')
.attr('y','100')
.attr('font-size','0.85em')
.text(d.properties.eapply)
}
//take away park labels/park fills
function removeFill(event,d) {
d3.selectAll('text.labels').remove()
d3.select(this).style("fill", "red")
d3.select(this).style('fill-opacity','0.5')
}

//add subway stop tool tip
var tooltip = svg.append("g");
function addBox(event, d) {
var centroid = path.centroid(d);
tooltip.call(
callout,
`${d.properties.name}
${d.properties.line}, ${d.properties.line}`
);
tooltip.attr("transform", "translate(" + centroid + ")");
}

//Add post office box
//var tooltip = svg.append("g");
function addRack(event, d) {
var centroid = path.centroid(d);
tooltip.call(
callout,
`${d.properties.name}
${d.properties.streetname}`
);
tooltip.attr("transform", "translate(" + centroid + ")");
}
function removeBox(event, d) {
//console.log('hello')
tooltip.call(callout,null)
}

return svg.node();
}
Insert cell
callout = (g, value) => {
if (!value) return g.style("display", "none");

g
.style("display", null)
.style("pointer-events", "none")
.style("font", "10px sans-serif");

const path = g.selectAll("path")
.data([null])
.join("path")
.attr("fill", "white")
.attr("stroke", "black");

const text = g.selectAll("text")
.data([null])
.join("text")
.call(text => text
.selectAll("tspan")
.data((value + "").split(/\n/))
.join("tspan")
.attr("x", 0)
.attr("y", (d, i) => `${i * 1.1}em`)
.style("font-weight", (_, i) => i ? null : "bold")
.text(d => d));

const {x, y, width: w, height: h} = text.node().getBBox();

text.attr("transform", `translate(${-w / 2},${15 - y})`);
path.attr("d", `M${-w / 2 - 10},5H-5l5,-5l5,5H${w / 2 + 10}v${h + 20}h-${w + 20}z`);
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more