Public
Edited
Feb 22, 2023
Fork of base map
2 forks
Insert cell
# Wolf Road Spots
Insert cell
d3 = require("d3@6")
Insert cell
viewof spotRadius = Inputs.radio([3,5,7], {label: "Radius",value:5})
Insert cell
hR = [
{name:"ms",cost:72,happiness:8},
{name:"tr",cost:40,happiness:4},
{name:"tbs",cost:25,happiness:3},
{name:"py",cost:33,happiness:6},
{name:"wsb",cost:36,happiness:5},
{name:"hh",cost:28,happiness:4},
{name:"hkb",cost:35,happiness:3}
]
Insert cell
//calculate total cost and total happiness
totalValues = {
var totalCost = 0
var totalHappiness = 0
totalCost = totalCost +(hR[0].cost*ms)
totalCost = totalCost +(hR[1].cost*tr)
totalCost = totalCost +(hR[2].cost*tbs)
totalCost = totalCost +(hR[3].cost*py)
totalCost = totalCost +(hR[4].cost*wsb)
totalCost = totalCost +(hR[5].cost*hh)
totalCost = totalCost +(hR[6].cost*hkb)

totalHappiness = totalHappiness+(hR[0].happiness*ms)
totalHappiness = totalHappiness+(hR[1].happiness*tr)
totalHappiness = totalHappiness+(hR[2].happiness*tbs)
totalHappiness = totalHappiness+(hR[3].happiness*py)
totalHappiness = totalHappiness+(hR[4].happiness*wsb)
totalHappiness = totalHappiness+(hR[5].happiness*hh)
totalHappiness = totalHappiness+(hR[6].happiness*hkb)

return[totalCost,totalHappiness]
}
Insert cell
hR[4].cost
Insert cell
viewof ms = Inputs.radio([0,1,2,3,4,5], {label: "MOSU",value:0})
Insert cell
viewof tr = Inputs.radio([0,1,2,3,4,5], {label: "Texas Roadhouse",value:0})
Insert cell
viewof tbs = Inputs.radio([0,1,2,3,4,5], {label: "The Boil Shack",value:0})
Insert cell
viewof py = Inputs.radio([0,1,2,3,4,5], {label: "Pho Yum",value:0})
Insert cell
viewof wsb = Inputs.radio([0,1,2,3,4,5], {label: "Wasabi",value:0})
Insert cell
viewof hh = Inputs.radio([0,1,2,3,4,5], {label: "Hu's House",value:0})
Insert cell
viewof hkb = Inputs.radio([0,1,2,3,4,5], {label: "Hong Kong Bakery & Bistro",value:0})
Insert cell
chart = {
const width = 960,
height = 900;
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], wbox);

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

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

var c = svg.selectAll("circle") //circle
.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.long,d.lat])[0]})
.attr("cy", function(d) {return projection([d.long,d.lat])[1]})
.attr('r',spotRadius)
.attr('fill',colorType) //add function to control color by 'type'
.style('fill-opacity','1')
.style('stroke','black')
.style('stoke-width','.5')
.on('mouseover',spotText)
.on('mouseout',removeText)


function colorType(d,i){ //non-listening event functions pass the data and then index
var color = 'black'
//if the type is live, make the color yellow
if(d.cuisine == 'Japanese'){color = 'rgb(0,128,128)'}
if(d.cuisine == 'American'){color = 'rgb(4,118,208)'}
if(d.cuisine == 'Vietnamese'){color = 'rgb(220,174,150)'}
if(d.cuisine == 'Chinese'){color = 'rgb(242,133,0)'}
return color
}


//create an if statement that says if the budget is too high, text pops up says it's too high
//create an if statement that says if happiness is above a certain level, and budget is below a certain level, you win!

if (totalValues[0] > 500) {
svg.append("text")
.attr('x','875')
.attr('y','800')
.attr('font-family','Helvetica')
.attr('font-size','1em')
.attr('font-weight','bold')
.attr('text-anchor','end')
.text("STOP DINING OUT AND")
svg.append("text")
.attr('x','875')
.attr('y','820')
.attr('font-family','Helvetica')
.attr('font-size','1em')
.attr('font-weight','bold')
.attr('text-anchor','end')
.text("GO COOK BY YOURSELF")
} else {
svg.append("text")
.attr('x','875')
.attr('y','800')
.attr('font-family','Helvetica')
.attr('font-size','1em')
.attr('font-weight','bold')
.attr('text-anchor','end')
.text("YEP STILL WITHIN THE BUDGET")
}

