chart= {
var borders= topojson.mesh(world, world.objects.countries, (a, b) => a!==b)
var path = d3.geoPath(projection)
var projection = d3.geoEqualEarth().translate( [ width /2, height -200]).scale([135])
var land = topojson.feature(world, world.objects.land)
var graticule = d3.geoGraticule10()
const svg= d3.create("svg")
.attr("viewBox", ([0, 0, width , height + 80]));
const grouping= svg.append("g");
grouping.append("path")
.join("path")
.attr("transform", `translate(${ margin.left }, ${ margin.height - 20})`)
.attr("d", path(land))
.attr("fill", "#E8E8E8")
.attr("stroke", "#000")
.attr("stroke-width", 0.25);
grouping.append("path")
.join("path")
.attr("d", path(borders))
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 0.15);
grouping.append("path")
.join("path")
.attr("d", path(graticule))
.attr("stroke", "#ccc")
.attr("fill", "none")
.attr("stroke-width", 0.75)
var colorScale = d3.scaleLinear()
.range(["#FF0000", "#00FF00"]);
var colorBar = svg.append("g")
.attr("transform", "translate(50, 450)");
var segmentWidth = 40;
var segmentHeight = 20;
var segments = 18;
drawColorBar(colorBar, segmentWidth, segmentHeight);
var years = [2017, 2018, 2018, 2019, 2020];
var select = d3.select("body")
.append("select")
.attr("class", "year-select")
.on("change", function() {
var selectedYear = d3.select(this).property("value");
updateColorScale(selectedYear);
});
select.selectAll("option")
.data(years)
.enter()
.append("option")
.attr("value", function(d) {
return d;
})
.text(function(d) {
return d;
});
var initialYear = years[0];
updateColorScale(initialYear);
var targetValue = [-0.70241702, 0.40272152, -0.74727201, 1.2615852, 1.3584321];
var longitude = [-179.875, -179.375, -179.375, -175.125, -157.375];
var latitude = [-15.625, -16.375, -15.875, -21.125, 21.125];
var scatterPoints = svg.selectAll("circle")
.data(targetValue)
.enter()
.append("circle")
.attr("cx", function(d, i) {
return projection([longitude[i], latitude[i]])[0];
})
.attr("cy", function(d, i) {
return projection([longitude[i], latitude[i]])[1];
})
.attr("r", 5)
.style("fill", function(d) {
return colorScale(d);
});
function updateColorScale(selectedYear) {
var min = d3.min(targetValue);
var max = d3.max(targetValue);
colorScale.domain([min, max]);
drawColorBar(colorBar, segmentWidth, segmentHeight);
scatterPoints.style("fill", function(d) {
var yearIndex = years.indexOf(selectedYear);
return colorScale(targetValue[yearIndex]);
});
}
var colorBar = svg.append("g")
.attr("transform", "translate(50, 450)");
var segmentWidth = 40;
var segmentHeight = 20;
var segments = 18;
drawColorBar(colorBar, segmentWidth, segmentHeight);
var years = [2017, 2018, 2018, 2019, 2020];
var select = d3.select("body")
.append("select")
.attr("class", "year-select")
.on("change", function() {
var selectedYear = d3.select(this).property("value");
updateColorScale(selectedYear);
});
select.selectAll("option")
.data(years)
.enter()
.append("option")
.attr("value", function(d) {
return d;
})
.text(function(d) {
return d;
});
var initialYear = years[0];
updateColorScale(initialYear);
var targetValue = [-0.70241702, 0.40272152, -0.74727201, 1.2615852, 1.3584321];
var longitude = [-179.875, -179.375, -179.375, -175.125, -157.375];
var latitude = [-15.625, -16.375, -15.875, -21.125, 21.125];
var scatterPoints = svg.selectAll("circle")
.data(targetValue)
.enter()
.append("circle")
.attr("cx", function(d, i) {
return projection([longitude[i], latitude[i]])[0];
})
.attr("cy", function(d, i) {
return projection([longitude[i], latitude[i]])[1];
})
.attr("r", 5)
.style("fill", function(d) {
return colorScale(d);
});
function updateColorScale(selectedYear) {
var min = d3.min(targetValue);
var max = d3.max(targetValue);
colorScale.domain([min, max]);
drawColorBar(colorBar, segmentWidth, segmentHeight);
scatterPoints.style("fill", function(d) {
var yearIndex = years.indexOf(selectedYear);
return colorScale(targetValue[yearIndex]);
});
}
function drawColorBar(colorBar, segmentWidth, segmentHeight) {
var segmentColors = colorScale.ticks(segments);
colorBar.selectAll("*").remove();
colorBar.selectAll("rect")
.data(segmentColors)
.enter()
.append("rect")
.attr("x", function(d, i) {
return i * segmentWidth;
})
.attr("y", 0)
.attr("width", segmentWidth)
.attr("height", segmentHeight)
.attr("fill", function(d) {
return d;
});
colorBar.selectAll("text")
.data(segmentColors)
.enter()
.append("text")
.attr("x", function(d, i) {
return i * segmentWidth;
})
.attr("y", segmentHeight + 15)
.text(function(d, i) {
var value = (max - min) / segments * i + min;
return value.toFixed(2);
});
}
});
return svg.node();
}