Public
Edited
Jul 11, 2023
1 fork
Insert cell
# Assignment10_Final Map
Insert cell
d3 = require("d3@7","d3-geo-scale-bar@1.2","d3-tile@1")
Insert cell
scaleBar=d3.geoScaleBar()
.top(.9)
.left(.08)
.units({units:"500 Feet", radius:20902259.664})
.distance(500)
.labelAnchor("start")
.tickPadding(3) // How far the tick text labels are from the lines
.tickValues(null)

Insert cell
chart = {
const width = 960,
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [50, 50, width-140, height-105]);

// Use Mercator projection
var projection = d3
.geoMercator()
.fitSize([width - 50, height - 50], bbox);
var path0 = d3.geoPath().projection(projection);
var path1 = d3.geoPath().projection(projection);
var path2 = d3.geoPath().projection(projection);
var path3 = d3.geoPath().projection(projection);
var path4 = d3.geoPath().projection(projection);
var path5 = d3.geoPath().projection(projection);

var g = svg.append("g").attr("id", "paths");
g.selectAll("path0") //d3 geopath
.data(bbox.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", path0) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "rgb(159,181,175)")
.style("fill-opacity", ".3")

g.selectAll("path1") //d3 geopath
.data(land.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", path1) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "rgb(255,255,255)")
.style("fill-opacity", "1")
.style('stroke-opacity','.4')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")
//Park
g.selectAll("path2") //d3 geopath
.data(park.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", path2) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "rgb(143,165,150)") //200,212,186
.style("fill-opacity", ".5")
.style('stroke-opacity','.4')
.style("stroke-width", '.0')
.style("stroke", "rgb(0,0,0)")
.on('mouseover',ParkOn)
.on('mouseout',ParkOff)

function ParkOn(event,d){
d3.select(this).style("fill-opacity", "1")
/*
svg.selectAll("text")
.data(park.features)
.enter() //there are more data than elements, this selects them
.append("text") //appends path to data
.attr("x", function(d) {return path2.centroid(d)[0]})
.attr("y", function(d) {return path2.centroid(d)[1]})
.attr('font-family','helvetica')
.attr('font-size','.3em')
.attr('font-weight','bold')
.style("fill", "rgb(255,255,255)")
.style('fill-opacity','1')
.attr('text-anchor','start')
.text(function(d) {return d.properties.park_name})
.attr('class','Parkname')
*/
}

function ParkOff(event,d){
d3.select(this).style("fill-opacity", ".5")
// d3.selectAll('text.Parkname').remove()
}
//Subline
g.selectAll("path3") //d3 geopath
.data(SubLns.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", path3) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "none")
.style('stroke-opacity','1')
.style("stroke-width", '1')
.style("stroke", "rgb(89,135,149)")

g.selectAll("path4") //d3 geopath
.data(Block.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", path4) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "rgb(240,240,240)")
.style("fill-opacity", ".5")
.style('stroke-opacity','.4')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")

g.selectAll("path5") //d3 geopath
.data(rt.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", path5) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "none")
.style('stroke-opacity','1')
.style("stroke-width", '2')
.style("stroke", "rgb(0,0,0)")
.attr('stroke-dasharray','5 5')
//text
svg
.append("text")
.attr('class','title')
.attr('x','70')
.attr('y','73')
.attr('font-family','helvetica')
.attr('font-size','lem')
.attr('font-weight','bold')
.style("fill", "rgb(0,0,0)")
.text('Picnic Map for Lower Manhattan')

var c = svg.selectAll("circle") //d3 geopath
.data(sub_stat.features)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return path1.centroid(d)[0]})
.attr("cy", function(d) {return path1.centroid(d)[1]})
.attr('r',3)
.attr('fill','rgb(89,135,149)')
.attr('stroke-width','2')
.attr('stroke','rgb(0,0,0)')
.style('fill-opacity','1')
.on('mouseover',StatOn)
.on('mouseout',StatOff)

function StatOn(event,d){
svg
.append('circle')
.attr("cx", path1.centroid(d)[0])
.attr("cy", path1.centroid(d)[1])
.attr('r',100)
.attr('fill', "rgb(200,200,200)")
.attr('stroke-width','.5')
.attr('stroke','rgb(0,0,0)')
.attr('stroke-dasharray','2 2')
.style('fill-opacity','.3')
.attr('class','CircleStat')
}
function StatOff(event,d){
d3.selectAll('circle.CircleStat').remove()
}
//NYC Link
c.enter().append('circle')
.data(nyclink.features)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return path1.centroid(d)[0]})
.attr("cy", function(d) {return path1.centroid(d)[1]})
.attr('r',2)
.attr('fill','rgb(161,123,119)')
.style('fill-opacity','1')
//Cafe Shop_Interact
c.enter().append('circle') //d3 geopath
.data(Cafe)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return projection([d.long,d.lat])[0]})
.attr("cy", function(d) {return projection([d.long,d.lat])[1]})
.attr('r',2)
.attr('fill','rgb(100,100,100)')
.style('fill-opacity','1')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")
.on('mouseover',CafeOn)
.on('mouseout',CafeOff)

