Public
Edited
Sep 21, 2023
Insert cell
# My Personal Map - UD Fall 2023
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 960,
height = 660;
const svg = d3.create("svg")
.attr("viewBox", [50, 25, width-100, height-100]);

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


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");

svg
.append("text")
.attr('x','75')
.attr('y','70')
.style('font-family','courier new')
.style('font-size','32px')
.style('font-weight','bold')//use 'fill' to change font color
.text('PERSONAL MAP')

svg
.append("text")
.attr('x','75')
.attr('y','90')
.style('font-family','courier new')
.style('font-size','12px')
.style('font-weight','light')//use 'fill' to change font color
.text('NYC in a weekend')



g.selectAll("path2") //d3 geopath
.data(subwayLines.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','.3')
.style("stroke-width", '1.5')
.style("stroke", "rgb(222,118,16)")

g.selectAll("path3") //d3 geopath
.data(buildings.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", ".1")
.style('stroke-opacity','.2')
.style("stroke-width", '.1')
.style("stroke", "rgb(0,0,0)")

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


var c = svg.selectAll("circle") //use this line the first time we create a circle
.data(artgalleries.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','grey')
.style('stroke-opacity','.2')
.style('stroke','black')
.style('stroke-width','0.25')
.style('fill-opacity','.2')
.on('mouseover',galleryText)
.on('mouseout',removeGalText)
function galleryText(event,d){
console.log(d)
svg
.append("text")
.attr('x','750')
.attr('y','500')
.attr('class','gallery_position')
.style('font-family','courier new')
.style('font-size','16px')
.style('font-weight','bold')//use 'fill' to change font color
.style('fill-opacity','.5')
.text(d.properties.name)

p.enter().append("polyline")
.data(GalIcon)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.attr('class','Artline')
.attr('fill','none')
.style('stroke-opacity','.5')
.style('stroke','black')
.style('stroke-width','.5')

p.enter().append("polyline")
.data(frameRollover)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.attr('class','Frameline')
.attr('fill','none')
.style('stroke-opacity','.5')
.style('stroke','black')
.style('stroke-width','2')

p.enter().append("polyline")
.data(peopleFill)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.attr('class','PFill')
.attr('fill','grey')
.style('fill-opacity','.5')
.style('stroke','none')
.style('stroke-width','2')

p.enter().append("polyline")
.data(artFill)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.attr('class','ArtFill')
.attr('fill','grey')
.style('fill-opacity','.5')
.style('stroke','none')
.style('stroke-width','2')

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

function removeGalText(){
d3.selectAll('text.gallery_position').remove()
d3.selectAll('polyline.Artline').remove()
d3.selectAll('polyline.Frameline').remove()
d3.selectAll('polyline.PFill').remove()
d3.selectAll('polyline.ArtFill').remove()
}

c.enter().append("circle")//use this line everytime after we create a circle
.data(substations.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','rgb(222,118,16)')
.style('fill-opacity','1')

c.enter().append("circle")//use this line everytime after we create a circle
.data(personal_spots)
.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',4)
.attr('fill','purple')
.style('stroke','black')
.style('stroke-width','0.25')
.style('fill-opacity','1')
.on('mouseover',SpotsText)
.on('mouseout',removeSpotsText)


function SpotsText(event,d){
console.log(d)

p.enter().append("polyline")
.data(spotsHighlight)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.attr('class','SHiLite')
.attr('fill','purple')
.style('fill-opacity','0.1')
.style('stroke-opacity','0.5')
.style('stroke','purple')
.style('stroke-width','4')

svg
.append("text")
.attr('x', projection([d.long,d.lat])[0]+12)
.attr('y', projection([d.long,d.lat])[1]-7)
.attr('class','Spots_position')
.style('font-family','courier new')
.style('font-size','16px')
.style('font-weight','bold')//use 'fill' to change font color
.text(d.name)//don't need 'properties' when you're trying to grab data from a spreadsheet


svg
.append("text")
.attr('x','75')
.attr('y','70')
.attr('class','T_Txt')
.style('font-family','courier new')
.style('font-size','32px')
.style('font-weight','bold')//use 'fill' to change font color
.text('PERSONAL MAP')

svg
.append("text")
.attr('x','75')
.attr('y','90')
.attr('class','ST_Txt')
.style('font-family','courier new')
.style('font-size','12px')
.style('font-weight','light')//use 'fill' to change font color
.text('NYC in a weekend')

svg
.append("text")
.attr('x', 760)
.attr('y', 70)
.attr('class','Des_position')
.style('font-family','courier new')
.style('font-size','12px')
.style('font-weight','light')//use 'fill' to change font color
.text(d.description)//don't need 'properties' when you're trying to grab data from a spreadsheet

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

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

/* svg
.append("line")
.attr('x1','180')
.attr('y1','150')
.attr('x2', projection([d.long,d.lat])[0])
.attr('y2', projection([d.long,d.lat])[1])
.attr('class','Sline')
.attr('stroke-dasharray',"4 2")
.style("stroke-width", '0.5')
.style("stroke", "rgb(0,0,0)")
*/
}

function removeSpotsText(){
d3.selectAll('text.Spots_position').remove()
d3.selectAll('line.Sline').remove()
d3.selectAll('polyline.SHiLite').remove()
d3.selectAll('text.T_Txt').remove()
d3.selectAll('text.ST_Txt').remove()
d3.selectAll('text.Des_position').remove()
}



var c = svg.selectAll("text") //use this line the first time we create a circle
.data(substations.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]-5})
.style('font-family','courier new')
.style('font-size','4px')
.style('text-anchor','middle')
.style('font-weight','light')//use 'fill' to change font color
.text(function(d){return d.properties.name})




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

var p = svg.selectAll("polyline") //use this line the first time we create a polyline
// .data(HHiLite)
// .enter() //there are more data than elements, this selects them
// .append("polyline") //appends path to data
// .attr("points", function(d) {return d})
// .attr('fill','white')
// .style('fill-opacity','.5')
// .style('stroke','none')
// .style('stroke-width','0.5')

/*p.enter().append("polyline")
.data(test2)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.attr('fill','white')
.style('fill-opacity','0.2')
.style('stroke','white')
.style('stroke-width','0.5')

*/

return svg.node();
}
Insert cell
boundingBox = FileAttachment("bounding box.geojson").json()
Insert cell
parks = FileAttachment("parks.geojson").json()
Insert cell
substations = FileAttachment("substations.geojson").json()
Insert cell
artgalleries = FileAttachment("art galleries.geojson").json()
Insert cell
buildings = FileAttachment("NYC Building Footprints 2.geojson").json()
Insert cell
subLns = FileAttachment("subway_lines.geojson").json()
Insert cell
mapoutline = FileAttachment("personal map outline.geojson").json()
Insert cell
subwayLines = FileAttachment("subway lines.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
test2 = FileAttachment("test2.txt").tsv({array:true})
Insert cell
test3 = FileAttachment("test3@1.txt").tsv({array:true})
Insert cell
HHiLite = FileAttachment("Heading highlight.txt").tsv({array:true})
Insert cell
GalIcon = FileAttachment("art galleries rollover@1.txt").tsv({array:true})
Insert cell
spotsHighlight = FileAttachment("Spots Highlight@1.txt").tsv({array:true})
Insert cell
frameRollover = FileAttachment("frame rollover.txt").tsv({array:true})
Insert cell
artFill = FileAttachment("art fill.txt").tsv({array:true})
Insert cell
peopleFill = FileAttachment("people fill.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