Public
Edited
Sep 15, 2023
Insert cell
# base map - UD Fall 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-150, height-150]);

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

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(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", path2) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "none")
.style('stroke-opacity','1')
.style("stroke-width", '.75')
.style("stroke", "rgb(0,0,0)")

g.selectAll("path3") //d3 geopath
.data(buildingoutline.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", '.15')
.style("stroke", "rgb(0,0,0)")

var c = svg.selectAll("circle") //use this line the first time we create a circle
.data(library.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]})//we use a function in order to draw ALL of the fountains - loop through the fountains
.attr("cy", function(d) {return path1.centroid(d)[1]})
.attr('r',3)
.style('stroke','black')
.style('stroke-width','1')
.attr('fill','cyan')
.style('fill-opacity','1')
.on('mouseover',LibraryText)
.on('mouseout',removeLibraryText)

function LibraryText(event,d){
//console.log(d)

svg
.append("text")
.attr('x','100')
.attr('y','220')
.attr('class','library_position')
.style('font-family','helvetica')
.style('font-size','10px')
.style('font-weight','light')//use 'fill' to change color
.text(d.properties.Position)

var wrap = svg.selectAll("text.library_position")
.each(function(d, i) { wrap_text(d3.select(this), 75) });//control how many pixels wide our text block can be

svg
.append("line")
.attr('x1','120')
.attr('y1','180')
.attr('x2', path1.centroid(d)[0])
.attr('y2', path1.centroid(d)[1])
.attr('class','fLine')
.style('stroke-width', '1')
.style("stroke", "rgb(0,0,0)")
.style('stroke-dasharray', '4')

p.enter().append("polyline") //use this line the first time we create a polyline
.data(text)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})//we use a function in order to draw ALL of the fountains - loop through the fountains
.attr("class", 'book')
.style('fill','black')
.style('stroke','black')
.style('stroke-width','.5')
}//this is where LibraryText function ends

function removeLibraryText(){
d3.selectAll('text.library_position').remove()//geometry type, then class name
d3.selectAll('line.fLine').remove()
d3.selectAll('polyline.book').remove()
}

c.enter().append("circle") // use this line everytime after the first time we create a circle
.data(gallery.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)
.style('stroke','black')
.style('stroke-width','1')
.attr('fill','pink')
.style('fill-opacity','1')
.on('mouseover',galleryText)
.on('mouseout',removeGalleryText)

function galleryText(event,d){
//console.log(d)

svg
.append("text")
.attr('x','120')
.attr('y','200')
.attr('class','Gallery_position')
.style('font-family','helvetica')
.style('font-size','10px')
.style('font-weight','light')//use 'fill' to change color
.text(d.properties.Position)

var wrap = svg.selectAll("text.Gallery_position")
.each(function(d, i) { wrap_text(d3.select(this), 75) });//control how many pixels wide our text block can be

svg
.append("line")
.attr('x1','120')
.attr('y1','190')
.attr('x2', path1.centroid(d)[0])
.attr('y2', path1.centroid(d)[1])
.attr('class','fLine')
.style('stroke-width', '1')
.style("stroke", "rgb(0,0,0)")
.style('stroke-dasharray', '4')

p.enter().append("polyline") //use this line the first time we create a polyline
.data(text2)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})//we use a function in order to draw ALL of the fountains - loop through the fountains
.attr("class", 'artwork')
.style('fill','none')
.style('stroke','black')
.style('stroke-width','.5')
}//this is where GalleryText function ends

function removeGalleryText(){
d3.selectAll('text.Gallery_position').remove()//geometry type, then class name
d3.selectAll('line.fLine').remove()
d3.selectAll('polyline.artwork').remove()
}


c.enter().append("circle") //d3 geopath
.data(substop.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','black')
.style('fill-opacity','1')

svg
.append("text")
.attr('x','100')
.attr('y','95')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('Lu_Zexuan_Manhattan_NY_Personal Map')

svg
.append("text")
.attr('x','100')
.attr('y','120')
.style('font-family','helvetica')
.style('font-size','10px')
.style('font-weight','light')//use 'fill' to change color
.text('Fall_2023_Urban Data')

svg
.append("text")
.attr('x','310')
.attr('y','380')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('Harbs')

svg
.append("text")
.attr('x','730')
.attr('y','295')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('CheLi')

svg
.append("text")
.attr('x','740')
.attr('y','270')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('Mountain House')

svg
.append("text")
.attr('x','420')
.attr('y','240')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('New York University')

svg
.append("text")
.attr('x','460')
.attr('y','380')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('Soho')

svg
.append("text")
.attr('x','670')
.attr('y','280')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('Gong Cha')

svg
.append("line")
.attr('x1','100')
.attr('y1','104')
.attr('x2','200')
.attr('y2','104')
.style("stroke-width", '1')
.style("stroke", "rgb(0,0,0)")

svg
.append("text")
.attr('x','100')
.attr('y','180')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('Library')

svg
.append("text")
.attr('x','100')
.attr('y','200')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('Gallery')

svg
.append("text")
.attr('x','100')
.attr('y','220')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('The meaningful Place')

var p = svg.selectAll("polyline") //use this line the first time we create a polyline
.data(text1)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})//we use a function in order to draw ALL of the fountains - loop through the fountains
.style('fill','grey')
.style('stroke','black')
.style('stroke-width','2')
return svg.node();
}
Insert cell
fountains = FileAttachment("fountains.geojson").json()
Insert cell
b = FileAttachment("b.geojson").json()
Insert cell
buildingoutline = FileAttachment("buildingoutline.geojson").json()
Insert cell
library = FileAttachment("library.geojson").json()
Insert cell
subwayline = FileAttachment("Subwayline.geojson").json()
Insert cell
Insert cell
substop = FileAttachment("substop.geojson").json()
Insert cell
Insert cell
personal_spots = d3.csv(personal_spots_link,d3.autoType)
Insert cell
import { wrap_text, wrap_text_nchar } from "@ben-tanen/svg-text-and-tspan-word-wrapping"
Insert cell
text = FileAttachment("text.txt").text()
Insert cell
text2 = FileAttachment("text2@1.txt").tsv({array:true})
Insert cell
text1 = FileAttachment("text1.txt").tsv({array:true})//end text file line with this code
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