chart = {
const width = 700,
height = 700;
const svg = d3.create("svg")
.attr("viewBox", [50, 50, width - 150, height - 110]);
var projection = d3
.geoMercator()
.fitSize([width - 20, height - 20], boundingbox);
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("path1")
.data(sanfran_county.features)
.enter()
.append("path")
.attr('class','outlines')
.attr("d", path2) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "white")
.style('fill-opacity','.5')
.style('stroke-opacity','1')
.style("stroke-width", '.5')
.style("stroke", "gray")
g.selectAll("path1") //d3 geopath
.data(sanfran_park.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", "green")
.style('fill-opacity','.05')
.style('stroke-opacity','1')
.style("stroke-width", '.05')
.style("stroke", "gray")
g.selectAll("path3") //d3 geopath
.data(sanfran_footprint.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", "gray")
.style("fill-opacity", ".15")
.style('stroke-opacity','.2')
.style("stroke-width", '.25')
.style("stroke", "white")
g.selectAll("path3") //d3 geopath
.data(sanfran_selfootprint.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", "gray")
.style("fill-opacity", ".5")
.style('stroke-opacity','.4')
.style("stroke-width", '.25')
.style("stroke", "white")
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)//add function to control color by 'type'
.style('fill-opacity','1')
.style('stroke','gray')
.style('stroke-width','1')
.on('mouseover', spotText) //listening event watching your mouse and waits for something to happen
.on('mouseout', removeSpotText)
function colorType(d,i){
var color = 'gray'
if(d.type=='food'){color = "rgb(174, 132, 126)"}
if(d.type=='activity'){color = "rgb(159, 194, 179)"}
if(d.type=='sight-see'){color = "rgb(84, 86, 67)"}
return color
}
/*
svg.append('text')
.attr('class', 'meter')
.attr('x', '150')
.attr('y', '150')
.attr('font-family', 'Calibri')
.attr('font-size', '.6em')
.attr('text-anchor', 'end')
.attr('font-weight', '300')
.text('JOY: ' + totalValues[0])
svg.append('text')
.attr('class', 'meter')
.attr('x', '150')
.attr('y', '160')
.attr('font-family', 'Calibri')
.attr('font-size', '.6em')
.attr('text-anchor', 'end')
.attr('font-weight', '300')
.text('FULLNESS: ' + totalValues[1])
*/
//() in the parenthesis are arguments; default event type and data of object
function spotText(event, d){
//'this' refers to whatever caused the thing to happen; function knows which circle fired the function
d3.select(this).attr('fill','pink')
svg.append('text')
.attr('class', 'spots')
.attr('x', '450')
.attr('y', '150')
.attr('font-family', 'Calibri')
.attr('font-size', '.6em')
.attr('text-anchor', 'end')
.attr('font-weight', '500')
.text(d.name)
svg.append('text')
.attr('class', 'spots')
.attr('x', '450')
.attr('y', '160')
.attr('font-family', 'Calibri')
.attr('font-size', '.6em')
.attr('text-anchor', 'end')
.attr('font-weight', '300')
.text(d.description)
svg.append('line')
.attr('class', 'spotsLine')
.attr('x1', '455')
.attr('y1', '147')
.attr('x2', projection([d.longitude, d.latitude])[0])
.attr('y2', 147)
.style('stroke-width', '.5')
.style('stroke', 'black')
.style('stroke-dasharray', '4')
svg.append('line')
.attr('class', 'spotsLine')
.attr('x1', projection([d.longitude, d.latitude])[0])
.attr('y1', '147')
.attr('x2', projection([d.longitude, d.latitude])[0])
.attr('y2', projection([d.longitude, d.latitude])[1])
.style('stroke-width', '.5')
.style('stroke', 'black')
.style('stroke-dasharray', '4')
}
function removeSpotText(event, d){
d3.select(this).attr('fill', colorType)
d3.selectAll('text.spots').remove() //data type and class
d3.selectAll('line.spotsLine').remove()
}
if (totalValues[0] > 150){
svg.append('text')
.attr('class', 'meter')
.attr('x', '325')
.attr('y', '390')
.attr('font-family', 'Calibri')
.attr('font-size', '1.55em')
.attr('text-anchor', 'middle')
.attr('font-weight', 'bold')
.attr('fill' , 'rgb(184, 88, 88)')
.text('YOU HAVE HAD A GOOD TIME, NOW GO HOME')
}
if (totalValues[1] > 70){
svg.append('text')
.attr('class', 'meter')
.attr('x', '325')
.attr('y', '420')
.attr('font-family', 'Calibri')
.attr('font-size', '1.55em')
.attr('text-anchor', 'middle')
.attr('font-weight', 'bold')
.attr('fill' , 'rgb(184, 88, 88)')
.text('YOU ARE TOO FULL, STOP EATING')
}
return svg.node();
}