Public
Edited
Jan 25, 2023
Fork of base map
Insert cell
# BCN Base Map
Insert cell
d3 = require("d3@6")
Insert cell
viewof viriat = Inputs.range([0, 8], {step: 1, label: "Viriat", value: 0})
Insert cell
viewof buscate = Inputs.range([0, 8], {step: 1, label: "Buscaté Bubble Tea", value: 0})
Insert cell
viewof boa = Inputs.range([0, 8], {step: 1, label: "Boa-Bao", value: 0})
Insert cell
viewof sinluz = Inputs.range([0, 8], {step: 1, label: "Sinluz. Enginyeria i Arquitectura", value: 0})
Insert cell
viewof IES = Inputs.range([0, 8], {step: 1, label: "IES Abroad Barcelona", value: 0})
Insert cell
viewof Apple = Inputs.range([0, 8], {step: 1, label: "Apple Passeig de Gràcia", value: 0})
Insert cell
viewof kfc = Inputs.range([0, 8], {step: 1, label: "KFC", value: 0})
Insert cell
viewof Aloha = Inputs.range([0, 8], {step: 1, label: "Aloha Poke", value: 0})
Insert cell
totalValues = {
var totalCost = 0
var totalHappiness = 0
totalCost = totalCost +(values[0].cost*viriat)
totalCost = totalCost +(values[1].cost*buscate)
totalCost = totalCost +(values[2].cost*boa)
totalCost = totalCost +(values[3].cost*sinluz)
totalCost = totalCost +(values[4].cost*IES)
totalCost = totalCost +(values[5].cost*Apple)
totalCost = totalCost +(values[6].cost*kfc)
totalCost = totalCost +(values[7].cost*Aloha)

totalHappiness = totalHappiness +(values[0].happiness*viriat)
totalHappiness = totalHappiness +(values[1].happiness*buscate)
totalHappiness = totalHappiness +(values[2].happiness*boa)
totalHappiness = totalHappiness +(values[3].happiness*sinluz)
totalHappiness = totalHappiness +(values[4].happiness*IES)
totalHappiness = totalHappiness +(values[5].happiness*Apple)
totalHappiness = totalHappiness +(values[6].happiness*kfc)
totalHappiness = totalHappiness +(values[7].happiness*Aloha)


return[totalCost, totalHappiness]
}
Insert cell
chart = {
const width = 960,
height = 550;
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], planBoundary);

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(subLns.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", '.75')
.style("stroke", "rgb(0,0,0)")
*/
g.selectAll("path3") //d3 geopath
.data(allLinesSimplified.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") //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.long,d.lat])[0]})
.attr("cy", function(d) {return projection([d.long,d.lat])[1]})
.attr('r',3)
.attr('fill',colorType)
.style('fill-opacity','1')
.style('stroke','black')
.style('stroke-width','1')
.on('mouseover',spottext)
.on('mouseout',removetext)


function colorType(d,i) {
var color = 'black'

if (d.type=='Food'){color='cyan'}
if (d.type=='Work'){color='red'}
if (d.type=='Living'){color='green'}
if (d.type=='Shopping'){color='blue'}
return color
}



function spottext(i,d) {

svg.append("text")
.attr('class','spotInfo')
.attr('x','100')
.attr('y','100')
.attr('font-family','Helvetica')
.attr('font-size','1em')
.attr('font-weight','Bold')
.attr('text-anchor','start')
.text(d.name)

svg.append("text")
.attr('class','spotInfo')
.attr('x','100')
.attr('y','115')
.attr('font-family','Helvetica')
.attr('font-size','.8em')
.attr('font-weight','normal')
.attr('text-anchor','start')
.text(d.desc)

svg.append('line')
.attr('class','spotLine')
.attr('x1','115')
.attr('y1','120')
.attr('x2',projection([d.long,d.lat])[0])
.attr('y2',projection([d.long,d.lat])[1])
.attr('stroke-width','.5')
.attr('stroke','black')

}

function removetext(i,d) {

d3.selectAll('text.spotInfo').remove()
d3.selectAll('line.spotLine').remove()

}




svg.append("text")
// .attr('class','spotInfo')
.attr('x','130')
.attr('y','460')
.attr('font-family','Helvetica')
.attr('font-size','.6em')
.attr('font-weight','200')
.attr('text-anchor','end')
.text("Total Cost: ")

svg.append("text")
// .attr('class','spotInfo')
.attr('x','130')
.attr('y','480')
.attr('font-family','Helvetica')
.attr('font-size','.6em')
.attr('font-weight','200')
.attr('text-anchor','end')
.text("Total Happiness: ")
svg.append("text")
// .attr('class','spotInfo')
.attr('x','130')
.attr('y','460')
.attr('font-family','Helvetica')
.attr('font-size','.6em')
.attr('font-weight','Bold')
.attr('text-anchor','start')
.text(totalValues[0])

svg.append("text")
// .attr('class','spotInfo')
.attr('x','130')
.attr('y','480')
.attr('font-family','Helvetica')
.attr('font-size','.6em')
.attr('font-weight','Bold')
.attr('text-anchor','start')
.text(totalValues[1])



if (totalValues[0] > 100){
svg.append("text")
// .attr('class','spotInfo')
.attr('x','480')
.attr('y','275')
.attr('font-family','Helvetica')
.attr('font-size','10em')
.attr('font-weight','Bold')
.attr('text-anchor','middle')
.attr('fill','red')
.text("BROKE :/")
}

if (totalValues[0] < 100){
if (totalValues[1] > 30){
svg.append("text")
// .attr('class','spotInfo')
.attr('x','480')
.attr('y','275')
.attr('font-family','Helvetica')
.attr('font-size','6em')
.attr('font-weight','Bold')
.attr('text-anchor','middle')
.attr('fill','green')
.text("Un-Depressed!")
}
}



return svg.node();
}



Insert cell
buildingsAndLines = FileAttachment("Buildings and Lines.geojson").json()
Insert cell
planBoundary = FileAttachment("Plan Boundary@1.geojson").json()
Insert cell
allLinesSimplified = FileAttachment("All Lines Simplified.geojson").json()
Insert cell
Insert cell
spots = d3.csv(spotslink, d3.autoType)
Insert cell
values = [
{name:"V", cost: 10, happiness: 3},
{name:"BT", cost: 6, happiness: 4},
{name:"BB", cost: 10, happiness: 2},
{name:"S", cost: -10, happiness: -6},
{name:"IES", cost: -1, happiness: -1},
{name:"APG", cost: 10, happiness: 1},
{name:"KFC", cost: 4, happiness: 1},
{name:"AP", cost: 9, happiness: 2},
]
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more