Public
Edited
Sep 20, 2023
Insert cell
# base map - UD Fall 2023 - Max Roy
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 960,
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [-100, -100, width+200, height+200]);

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

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(Roads.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", '1.5')
.style("stroke", "rgb(220,220,220)")

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


var c = svg.selectAll("circle") //d3 geopath
.data(genHydrants.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','gray')
.style('fill-opacity','.2')

c.enter().append("circle") //d3 geopath
.data(hydrants)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return projection([d.Longitude,d.Latitude])[0]})
.attr("cy", function(d) {return projection([d.Longitude,d.Latitude])[1]})
.attr('r',4)
.attr('fill', function(d) {return d.Color})
.style('fill-opacity','1')
.style("stroke","rgb(255,255,255)")
.on('mouseover',hydrantText)
.on('mouseout',removeText)

var p = svg.selectAll("polyline") //d3 geopath
.data(test1)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.style('fill','white')
.style("stroke", 'white')

p.enter().append("polyline") //d3 geopath
.data(Border)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.style('fill','none')
.style("stroke", 'black')
p.enter().append("polyline") //d3 geopath
.data(hMask)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.style('fill','white')
.style("stroke", 'black')
.style("stroke-width", '2')
p.enter().append("polyline") //d3 geopath
.data(hLine)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.style('fill','none')
.style("stroke", 'black')
.style("stroke-width", '.5')


var t = svg.selectAll("text") //d3 geopath
.data(hydrants)
.enter() //there are more data than elements, this selects them
.append("text") //appends path to data
.attr('x', function(d) {return projection([d.Longitude,d.Latitude])[0]+5})
.attr('y', function(d) {return projection([d.Longitude,d.Latitude])[1]+4})
.style('font-family','georgia')
.style('font-size','10px')
.attr('fill',"rgb(0,0,0)")
.text(function(d) {return d.Manufacturer})
.on('mouseover',hydrantText)
.on('mouseout',removeText)


svg
.append("text")
.attr('x','-15')
.attr('y','-40')
.text('The Oldest On-Street Infrastructure in Newton, Massachusetts')
.style('font-family','georgia')
.style('font-size','25px')
.style('font-weight','bold')
.style('background-color','white')

svg
.append("text")
.attr('x','-15')
.attr('y','-22')
.text('Data as collected by a 10-year-old and recalled by a 22-year-old')
.style('font-family','georgia')
.style('font-size','15px')
.style('background-color','white')
function hydrantText(event,d){
p.enter().append("polyline") //d3 geopath
.data(hide)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.attr('class','temp_hide')
.style('fill','white')
.style("stroke", 'black')
p.enter().append("polyline") //d3 geopath
.data(hide)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})
.attr('class','temp_hide')
.style('fill','none')
.style("stroke", 'black')
svg
.append("text")
.attr('x','10')
.attr('y','60')
.attr('class','hydrant_info')
.text(d.Notes)
.style('font-family','georgia')
.style('font-size','12px')
svg
.append("text")
.attr('x','10')
.attr('y','20')
.attr('class','hydrant_info')
.text(d.Date)
.style('font-family','georgia')
.style('font-size','12px')
svg
.append("text")
.attr('x','10')
.attr('y','40')
.attr('class','hydrant_info')
.text(d.Status)
.style('font-family','georgia')
.style('font-size','12px')
svg
.append("line")
.attr('class','temp_line')
.attr('x1','200')
.attr('y1','25')
.attr('x2', projection([d.Longitude,d.Latitude])[0])
.attr('y2', projection([d.Longitude,d.Latitude])[1])
.style("stroke-width", '1')
.style("stroke", "rgb(0,0,0)")
.style("stroke-dasharray", "10 3")
svg
.append("line")
.attr('class','temp_line')
.attr('x1','200')
.attr('y1','25')
.attr('x2', '150')
.attr('y2', '25')
.style("stroke-width", '1')
.style("stroke", "rgb(0,0,0)")
.style("stroke-dasharray", "10 3")

var wrap = svg.selectAll("text.hydrant_info")
.each(function(d, i) {wrap_text(d3.select(this), 100)});
}

function removeText(){
d3.selectAll("text.hydrant_info").remove()
d3.selectAll("line.temp_line").remove()
d3.selectAll("polyline.temp_hide").remove()

}

svg
.append("line")
.attr('x1','60')
.attr('y1','950')
.attr('x2', '900')
.attr('y2', '950')
.style("stroke-width", '1')
.style("stroke", "rgb(0,0,0)")

svg
.append("text")
.attr('x','120')
.attr('y','970')
.attr('text-anchor','middle')
.text("Boston Machine Co.")
.style('font-family','georgia')
.style('font-size','14px')

svg
.append("text")
.attr('x','240')
.attr('y','970')
.attr('text-anchor','middle')
.text("Charles Carr")
.style('font-family','georgia')
.style('font-size','14px')

svg
.append("text")
.attr('x','360')
.attr('y','970')
.attr('text-anchor','middle')
.text("Holyoke")
.style('font-family','georgia')
.style('font-size','14px')

svg
.append("text")
.attr('x','480')
.attr('y','970')
.attr('text-anchor','middle')
.text("R.D. Wood")
.style('font-family','georgia')
.style('font-size','14px')

svg
.append("text")
.attr('x','600')
.attr('y','970')
.attr('text-anchor','middle')
.text("Corey-Rensselaer")
.style('font-family','georgia')
.style('font-size','14px')

svg
.append("text")
.attr('x','720')
.attr('y','970')
.attr('text-anchor','middle')
.text("Darling")
.style('font-family','georgia')
.style('font-size','14px')

svg
.append("text")
.attr('x','840')
.attr('y','970')
.attr('text-anchor','middle')
.text("Kennedy")
.style('font-family','georgia')
.style('font-size','14px')
return svg.node();
}
Insert cell
genHydrants = FileAttachment("fh2.geojson").json()
Insert cell
bldgs = FileAttachment("buildings.geojson").json()
Insert cell
bbox = FileAttachment("site_boundary@1.geojson").json()
Insert cell
Roads = FileAttachment("road2.geojson").json()
Insert cell
Insert cell
hydrants = d3.csv(hydrant_link,d3.autoType)
Insert cell
import {wrap_text, wrap_text_nchar} from "@ben-tanen/svg-text-and-tspan-word-wrapping"
Insert cell
test1 = FileAttachment("Border.txt").tsv({array:true})
Insert cell
Border = FileAttachment("BlackBorder.txt").tsv({array:true})
Insert cell
hLine = FileAttachment("HydrantLine.txt").tsv({array:true})
Insert cell
hMask = FileAttachment("HydrantMask.txt").tsv({array:true})
Insert cell
hide = FileAttachment("Sometimes.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