Public
Edited
Feb 7, 2023
Insert cell
# Shanghaiing Portland Map -Mariam version
Insert cell
bbox = FileAttachment("bbox@1.geojson").json()
Insert cell
parks = FileAttachment("parks@2.geojson").json()
Insert cell
streets = FileAttachment("streets@1.geojson").json()
Insert cell
portlandBoundary = FileAttachment("Portland Boundary.geojson").json()
Insert cell
sideWater = FileAttachment("side water.geojson").json()
Insert cell
water = FileAttachment("water@1.geojson").json()
Insert cell
d3 = require("d3@6")
Insert cell


chart = {
const width = 1000,
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [50, 50, width-100, height-80]);

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

var path1 = d3.geoPath().projection(projection);
var path2 = d3.geoPath().projection(projection);
var path3 = d3.geoPath().projection(projection);

var g = svg.append("g").attr("id", "paths");

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

g.selectAll("path3") //d3 geopath
.data(portlandBoundary.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", "floralwhite")
.style("fill-opacity", '.2')
.style('stroke-opacity','.6')
.style("stroke-width", '1')
.style("stroke", "rgb(119,94,148)")

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", "darkslateblue")
.style("fill-opacity", ".075")
.style('stroke-opacity','1')
.style("stroke-width", '.6')
.style("stroke", "lightslategrey")

g.selectAll("path3") //d3 geopath
.data(water.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", "slategrey")
.style("fill-opacity", ".1")
.style('stroke-opacity','.5')
.style("stroke-width", '1')
.style("stroke", "lightslategrey")

g.selectAll("path3") //d3 geopath
.data(sideWater.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", "slategrey")
.style("fill-opacity", ".05")
.style('stroke-opacity','.1')
.style("stroke-width", '.1')
.style("stroke", "black")



var c = svg.selectAll("circle") //d3 geopath
.data(locs)
.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',colorType)
.style('fill-opacity','2')
.style('stroke','rgb(119,94,148)')
.style('stroke-width','1')
.on('mouseover', spotText)
.on('mouseout', removespotText)

function colorType(d,i){
var color = 'lemonchiffon'
if(d.type=='morgue'){color = 'lemonchiffon'}
if(d.type=='house'){color = 'lemonchiffon'}
if(d.type=='house'){color = 'lemonchiffon'}
if(d.type=='house'){color = 'lemonchiffon'}
if(d.type=='saloon'){color = 'lemonchiffon'}
if(d.type=='scow'){color = 'lemonchiffon'}
return color

}
function spotText(event,d){
d3.select(this).attr('fill','lavenderblush')
.attr('r', 6)
.style('stroke', 'mediumslateblue')
.style("stroke-opacity",'.5')
.style("stroke-width", '5')
svg.append('text')
.attr('class','spots')
.attr('x', '550')
.attr('y','730')
.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', '550')
.attr('y','750')
.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', '550')
.attr('y1', '738')
.attr('x2', projection([d.longitude, d.latitude])[0])
.attr('y2', '738')
.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', '738')
.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.select(this).attr('r', 4)
d3.select(this).style('stroke-opacity','.65')
d3.select(this).style('stroke-width', '1')
d3.selectAll('text.spots').remove()
d3.selectAll('line.spotLine').remove()
}


svg.append('image')
.attr('href',skullTest3)
.attr('class','spotImage')
.attr('x', '485')
.attr('y','720')
.attr('width', 350)
.attr('height', 150)



return svg.node();
}


Insert cell
skullTest3 = FileAttachment("skull test.png").url()
Insert cell
import {skullTest} from "@mariamth/shanghai"
Insert cell
Insert cell
locs = d3.csv(locslink, 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