Public
Edited
Sep 28, 2023
Insert cell
# base map - UD Fall 2023 - play
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 960,
height = 960;
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], map_outline);

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


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


g.selectAll("path3") //d3 geopath
.data(bldgs1.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)")

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',3)
.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',3)
.style('stroke','black')
.style('stroke-width','1')
.style('fill','cyan')
.style('fill-opacity','1')

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

svg
.append("text")
.attr('x','100')
.attr('y','100')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('my great 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('this is a personal map')

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('waterfountain locations')

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

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
subSts = FileAttachment("sub_stops.geojson").json()
Insert cell
map_outline = FileAttachment("personal_map_outline.geojson").json()
Insert cell
fountains = FileAttachment("fountains.geojson").json()
Insert cell
galleries = FileAttachment("galleries.geojson").json()
Insert cell
subways = FileAttachment("subways.geojson").json()
Insert cell
bldgs1 = FileAttachment("bldgs@1.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

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