{
let map_width = height
const svg = d3.create("svg")
.attr("height", 0.8*height)
.attr("width", 2*width)
const map = svg.append("g")
.attr("transform", "translate("+0.8*width+" 0) scale(0.8)")
for(let country of countries){
map.append("path")
.attr("fill", "white")
.attr("stroke", "black")
.attr("stroke-width", 0.3)
.attr("d", make_path(country))
.property("name", country.properties.name)
}
const paths = map.selectAll("path")
.property("selected", false)
const graph = svg.append("g")
.attr("transform", "scale(0.8)")
const text = graph.append("text")
.attr("y", margin.top/2)
for(let record of correction_records_2019){
graph.append("circle")
.attr("r", 5)
.attr("cy", correction_y(record.happiness))
.attr("cx", correction_x(record.gdp))
.attr("fill-opacity", 0.3)
.attr("stroke", "black")
.attr("stroke-width", 0.4)
.property("name", record["Country name"])
//.attr("stroke-opacity", 1)
}
const circles = graph.selectAll("circle")
.property("selected", false)
.property("hovered", false)
//.datum( function(){ return( {selected: false , hovered: false} ) } )
function toggle_circle( circle ){
circle.property("selected", ! circle.property("selected") )
circle.attr("stroke-width", circle.property("selected") ? 4 : 0.4)
circle.dispatch("update")
}
function toggle_path( path ){
path.property("selected", ! path.property("selected") )
path.attr("fill", path.property("selected") ? "red" : "white")
}
function highlight(){
const circle = d3.select(this)
circle.property("hovered", true)
circle.attr("fill", "pink")
circle.dispatch("update")
}
function unhighlight(){
const circle = d3.select(this)
circle.property("hovered", false)
circle.attr("fill", "black")
circle.dispatch("update")
}
let selected_countries = new Set()
function update_comment(){
const circle = d3.select(this)
const country = circle.property("name")
if( circle.property("selected") || circle.property("hovered")){
selected_countries.add(country)
} else {
selected_countries.delete(country)
}
let first_5_countries = Array.from(selected_countries.values())
if(first_5_countries.length > 5){
first_5_countries.length = 6
first_5_countries[5] = "..."
}
text.text( first_5_countries.join(", ") )
}
circles.on("click", function() {
const circle = d3.select(this)
const path = paths.filter(function(){
return( circle.property("name") == d3.select(this).property("name"))
})
toggle_path( path )
toggle_circle( circle )
} )
// circles.on("mouseenter", highlight)
// circles.on("mouseleave", unhighlight)
circles.on("update", update_comment)
paths.on("click", function() {
const path = d3.select(this)
const circle = circles.filter(function(){
return( path.property("name") == d3.select(this).property("name"))
})
toggle_path( path )
toggle_circle( circle )
} )
//paths.on("mouseenter", highlight2)
//paths.on("mouseleave", unhighlight2)
const x_axis_y_pos = height-margin.bottom/2
const x_axis = graph.append("g").attr("transform", "translate(0 "+x_axis_y_pos+")")
const make_x_axis = d3.axisBottom(correction_x)
make_x_axis(x_axis)
x_axis.append("text")
.text("Log GDP per capita")
.attr("fill", "grey")
.attr("y", -2)
.attr("x", correction_range_x[1])
.attr("text-anchor", "end")
const y_axis_x_pos = margin.left/2
const y_axis = graph.append("g").attr("transform", "translate("+y_axis_x_pos+" 0)")
const make_y_axis = d3.axisLeft(correction_y)
make_y_axis(y_axis)
return(svg.node())
}