Public
Edited
Feb 5, 2023
Fork of base map
1 fork
Insert cell
# Hong Kong SAR Central District
Insert cell
d3 = require("d3@6")
Insert cell
hR = [
{name:"CFP", price:2.5, satisfaction:1},
{name:"HKM", price:3.5, satisfaction:2},
{name:"LKF", price:40, satisfaction:5},
{name:"HKOW", price:20, satisfaction:1},
{name:"LKH", price:100, satisfaction:4},
{name:"KKC", price:50, satisfaction:3},
{name:"FSHHK", price:613, satisfaction:2},
{name:"LMRB", price:5000, satisfaction:1},
{name:"LRL", price:0, satisfaction:5},
{name:"CM", price:140, satisfaction:5},

]
Insert cell
viewof spotsize = Inputs.radio([2, 4, 6, 8, 10], {label: "Spot Point Radius",value:2})
Insert cell
totalValues = {
var totalprice = 0
var totalsatisfaction = 0
totalprice = totalprice +(hR[5].price*CFP)
totalprice = totalprice +(hR[5].price*HKM)
totalprice = totalprice +(hR[5].price*LKF)
totalprice = totalprice +(hR[5].price*HKOW)
totalprice = totalprice +(hR[5].price*LKH)
totalprice = totalprice +(hR[5].price*KKC)
totalprice = totalprice +(hR[5].price*FSHHK)
totalprice = totalprice +(hR[5].price*LMRB)
totalprice = totalprice +(hR[5].price*LRL)
totalprice = totalprice +(hR[5].price*CM)


totalsatisfaction = totalsatisfaction+(hR[0].satisfaction*CFP)
totalsatisfaction = totalsatisfaction+(hR[0].satisfaction*HKM)
totalsatisfaction = totalsatisfaction+(hR[0].satisfaction*LKF)
totalsatisfaction = totalsatisfaction+(hR[0].satisfaction*HKOW)
totalsatisfaction = totalsatisfaction+(hR[0].satisfaction*LKH)
totalsatisfaction = totalsatisfaction+(hR[0].satisfaction*KKC)
totalsatisfaction = totalsatisfaction+(hR[0].satisfaction*FSHHK)
totalsatisfaction = totalsatisfaction+(hR[0].satisfaction*LMRB)
totalsatisfaction = totalsatisfaction+(hR[0].satisfaction*LRL)
totalsatisfaction = totalsatisfaction+(hR[0].satisfaction*CM)
return[totalprice, totalsatisfaction]
}
Insert cell
# Please choose numbers of visitors
Insert cell
viewof CFP = Inputs.radio([1, 2, 3, 4, 5], {label: "Central Ferry Pier",value:0})
Insert cell
viewof HKM = Inputs.radio([1, 2, 3, 4, 5], {label: "Hong Kong Station",value:0})
Insert cell
viewof LKF = Inputs.radio([1, 2, 3, 4, 5], {label: "Lan Kawi Fong",value:0})
Insert cell
viewof HKOW = Inputs.radio([1, 2, 3, 4, 5], {label: "Hong Kong Obervational Wheel",value:0})
Insert cell
viewof LKH = Inputs.radio([1, 2, 3, 4, 5], {label: "Lung King Heen",value:0})
Insert cell
viewof KKC = Inputs.radio([1, 2, 3, 4, 5], {label: "Kam Kee Cafe",value:0})
Insert cell
viewof FSHHK = Inputs.radio([1, 2, 3, 4, 5], {label: "Four Seasons Hotel Hong Kong",value:0})
Insert cell
viewof LMRB = Inputs.radio([1, 2, 3, 4, 5], {label: "Lok Man Rare Books",value:0})
Insert cell
viewof LRL = Inputs.radio([1, 2, 3, 4, 5], {label: "Lugard Road Lookout",value:0})
Insert cell
viewof CM = Inputs.radio([1, 2, 3, 4, 5], {label: "Central Market",value:0})
Insert cell
viewof y = Inputs.range([0, 100000], {step: 1, label: "Favorite number", value:0})
Insert cell
chart = {
const width = 1360,
height = 1100;
const svg = d3.create("svg")
.attr("viewBox", [-300, -30, width-100, height-90]);

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

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(HKroads.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','2')
.style("stroke-width", '.2')
.style("stroke", "rgb(32,32,32)")

g.selectAll("path3") //d3 geopath
.data(HKcoastline.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", "none")
.style("fill-opacity", "none")
.style('stroke-opacity','0.5')
.style("stroke-width", '0.8')
.style("stroke", "rgb(0,51,102)")

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

g.selectAll("path2") //d3 geopath
.data(HKmetro.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", "1")
.style('stroke-opacity','1')
.style("stroke-width", '0.9')
.style("stroke", "rgb(100,100,100)")
.style("fill", "rgb(51,25,0)")
.style('fill-opacity','0.3')

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

g.selectAll("path2") //d3 geopath
.data(HKmountainContours.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", '.2')
.style("stroke", "rgb(0,102,102)")

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.longtitude, d.lattitude])[0]})
.attr("cy", function(d) {return projection([d.longtitude, d.lattitude])[1]})
.attr('r',spotsize)
.attr('fill',colortype)
.style('fill-opacity','1')
.style('stroke','black')
.style('stroke-width','1')
.on('mouseover',spotText)
.on('mouseout',removeText)

function colortype(d,i){//non-listening event functions pass the data, and then index
var color = 'cyan'
//if the type is live, make the color yellow
if(d.type=='Transportation'){color = 'rgb(255,255,102)'}
if(d.type=='Landmark'){color = 'rgb(255,204,153)'}
if(d.type=='Dining'){color = 'rgb(204,153,255)'}
if(d.type=='Shopping'){color = 'rgb(255,153,204)'}
if(d.type=='Live'){color = 'grey'}
if(d.type=='Education'){color = 'rgb(102,178,255)'}

return color
}

if(totalValues[1] > 35){
svg.append("text")
//.attr('class','spotInfo')
.attr('x','60')
.attr('y','450')
.attr('font-family','Calibri')
.attr('font-size','6em')
.attr('font-weight','Regular')
.attr('text-anchor','start')
.text('OVER THE MOON!')
}

if(totalValues[0] > 1800){
svg.append("text")
//.attr('class','spotInfo')
.attr('x','133')
.attr('y','550')
.attr('font-family','Calibri')
.attr('font-size','4em')
.attr('font-weight','Regular')
.attr('text-anchor','start')
.text('BUT BUDGET CRISIS!')
}
svg.append("text")
//.attr('class','spotInfo')
.attr('x','890')
.attr('y','935')
.attr('font-family','Calibri')
.attr('font-size','1.1em')
.attr('font-weight','bold')
.attr('text-anchor','start')
.text(totalValues[0])

svg.append("text")
//.attr('class','spotInfo')
.attr('x','760')
.attr('y','935')
.attr('font-family','Calibri')
.attr('font-size','1.1em')
.attr('font-weight','bold')
.attr('text-anchor','start')
.text('Average Price (HK$):')


svg.append("text")
//.attr('class','spotInfo')
.attr('x','890')
.attr('y','900')
.attr('font-family','Calibri')
.attr('font-size','1.1em')
.attr('font-weight','bold')
.attr('text-anchor','start')
.text(totalValues[1])

svg.append("text")
//.attr('class','spotInfo')
.attr('x','753')
.attr('y','900')
.attr('font-family','Calibri')
.attr('font-size','1.1em')
.attr('font-weight','bold')
.attr('text-anchor','start')
.text('Satisfaction (10 ~ 50):')

function spotText(i,d){
//this is where we write what we want to happen when we mouseover the spot

//d.name is the name of wwhat I'm mousing over

d3.select(this).attr('fill','red')
svg.append("text")
.attr('class','spotInfo')
.attr('x','-140')
.attr('y','403')
.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','-140')
.attr('y','425')
.attr('font-family','Helvetica')
.attr('font-size','0.9em')
.attr('font-weight','300')
.attr('text-anchor','start')
.text(d.type)
svg.append("text")
.attr('class','spotInfo')
.attr('x','-140')
.attr('y','450')
.attr('font-family','Helvetica')
.attr('font-size','1em')
.attr('font-weight','300')
.attr('text-anchor','start')
.text(d.description)

svg.append('line')
.attr('class','spotLine')
.attr('x1','-150')
.attr('y1','410')
.attr('x2','-150')
.attr('y2',projection([d.longtitude, d.lattitude])[1])
.attr('stroke-width','0.7')
.attr('stroke','black')
.attr('stroke-dasharray','4.4')

svg.append('line')
.attr('class','spotLine')
.attr('x1',projection([d.longtitude, d.lattitude])[0])
.attr('y1',projection([d.longtitude, d.lattitude])[1])
.attr('x2','-150')
.attr('y2',projection([d.longtitude, d.lattitude])[1])
.attr('stroke-width','0.7')
.attr('stroke','black')
.attr('stroke-dasharray','4.4')

}


function removeText(i,d){
d3.select(this).attr('fill',colortype)
d3.selectAll('text.spotInfo').remove()//to remove a class of obects, state the object type, then class name
d3.selectAll('line.spotLine').remove()
}
return svg.node();
}
Insert cell
spots = d3.csv(spotslink,d3.autoType)
Insert cell
Insert cell
HKBox = FileAttachment("Bounding Box.geojson").json()
Insert cell
HKmountainContours = FileAttachment("Mountain Contours.geojson").json()
Insert cell
HKbuildings = FileAttachment("Buildings@1.geojson").json()
Insert cell
HKmetro = FileAttachment("Metro.geojson").json()
Insert cell
HKcoastline = FileAttachment("Coastline.geojson").json()
Insert cell
HKroads = FileAttachment("Roads@2.geojson").json()
Insert cell
HKwalkways = FileAttachment("Walkways@1.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

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