function CafeOn(event,d){
d3.select(this).style("stroke-width", '3')

svg
.append('rect')
.attr('fill', "rgb(150,150,150)")
.attr('x', projection([d.long,d.lat])[0]-75)
.attr('y', projection([d.long,d.lat])[1]-27)
.attr('width', 150)
.attr('height', 32)
.attr('class','BgDetail')
.style("fill-opacity", ".75")

svg
.append('text')
.attr("x", projection([d.long,d.lat])[0])
.attr("y", projection([d.long,d.lat])[1]-13)
.attr('class','Details')
.attr("font-family", "Helvetica")
.attr('font-size','.6em')
//.style("font-weight","bold")
//.style("font-style","italic")
.style("fill",'rgb(255,255,255)')
.attr('text-anchor','middle')
.text(d.desc)

var wrap = svg.selectAll('text.Details')
.each(function(d, i) { wrap_text(d3.select(this), 150) });
}

function CafeOff(event,d){
d3.select(this).style("stroke-width", '.5')
d3.selectAll('text.Details').remove()
d3.selectAll('rect.BgDetail').remove()
}
//Station Name
var t = svg.selectAll("text")
.data(sub_stat.features)
.enter() //there are more data than elements, this selects them
.append("text") //appends path to data
.attr("x", function(d) {return path1.centroid(d)[0]})
.attr("y", function(d) {return path1.centroid(d)[1]-10})
.attr('font-family','helvetica')
.attr('font-size','.5em')
.attr('font-weight','bold')
.style("fill", "rgb(0,0,0)")
.style('fill-opacity','.7')
.attr('text-anchor','end')
.text(function(d) {return d.properties.name})


//Cafeshop
t.enter().append('text')
.data(Cafe)
.enter()
.append('text')
.attr('class','Cafename')
.attr('x','180')
.attr('y',function(d, i) { return i * 30 + 100;})
.attr("font-family", "Helvetica")
.attr('font-size','.55em')
.attr('text-anchor','end')
.style("font-weight","bold")
.style("fill","rgb(100,100,100)")
.text(function(d) {return d.name})

t.enter().append('text')
.data(Cafe)
.enter()
.append('text')
.attr('class','CafeLoc')
.attr('x','180')
.attr('y',function(d, i) { return i * 30 + 108;})
.attr("font-family", "Helvetica")
.attr('font-size','.4em')
.attr('text-anchor','end')
.style("font-weight","normal")
.style("fill","rgb(100,100,100)")
.text(function(d) {return d.loc})

t.enter().append('line')
.data(Cafe)
.enter()
.append('line')
.attr('class','annotation_line')
.attr('x1','183')
.attr('y1',function(d, i) { return i * 30 + 97 ;})
.attr('x2',function(d) {return projection([d.long,d.lat])[0]})
.attr('y2',function(d, i) { return i * 30 + 97 ;})
.attr('stroke-width','.5')
.attr('stroke-opacity','1')
.attr('stroke-dasharray',"2 2")
.style('stroke','rgb(100,100,100)')

t.enter().append('line')
.data(Cafe)
.enter()
.append('line')
.attr('class','annotation_line')
.attr('x1',function(d) {return projection([d.long,d.lat])[0]})
.attr('y1',function(d, i) { return i * 30 + 97 ;})
.attr('x2',function(d) {return projection([d.long,d.lat])[0]})
.attr('y2',function(d) {return projection([d.long,d.lat])[1]})
.attr('stroke-width','.5')
.attr('stroke-opacity','1')
.attr('stroke-dasharray',"2 2")
.style('stroke','rgb(100,100,100)')

//Legend
svg
.append('rect')
.attr('fill', "rgb(255,255,255)")
.attr("fill-opacity", ".5")
.attr('x', 700)
.attr('y', 670)
.attr('width', 160)
.attr('height', 160)
svg
.append("circle") //appends path to data
.attr("cx", 720)
.attr("cy", 690)
.attr('r',2)
.attr('fill','rgb(161,123,119)')
.style('fill-opacity','1')

