chart = {
let text;
if (myWidth < 459) {
text = await FileAttachment("ART-V06_mobile-300x460.svg").text();
} else {
text = await FileAttachment("ART-V06_desktop-460x460.svg").text();
}
const document = new DOMParser().parseFromString(text, "image/svg+xml");
const svg = d3.select(document.documentElement).remove();
svg
.attr("id", "cartogram")
.attr("width", `${Math.min(460, myWidth - 20)}px`)
.style("display", "block")
.style("margin", "0 auto");
const mapGroup = svg.select("g#regions-group");
const regionsGroups = mapGroup.selectAll("g").attr("class", "region-group");
if (detectLocation) {
const myCountryGroup = mapGroup
.select(`g#${myCountry}`)
.raise()
.classed("myCountry", true);
}
mapGroup
.selectAll("circle")
.attr("class", "auxCircles")
.style("opacity", 0)
.call((el) => setTooltipEvent(el));
mapGroup
.selectAll("polygon")
.attr("class", "region-hex")
.call((el) => setTooltipEvent(el));
const context = svg.select("g#context");
context.style("opacity", showContextGrid ? 1 : 0);
const defs = svg.append("defs");
defs
.append("pattern")
.attr("width", 4)
.attr("height", 4)
.attr("patternUnits", "userSpaceOnUse")
.attr("id", "hatching")
.append("path")
.attr("d", "M-1,1 l2,-2 M0,4 l4,-4 M3,5 l2,-2")
.style("stroke", colorNeutral(100))
.style("strokeWidth", 1);
svg
.select("g#countries-group")
.selectAll("circle")
.attr("class", "auxCircles")
.style("opacity", 0);
const myMap = svg.selectAll(".region-group").datum(function (d) {
return {
region_code: d3.select(this).attr("id")
};
});
myMap.data(data, function (d, i) {
return d.region_code;
});
myMap.each(function (d, i) {
const myCircle = d3.select(this).select(".auxCircles");
const xPosit = +myCircle.attr("cx");
const yPosit = +myCircle.attr("cy");
d["x"] = xPosit;
d["y"] = yPosit;
d.ART_infants_per_national_births_percent = undefined;
for (let i = 0; i < data.length; i++) {
if (data[i].country_code === d.region_code) {
d.ART_infants_per_national_births_percent =
data[i].ART_infants_per_national_births_percent;
d.region_name = data[i].country_name;
d.national_births = data[i].national_births;
d.ART_infants = data[i].ART_infants;
break;
}
}
});
const geometryClass = ".region-hex";
regionsGroups
.select(geometryClass)
.style("fill", (d) => colorScale(d.ART_infants_per_national_births_percent))
.style("stroke", colorNeutral(0))
.style("stroke-width", "none");
regionsGroups
.append("text")
.text((d) => d.region_code)
.attr("x", (d) => d.x)
.attr("y", (d) => d.y)
.style("font-size", isMobile ? "0.7rem" : "0.6rem")
.style("text-anchor", "middle")
.style("alignment-baseline", "central")
.style("dominant-baseline", "central")
.style("fill", colorNeutral(800))
.style("pointer-events", "none");
regionsGroups.style("cursor", "auto");
return svg.node();
}