Public
Edited
Jul 20, 2023
Insert cell
# base map - ipek - cornell 2023
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 960,
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [50, 50, width-100, height-100]);

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

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 g = svg.append("g").attr("id", "paths");

g.selectAll("path2") //d3 geopath
.data(parks.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(156,255,163)")
.style('stroke-opacity','1')
.style("stroke-width", '.75')
.style("stroke", "rgb(156,255,163)")

g.selectAll("path3") //d3 geopath
.data(bldgs.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", "rgb(249,195,201)")
.style("fill-opacity", ".5")
.style('stroke-opacity','.4')
.style("stroke-width", '.5')
.style("stroke", "rgb(237,117,148)")


var c = svg.selectAll("circle") //d3 geopath
.data(wwfacility.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',5)
.attr('fill', "rgb(88,95,169)")
.style('fill-opacity','1')

c.enter().append("circle")//after the first use of a circle, use this method to add more
.data(wwdata)
.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',"3")
.style('fill','tan')
.style('fill-opacity','1')
.style("stroke", "rgb(0,0,0)")
.style("stroke-width", '1')
.on("mouseover",doughHover)
.on("mouseout",doughRemove)

function doughHover(i,d){
svg.append("text")
.attr("class","dText")
.attr("x","140")
.attr("y","200")
.style("font-family","helvetica")
.style("font-weight","800")
.style("font-size","8px")
.style("text-anchor","end")
.text(d.name)

svg.append("text")
.attr("class","dText")
.attr("x","140")
.attr("y","210")
.style("font-family","helvetica")
.style("font-weight","200")
.style("font-size","7px")
.style("text-anchor","end")
.text(d.address)
/*
var wrap = svg.selectAll("text.dText")
.each(function(d, i) { wrap_text(d3.select(this), 100) });

svg.append("line")
.attr("x1","143")
.attr("y1","197")
.attr("x2",projection([d.long,d.lat])[0])
.attr("y2",projection([d.long,d.lat])[1])
.style("stroke","black")
.style("stroke-width","2px")
*/
/*
p.enter().append("polyline") //d3 geopath
.data(seamonster)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("class","seamonster")
.attr("points", function(d) {return d})
.style("stroke","black")
.style("stroke-width","2px")
.style("fill","none")
*/
}

function doughRemove(){
svg.selectAll("text.dText").remove()
svg.selectAll("polyline.seamonster").remove()
}
return svg.node();
}
Insert cell
wwfacility = FileAttachment("wastewater.geojson").json()
Insert cell
bldgs = FileAttachment("buildings.geojson").json()
Insert cell
wwdata = d3.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vQYfvd4RpieXBYZaDbgtl388EbV1kYi_UC2_BJdfgLfxYGPSGkBRQ3NxkVH4KMZHKD5SkaaqyIYsSWE/pub?output=csv")
Insert cell
bbox = FileAttachment("bbox.geojson").json()
Insert cell
parks = FileAttachment("green.geojson").json()
Insert cell
boundary = FileAttachment("0_nyc.geojson").json()
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