{
const svg = d3.create("svg").attr("width", width).attr("height", height);
svg
.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(xAxis);
svg.append("g").attr("transform", `translate(${margin.left}, 0)`).call(yAxis);
const bubbles = svg
.selectAll("bubbles")
.data(data)
.enter()
.append("circle")
.attr("class", "country_bubbles")
.attr("cx", (d) => xScale(d["Income per person"]))
.attr("cy", (d) => yScale(d["Life expectancy "]))
.attr("r", 0)
.attr("fill", (d) => findColors(d.region))
.style("fill-opacity", 0.74)
.attr("stroke", "#555")
.on("mousemove", function (event, d, index) {
toolTip
.style("display", "block")
.html(
`<p>
<span style="font-weight:bold;">Region: </span>
<span style=" color:${findColors(d.region)}"> ${d.region}</span>
<br>
<span style="font-weight:bold">Country: </span>
${d.name}
<br>
<span style="font-weight:bold">Population: </span>
${d3.format(".2s")(d.Population)}</span>
</p>`
);
let yoffset = 52;
let xoffset = 10;
toolTip
.style("left", event.pageX - d3.select(".toolTip").node().offsetWidth - xoffset + "px")
.style("top", event.pageY - yoffset + "px");
d3.select(this)
.style("stroke-width", 3)
.style("fill-opacity", 0.95);
})
.on("mouseout", function () {
toolTip
.style("display", "none");
d3.select(this)
.style("stroke-width", 1)
.style("fill", (d) => findColors(d.region))
.style("fill-opacity", 0.74);
});
bubbles
.transition()
.duration(1500)
.attr("r", function(d)
{
console.log(d)
return radiusScale(d.Population);
});
const toolTip = d3.select("body").append("div").attr("class", "toolTip");
getLegend(svg, "vertical", 16, 7, 5, domains.length)
return svg.node();
}