Public
Edited
Jan 19, 2023
Insert cell
# CASE Field Trip old
Insert cell
boundingBox = FileAttachment("Bounding Box.geojson").json()
Insert cell
boroughBoundaries = FileAttachment("Borough Boundaries.geojson").json()
Insert cell
ferryLine = FileAttachment("Ferry Line.geojson").json()
Insert cell
parkWaters = FileAttachment("Park Waters.geojson").json()
Insert cell
parks2 = FileAttachment("Parks 2.geojson").json()
Insert cell
parks = FileAttachment("Parks.geojson").json()
Insert cell
streets = FileAttachment("Streets.geojson").json()
Insert cell
waterfrontParks = FileAttachment("Waterfront Parks.geojson").json()
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 1000,
height = 750;
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], 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 path5 = d3.geoPath().projection(projection);
var path6 = d3.geoPath().projection(projection);
var path7 = d3.geoPath().projection(projection);

var g = svg.append("g").attr("id", "paths");
//i'm not using the boundary lines, so I'm going to comment them out
/*
g.selectAll("path1") //d3 geopath
.data(boundary.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", path1) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "none")
.style('stroke-opacity','1')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")
*/


g.selectAll("path2") //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','.8')
.style("stroke-width", '.3')
.style("stroke", "black")

g.selectAll("path2") //d3 geopath
.data(ferryLine.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", '2')
.style("stroke", "blue")
.style('stroke-dasharray','2 2')
g.selectAll("path4") //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", path4) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "lightsteelblue")
.style("fill-opacity", '.35')
.style('stroke-opacity','.5')
.style("stroke-width", '.75')
.style("stroke", "rgb(0,0,0)")



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


g.selectAll("path6") //d3 geopath
.data(parks2.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", path6) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "none")
.style("fill-opacity", '.5')
.style('stroke-opacity','.25')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")


g.selectAll("path7") //d3 geopath
.data(waterfrontParks.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", path7) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "lightsteelblue")
.style("fill-opacity", '.1')
.style('stroke-opacity','.1')
.style("stroke-width", '2')
.style("stroke", "rgb(0,0,0)")

g.selectAll("path3") //d3 geopath
.data(boroughBoundaries.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", "none")
.style('stroke-opacity','1')
.style("stroke-width", '1')
.style("stroke", "steelblue")

g.selectAll("path3") //d3 geopath
.data(bboxHatch.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", "black")
.style("fill-opacity", ".5")
var c = svg.selectAll("circle") //d3 geopath
.data(spots)
.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',3)
.attr('fill',colorType)
.style('fill-opacity','2')
.style('stroke','steelblue')
.style('stroke-width','.8')
.on('mouseover', spotText)
.on('mouseout', removespotText)

function colorType(d,i){
var color = 'steelblue'
if(d.type=='Food'){color = 'steelblue'}
if(d.type=='Academics'){color = 'pink'}
if(d.type=='House'){color = 'pink'}
if(d.type=='Sports Facility'){color = 'pink'}
if(d.type=='Social Facility'){color = 'pink'}
if(d.type=='Market'){color = 'pink'}
if(d.type=='Field'){color = 'pink'}
return color

}
function spotText(event,d){
d3.select(this).attr('fill','grey')
svg.append('text')
.attr('class','spots')
.attr('x', '770')
.attr('y','540')
.attr('font-family','Helvetica')
.attr('font-size', '.7em')
.attr('text.anchor','start')
.attr('font-weight', 'bold')
.text(d.name)

svg.append('text')
.attr('class','spots')
.attr('x', '770')
.attr('y','550')
.attr('font-family','Helvetica')
.attr('font-size', '.65em')
.attr('text.anchor','start')
.attr('font-weight', 'normal')
.text(d.description)

svg.append('line')
.attr('class','spotLine')
.attr('x1', '770')
.attr('y1', '542')
.attr('x2', projection([d.longitude, d.latitude])[0])
.attr('y2', '542')
.style('stroke-width','.5')
.style('stroke','black')
.style('stroke-dasharray','2 2')
svg.append('line')
.attr('class','spotLine')
.attr('x1', projection([d.longitude, d.latitude])[0])
.attr('y1', '542')
.attr('x2', projection([d.longitude, d.latitude])[0])
.attr('y2', projection([d.longitude, d.latitude])[1])
.style('stroke-width','.7')
.style('stroke','darkblue')
.style('stroke-dasharray','2 2')
}
function removespotText(event,d){
d3.select(this).attr('fill',colorType)
d3.selectAll('text.spots').remove()
d3.selectAll('line.spotLine').remove()
}




return svg.node();
}
Insert cell
bboxHatch = FileAttachment("bbox hatch.geojson").json()
Insert cell
Insert cell
spots = d3.csv(spotsLink,d3.autoType)
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