Public
Edited
Jul 20, 2023
Insert cell
# base map - cornell 2023 - fleet's version
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], bbox2);

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(0,0,0)")
.style('stroke-opacity','1')
.style("stroke-width", '.75')
.style("stroke", "rgb(0,0,0)")

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(240,240,240)")
.style("fill-opacity", ".5")
.style('stroke-opacity','.4')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")

var p = svg.selectAll("polyline") //d3 geopath
.data(blobs)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.style("stroke","black")
.style("stroke-width","2px")
.style("fill","none")

var t = svg.selectAll("text") //d3 geopath
.data(subSts.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]-4})
.style("font-family","helvetica")
.style("fill","black")
.style("font-weight","600")
.style("font-size","6px")
.style("text-anchor","middle")
.text(function(d) {return d.properties.name})


var c = svg.selectAll("circle") //d3 geopath
.data(subSts.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',1.5)
.attr('fill','red')
.style('fill-opacity','1')
.style("stroke", "rgb(0,0,0)")
.style("stroke-width", '1')

c.enter().append("circle")//after the first use of a circle, use this method to add more
.data(donutData)
.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){
console.log(d.name)

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.description)

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])
.attr("class","dLine")
.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()
svg.selectAll("line.dLine").remove()
}

return svg.node();
}
Insert cell
subSts = FileAttachment("substops.geojson").json()
Insert cell
bldgs = FileAttachment("footprints.geojson").json()
Insert cell
bbox2 = FileAttachment("boundingbox.geojson").json()
Insert cell
subLns = FileAttachment("subway_lines.geojson").json()
Insert cell
parks = FileAttachment("parks.geojson").json()
Insert cell
boundary = FileAttachment("nyc_boundary.geojson").json()
Insert cell
Insert cell
donutData = d3.csv(donutDataLink,d3.autoType)
Insert cell
import { wrap_text, wrap_text_nchar } from "@ben-tanen/svg-text-and-tspan-word-wrapping"
Insert cell
blobs = FileAttachment("linetest.txt").tsv({array:true})
Insert cell
seamonster = FileAttachment("seamonster.txt").tsv({array:true})
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