Published
Edited
Oct 12, 2021
Fork of simple map
3 forks
1 star
Insert cell
# simple map - interactive data
Insert cell
zips = FileAttachment("zip_test4.geojson").json()
Insert cell
nycParks = FileAttachment("parks_nyc_reduced@2.geojson").json()
Insert cell
subway_lines = FileAttachment("subway_lines@2.geojson").json()
Insert cell
subSts = FileAttachment("subway_sts@1.geojson").json()
Insert cell
racks = FileAttachment("bike_racks.geojson").json()
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 960,
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [-60, 0, width, height]);

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

var path = d3.geoPath().projection(projection);
var path2 = d3.geoPath().projection(projection);



//add line from label to bike rack
svg.selectAll('line')
.data(racks.features)
.enter()
.append('line')
.attr('x1',function(d) {return path.centroid(d)[0]})
.attr('y1',function(d) {return path.centroid(d)[1]})
.attr('x2',52)
.attr('y2',function(d,i){return i*2+10})
.attr('stroke-width','.4')
.attr('stroke-opacity','.08')
.style('stroke','rgb(0,0,0)')
var g = svg.append("g").attr("id", "paths");
g.selectAll("path") //d3 geopath
//svg
//.selectAll('path')
.data(zips.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", path) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "white")
.style('fill-opacity','.8')
.style("stroke-width", ".25")
.style("stroke", "#ccc")



//insert parks into map
g.selectAll("path") //d3 geopath
//svg
//.selectAll('path')
.data(nycParks.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", path) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "red")
.style('fill-opacity','.25')
.style("stroke-width", ".2")
.style("stroke", "rgb(255,0,0)")
.on('mouseover', addFill)
.on('mouseleave', removeFill)


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

var c = svg.selectAll("circle") //d3 geopath
//svg
//.selectAll('path')
.data(subSts.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr('cx',function(d) {return path.centroid(d)[0]})
.attr("cy",function(d) {return path.centroid(d)[1]})
.attr('r',1.5)
.attr('fill','black')
.style('fill-opacity','.9')
.on('mouseover', addBox)
.on('mouseout', removeBox)

c.enter().append('circle')
//svg
//.selectAll('path')
.data(racks.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr('cx',function(d) {return path.centroid(d)[0]})
.attr("cy",function(d) {return path.centroid(d)[1]})
.attr('r',.75)
.attr('fill','rgb(100,100,100)')
.style('fill-opacity','.2')
.on('mouseover', addRack)
.on('mouseout', removeBox)

//add bike rack text
svg.selectAll('text')
.data(racks.features)
.enter()
.append('text')
.attr('x','50')
.attr('y',function(d,i){return i*2+10})
.attr('font-size','.15em')
.attr('text-anchor','end')
.text(function(d){return d.properties.Name})


//add park labels and park fill
function addFill(event,d) {
d3.select(this).style("fill", "blue")
d3.select(this).style('fill-opacity','.8')

svg
.append('text')
.attr("class","labels")
.attr('x','100')
.attr('y','100')
.attr('font-size','.85em')
.text(d.properties.park_name)
}
//take away park labels/park fill
function removeFill(event,d) {
d3.selectAll("text.labels").remove()
d3.select(this).style("fill", "red")
d3.select(this).style('fill-opacity','.25')
}

//add subway stop tool tip
var tooltip = svg.append("g");
function addBox(event, d) {
var centroid = path.centroid(d);
tooltip.call(
callout,
`${d.properties.name}
${d.properties.line}, ${d.properties.line}`
);
tooltip.attr("transform", "translate(" + centroid + ")");
}

//add bike rack box
function addRack(event, d) {
var centroid = path.centroid(d);
tooltip.call(
callout,
`${d.properties.Name}
${d.properties.total_rack}`
);
tooltip.attr("transform", "translate(" + centroid + ")");
}

function removeBox(event, d) {
tooltip.call(callout,null)
}



return svg.node();
}
Insert cell
callout = (g, value) => {
if (!value) return g.style("display", "none");

g
.style("display", null)
.style("pointer-events", "none")
.style("font", "10px sans-serif");

const path = g.selectAll("path")
.data([null])
.join("path")
.attr("fill", "white")
.attr("stroke", "black");

const text = g.selectAll("text")
.data([null])
.join("text")
.call(text => text
.selectAll("tspan")
.data((value + "").split(/\n/))
.join("tspan")
.attr("x", 0)
.attr("y", (d, i) => `${i * 1.1}em`)
.style("font-weight", (_, i) => i ? null : "bold")
.text(d => d));

const {x, y, width: w, height: h} = text.node().getBBox();

text.attr("transform", `translate(${-w / 2},${15 - y})`);
path.attr("d", `M${-w / 2 - 10},5H-5l5,-5l5,5H${w / 2 + 10}v${h + 20}h-${w + 20}z`);
}
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