svg
.append("circle") //appends path to data
.attr("cx", 720)
.attr("cy", 710)
.attr('r',2)
.attr('fill','rgb(100,100,100)')
.style('fill-opacity','1')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")

svg
.append("circle") //appends path to data
.attr("cx", 720)
.attr("cy", 730)
.attr('r',3)
.attr('fill','rgb(89,135,149)')
.attr('stroke-width','2')
.attr('stroke','rgb(0,0,0)')
.style('fill-opacity','1')
svg
.append('line')
.attr('x1',715)
.attr('y1',750)
.attr('x2',725)
.attr('y2',750)
.style('stroke-opacity','1')
.style("stroke-width", '1')
.style("stroke", "rgb(89,135,149)")
svg
.append('line')
.attr('x1',715)
.attr('y1',770)
.attr('x2',725)
.attr('y2',770)
.style('stroke-opacity','1')
.style("stroke-width", '1')
.style("stroke", "rgb(89,135,149)")
.style('stroke-opacity','1')
.style("stroke-width", '2')
.style("stroke", "rgb(0,0,0)")
.attr('stroke-dasharray','2 2')

svg
.append("circle") //appends path to data
.attr("cx", 720)
.attr("cy", 790)
.attr('r',5)
.attr('fill', "rgb(200,200,200)")
.attr('stroke-width','.5')
.attr('stroke','rgb(0,0,0)')
.attr('stroke-dasharray','2 2')
.style('fill-opacity','.3')
svg
.append('rect')
.style("fill", "rgb(143,165,150)")
.style("fill-opacity", ".5")
.attr('x', 715)
.attr('y', 807)
.attr('width', 10)
.attr('height', 10)

svg
.append("text")
.attr('class','title')
.attr('x','740')
.attr('y','693')
.attr('font-family','helvetica')
.attr('font-size','.5em')
.attr('font-weight','20')
.style("fill", "rgb(100,100,100)")
.text('NYC Safety Link')

svg
.append("text")
.attr('class','title')
.attr('x','740')
.attr('y','713')
.attr('font-family','helvetica')
.attr('font-size','.5em')
.attr('font-weight','20')
.style("fill", "rgb(100,100,100)")
.text('Cafe Shop')

svg
.append("text")
.attr('class','title')
.attr('x','740')
.attr('y','733')
.attr('font-family','helvetica')
.attr('font-size','.5em')
.attr('font-weight','20')
.style("fill", "rgb(100,100,100)")
.text('Subway Station')

svg
.append("text")
.attr('class','title')
.attr('x','740')
.attr('y','753')
.attr('font-family','helvetica')
.attr('font-size','.5em')
.attr('font-weight','20')
.style("fill", "rgb(100,100,100)")
.text('Subway Line')

svg
.append("text")
.attr('class','title')
.attr('x','740')
.attr('y','773')
.attr('font-family','helvetica')
.attr('font-size','.5em')
.attr('font-weight','20')
.style("fill", "rgb(100,100,100)")
.text('Drone Routine')

svg
.append("text")
.attr('class','title')
.attr('x','740')
.attr('y','793')
.attr('font-family','helvetica')
.attr('font-size','.5em')
.attr('font-weight','20')
.style("fill", "rgb(100,100,100)")
.text('300m Dis. Zone')

svg
.append("text")
.attr('class','title')
.attr('x','740')
.attr('y','815')
.attr('font-family','helvetica')
.attr('font-size','.5em')
.attr('font-weight','20')
.style("fill", "rgb(100,100,100)")
.text('Public Park')


//Scale Bar
scaleBar
.projection(projection)
.size([width , height]);
svg.append("g")
.call(scaleBar);

return svg.node();
}
Insert cell
rt = FileAttachment("Routine@1.geojson").json()
Insert cell
land = FileAttachment("Land.geojson").json()
Insert cell
Block = FileAttachment("Block Boundary@1.geojson").json()
Insert cell
bbox = FileAttachment("Bbox.geojson").json()
Insert cell
park = FileAttachment("Park.geojson").json()
Insert cell
sub_stat = FileAttachment("Sub_Stop.geojson").json()
Insert cell
SubLns = FileAttachment("Subway@1.geojson").json()
Insert cell
CafeMap = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vSFuL2_Ovgrbc7ZCu15PHRHsgBkfLEahMTJSMmU7HxST-hnYtrNpH-xG3xFfb08VRZjN5PpznVBQx2S/pub?output=csv'
Insert cell
Cafe = d3.csv(CafeMap, d3.autoType)
Insert cell
import { wrap_text, wrap_text_nchar } from "@ben-tanen/svg-text-and-tspan-word-wrapping"
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more