chart2 = {
const svg = d3.create("svg").attr("viewBox", `0 0 ${width} ${height}`);
const tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("visibility", "hidden");
function handleClick(event, d) {
closeModal();
}
function closeModal() {
const myModal = document.getElementById("myModal");
myModal.style.display = "none";
}
const sizeScale = d3.scaleSqrt().domain([0, d3.max(dataJoined, d => d.pop_size)]).range([5, 50]);
const colorScale = d3.scaleSequential(d3.interpolateBlues).domain([1, d3.max(dataJoined, d => d.mean_homo)]);
svg.selectAll("path")
.data(countries.features)
.enter()
.append("g")
.attr("class", "country-group")
.each(function (d) {
const countryGroup = d3.select(this);
countryGroup.append("path")
.attr("d", path)
.attr("stroke", "#111")
.attr("stroke-width", 0.5)
.attr("fill", "none")
// Tooltip functionality
.on("mouseover", function (event, d) {
let text = d.properties.name
const mean_homo = d.properties.dataJoined.get(year)[0].mean_homo
const mean_homo_rounded = Math.round((mean_homo + Number.EPSILON) * 100) / 100
text = text + "\nJustifiability: " + mean_homo_rounded
const year_to_show = d.properties.dataJoined.get(year)[0].displayed_year
text = text + "\nThis value is from: " + year_to_show
showToolTip(text, [event.pageX, event.pageY])
})
.on("mousemove", function (event) {
d3.select(".tooltip")
.style("top", event.pageY - 10 + "px")
.style("left", event.pageX + 10 + "px")
})
.on("mouseout", function () {
d3.select(".tooltip").style("visibility", "hidden")
})
.on("click", handleClick)
});
svg.selectAll("g")
.data(countries.features)
.each(function (d) {
const countryGroup = d3.select(this);
// Append star symbol if when_legal equals the displayed year and not already added
const whenLegalYear = d.properties.dataJoined?.get(year) ? d.properties.dataJoined.get(year)[0].when_legal : null;
const symbolAdded = whenLegalYear !== null && whenLegalYear <= year;
console.log("Symbol Added:", symbolAdded); // Log the presence of the symbolAdded condition
const [x, y] = path.centroid(d);
const countryArea = path.area(d);
// Append circle for population size and color based on rating
countryGroup.append("circle")
.attr("class", "bubble")
.attr("cx", x)
.attr("cy", y)
.attr("r", sizeScale(d.properties.dataJoined?.get(year)[0].pop_size))
// .attr("fill", colorScale(d.properties.dataJoined.get(year)[0].mean_homo))
.attr("fill", d => d.properties.dataJoined?.get(year) && !isNaN(d.properties.dataJoined?.get(year)[0].mean_homo)
? cScale(d.properties.dataJoined.get(year)[0].mean_homo)
: "lightgrey")
.style("opacity", 0.5);
if (symbolAdded) {
const [x, y] = path.centroid(d);
const countryArea = path.area(d);
const symbolSize = Math.log(countryArea) * 2; // Adjust the scaling factor as needed
console.log("Symbol Size:", symbolSize); // Log the symbol size
countryGroup.append("text")
.attr("class", "symbol")
.attr("x", x)
.attr("y", y)
.text("\u2726") // Unicode for six-pointed star symbol
.attr("text-anchor", "middle")
.attr("alignment-baseline", "middle")
.style("font-size", `${symbolSize}px`)
.style("fill", "orange");
}
});
svg.selectAll("g")
.on("mouseover", function (event, d) {
let text = d.properties.name
const mean_homo = d.properties.dataJoined.get(year)[0].mean_homo
const mean_homo_rounded = Math.round((mean_homo + Number.EPSILON) * 100) / 100
text = text + "\nJustifiability: " + mean_homo_rounded
const year_to_show = d.properties.dataJoined.get(year)[0].displayed_year
text = text + "\nThis value is from: " + year_to_show
showToolTip(text, [event.pageX, event.pageY])
})
.on("mousemove", function (event) {
d3.select(".tooltip")
.style("top", event.pageY - 10 + "px")
.style("left", event.pageX + 10 + "px")
})
.on("mouseout", function () {
d3.select(".tooltip").style("visibility", "hidden")
});
return svg.node();
// function showToolTip(text, [x, y]) {
// tooltip.style("visibility", "visible").html(text);
// }
};