if (totalValues[1] == 165) {
svg.append("text")
.attr('x','75')
.attr('y','800')
.attr('font-family','Helvetica')
.attr('font-size','1em')
.attr('font-weight','bold')
.attr('text-anchor','start')
.text("GREAT JOB!")
svg.append("text")
.attr('x','75')
.attr('y','820')
.attr('font-family','Helvetica')
.attr('font-size','1em')
.attr('font-weight','bold')
.attr('text-anchor','start')
.text("YOU'RE TRULY A FOODIE NOW")
}


svg.append("text")
//.attr('class','spotInfo')
.attr('x','575')
.attr('y','190')
.attr('font-family','Helvetica')
.attr('font-size','.5em')
.attr('font-weight','regular')
.attr('text-anchor','start')
.text(totalValues[0])
svg.append("text")
//.attr('class','spotInfo')
.attr('x','575')
.attr('y','190')
.attr('font-family','Helvetica')
.attr('font-size','.5em')
.attr('font-weight','light')
.attr('text-anchor','end')
.text('COST=')

svg.append("text")
//.attr('class','spotInfo')
.attr('x','575')
.attr('y','200')
.attr('font-family','Helvetica')
.attr('font-size','.5em')
.attr('font-weight','regular')
.attr('text-anchor','start')
.text(totalValues[1])
svg.append("text")
//.attr('class','spotInfo')
.attr('x','575')
.attr('y','200')
.attr('font-family','Helvetica')
.attr('font-size','.5em')
.attr('font-weight','light')
.attr('text-anchor','end')
.text('HAPPINESS=')

function spotText(i,d) {
//what happen when mouseovering the spot

//name
svg.append("text")
.attr('class','spotInfo')
.attr('x','455')
.attr('y','150')
.attr('font-family','Helvetica')
.attr('font-size','.8em')
.attr('font-weight','bold')
.attr('text-anchor','left')
.text(d.name)

//description
svg.append("text")
.attr('class','spotInfo')
.attr('x','455')
.attr('y','162')
.attr('font-family','Helvetica')
.attr('font-size','.6em')
.attr('font-weight','regular')
.attr('text-anchor','left')
.text(d.description)

//spotlines
svg.append('line')
.attr('class','spotLine')
.attr('x1','457')
.attr('y1','170')
.attr('x2','457')
.attr('y2',projection([d.long,d.lat])[1])
.attr('stroke-width','1')
.attr('stroke','black')
.attr('stroke-dasharray','4 2 1 2')

svg.append('line')
.attr('class','spotLine')
.attr('x1',projection([d.long,d.lat])[0])
.attr('y1',projection([d.long,d.lat])[1])
.attr('x2','457')
.attr('y2',projection([d.long,d.lat])[1])
.attr('stroke-width','1')
.attr('stroke','black')
.attr('stroke-dasharray','4 2 1 2')
}

function removeText(i,d){
d3.selectAll('text.spotInfo').remove() //to remove a class of object
d3.selectAll('line.spotLine').remove()
}

function spotProfile3(event,d){
d3.select(this).attr('fill','lavenderblush')

svg.append('image')
.attr('href',Annie)
.attr('class','spotProfile3')
.attr('x', '100')
.attr('y','103')
.attr('width', 295)
.attr('height', 145)
}

return svg.node();

}
Insert cell
wbox = FileAttachment("WolfRoad_bb.geojson").json()
Insert cell
streets = FileAttachment("WolfRoad_streets.geojson").json()
Insert cell
wbldgs = FileAttachment("WolfRoad_bldgs.geojson").json()
Insert cell
subSts = FileAttachment("sub_stops.geojson").json()
Insert cell
Insert cell
spots = d3.csv(spotsLink,d3.autoType)
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
Insert cell
Annie = FileAttachment("main logo.png").url()
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