Public
Edited
Dec 17, 2023
Insert cell
# base map - UD Fall 2023 - Fleet's copy
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 960,
height = 960;
const svg = d3.create("svg")
.attr("viewBox", [70,80, width-800, height-800]);

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

var path1 = d3.geoPath().projection(projection);//any path that comes from qgis
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("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','.2')
.style("stroke-width", '.25')
.style("stroke", "rgb(0,0,0)")


g.selectAll("path3") //d3 geopath
.data(buildingFootprints.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','.1')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")
g.selectAll("path4") //d3 geopath
.data(streets.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','.75')
.style("stroke-width", '.25')
.style("stroke", "rgb(0,0,0)")

g.selectAll("path5") //d3 geopath
.data(borough.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','.70')
.style("stroke-width", '.25')
.style("stroke", "rgb(0,0,0)")

var c = svg.selectAll("circle") //use this line the first time we create a circle
.data(fountains.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',1)
.style('fill','black')
.style('fill-opacity','1')
.on('mouseover',fountainText)
.on('mouseout',removeFountainText)

function fountainText(event,d){//this is everything that happens when we hover over a fountain
//console.log(d)

svg
.append("text")
.attr('x','100')
.attr('y','220')
.attr('class','fountain_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.fountain_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','240')
.attr('y1','195')
.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', '6 4')

p.enter().append("polyline") //use this line the first time we create a polyline
.data(f1)
.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", 'fountain')
.style('fill','black')
.style('stroke','none')
.style('stroke-width','.5')

p.enter().append("polyline") //use this line the first time we create a polyline
.data(f2)
.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", 'fountain')
.style('fill','none')
.style('stroke','blue')
.style('stroke-width','2')
}//this is where fountainText function ends

function removeFountainText(){
d3.selectAll('text.fountain_position').remove()//geometry type, then class name
d3.selectAll('line.fLine').remove()
d3.selectAll('polyline.fountain').remove()
}

c.enter().append("circle") // use this line everytime after the first time we create a circle
.data(galleries.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')
.style('fill','red')
.style('fill-opacity','1')

c.enter().append("circle") // use this line everytime after the first time 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]})//projection([d.long,d.lat])[0]
.attr("cy", function(d) {return projection([d.LONG,d.LAT])[1]})
.attr('r',1)
.style('stroke','black')
.style('stroke-width','.25')
.style('fill','cyan')
.style('fill-opacity','1')


svg
.append("text")
.attr('x','70')
.attr('y','90')
.style('font-family','georgia')
.style('font-size','7px')
.style('font-weight','bold')//use 'fill' to change color
.text('MARINAS MAP')

svg
.append("text")
.attr('x','70')
.attr('y','93')
.style('font-family','georgia')
.style('font-size','2px')
.style('font-weight','light')//use 'fill' to change color
.text('IN THE BEAUTIFUL CHELSEA ART DISTRICT')

svg
.append("line")
.attr('x1','70')
.attr('y1','94')
.attr('x2','170')
.attr('y2','94')
.style("stroke-width", '.25')
.style("stroke", "rgb(0,0,0)")

var p = svg.selectAll("polyline") //use this line the first time we create a polyline
.data(test1)
.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','none')
.style('stroke','black')
.style('stroke-width','2')

p.enter().append("polyline") //use this line the first time we create a 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})//we use a function in order to draw ALL of the fountains - loop through the fountains
.style('fill','cyan')
.style('stroke','black')
.style('stroke-width','2')



return svg.node();
}
Insert cell
borough = FileAttachment("BOROUGH.geojson").json()
Insert cell
streets = FileAttachment("STREETS.geojson").json()
Insert cell
buildingFootprints = FileAttachment("BUILDING FOOTPRINTS.geojson").json()
Insert cell
subwayLines = FileAttachment("SUBWAY LINES.geojson").json()
Insert cell
boundingBox = FileAttachment("BOUNDING BOX.geojson").json()
Insert cell
fountains = FileAttachment("fountains.geojson").json()
Insert cell
galleries = FileAttachment("galleries.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
test1 = FileAttachment("test1.txt").tsv({array:true})//end text file line with this code
Insert cell
test2 = FileAttachment("test2.txt").tsv({array:true})
Insert cell
f1 = FileAttachment("f1@1.txt").tsv({array:true})
Insert cell
f2 = FileAttachment("f2@1.txt").tsv({array:true})
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