chart = {
const width = 1360,
height = 1100;
const svg = d3.create("svg")
.attr("viewBox", [-300, -30, width-100, height-90]);
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");
g.selectAll("path2")
.data(HKroads.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", "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();
}