Published
Edited
Jul 15, 2022
1 star
Insert cell
# fleet's interesting base map_labels
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 960, //size of map on screen
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [50, 50, width-100, height-100]); // adjusting position of the view box

// Use Mercator projection
var projection = d3
.geoMercator()
.fitSize([width - 50, height - 50], bb_test);
//added before other graphic elements are created - so it will be a background
svg
.append('rect')
.attr('fill', "rgb(150,150,150)")
.attr('stroke',"rgb(0,0,0)")
.attr('stroke-width',"3px")
.attr('x', 0)
.attr('y', 0)
.attr('width', 1000)
.attr('height', 1000)

var path1 = d3.geoPath().projection(projection);//path variable for each use of d3.geopath
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");
//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(100,100,100)")

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


g.selectAll("path4") //d3 geopath
.data(class_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", "rgb(220,0,0)")
.style("fill-opacity", ".2")
.style('stroke-opacity','.4')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")


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

svg
.append('rect')
.attr('fill', "rgb(100,0,0)")
.attr('stroke',"rgb(0,0,0)")
.attr('stroke-width',"3px")
.attr('x', 67)
.attr('y', 62)
.attr('width', 220)
.attr('height', 120)
c.enter().append('circle') //d3 geopath
.data(pizza)
.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]})
.attr("cy", function(d) {return projection([d.long,d.lat])[1]})
.attr('r',4)
.attr('fill','rgb(255,211,0)')
.style('fill-opacity','1')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")
.on('mouseover',pizzaMouse)
.on('mouseout',pizzaMouseOut)//use 'mouseleave' for polygons
function pizzaMouse(event,d){
d3.select(this).style("stroke-width", '3.5')

//create a line from pizza shop location, to the review
svg
.append('line')
.attr('x1','67')
.attr('y1',projection([d.long,d.lat])[1])
.attr('x2','67')
.attr('y2','182')
.attr('class','pizzaLine')
.attr('stroke','rgb(0,0,0)')
.attr('stroke-width','2')

svg
.append('line')
.attr('x1','67')
.attr('y1',projection([d.long,d.lat])[1])
.attr('x2',projection([d.long,d.lat])[0])
.attr('y2',projection([d.long,d.lat])[1])
.attr('class','pizzaLine')
.attr('stroke','rgb(0,0,0)')
.attr('stroke-width','2')
svg
.append('text')
.attr('x','75')
.attr('y','130')
.attr('class','review')
.attr("font-family", "Helvetica")
.attr('font-size','.6em')
//.style("font-weight","bold")
//.style("font-style","italic")
.style("fill",'rgb(255,211,0)')
.text(d.description)

var wrap = svg.selectAll('text.review')
.each(function(d, i) { wrap_text(d3.select(this), 180) });
}
function pizzaMouseOut(event,d){
d3.select(this).style("stroke-width", '.5')
d3.selectAll('text.review').remove()
d3.selectAll('line.pizzaLine').remove()
}

var t = svg.selectAll("text") //d3 geopath
.data(class_subs.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]+8})
.attr('font-family','helvetica')
.style('font-size','.3em')
.attr('text-anchor','middle')
.style("font-weight","bold")
.style("fill","rgb(0,0,0)")
.text(function(d) {return d.properties.name})

t.enter().append("text") //everytime after the first use of the variable
.data(pizza)
.enter() //there are more data than elements, this selects them
.append("text") //appends path to data
.attr("x", '600')
.attr("y", function(d, i) {return 100+(i*25);})
.attr('font-family','helvetica')
.style('font-size','.5em')
//.attr('text-anchor','middle')
.style("font-weight","bold")
.style("fill","rgb(0,0,0)")
.text(function(d) {return d.name})



svg
.append('text')
.attr('x','75')
.attr('y','85')
.attr("font-family", "Helvetica")
.attr('font-size','1.25em')
.style("font-weight","bold")
//.style("font-style","italic")
.style("fill",'rgb(255,211,0)')
.text('FiDi Pizza Hot Spots')





return svg.node();
}
Insert cell
class_subs = FileAttachment("class_subs.geojson").json()
Insert cell
class_parks = FileAttachment("class_parks.geojson").json()
Insert cell
subSts = FileAttachment("sub_stops.geojson").json()
Insert cell
bldgs = FileAttachment("bldgs.geojson").json()
Insert cell
bbox = FileAttachment("site_boundary.geojson").json()
Insert cell
subLns = FileAttachment("subway_lines.geojson").json()
Insert cell
boundary = FileAttachment("nyc_boundary.geojson").json()
Insert cell
bb_test = FileAttachment("bounding_box_test.geojson").json()
Insert cell
sub_test = FileAttachment("sub_test.geojson").json()
Insert cell
Insert cell
pizza = d3.csv(pizzaLink, d3.autoType)
Insert cell
import { wrap_text, wrap_text_nchar } from "@ben-tanen/svg-text-and-tspan-word-wrapping"
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