Skip to content

Commit 12f4086

Browse files
committed
fixed scatterplot tooltips
1 parent 83e08dd commit 12f4086

1 file changed

Lines changed: 59 additions & 20 deletions

File tree

js/script.js

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,42 @@ document.addEventListener("DOMContentLoaded", function () {
681681
.nice()
682682
.range([height - margin.bottom, margin.top]);
683683

684+
container
685+
.style("position", "relative")
686+
.style("overflow", "visible");
687+
684688
const tooltip = container.append("div")
689+
.attr("class", "snapshot-tooltip")
685690
.style("position", "absolute")
686691
.style("opacity", 0)
687692
.style("pointer-events", "none")
688-
.style("background", "rgba(33,37,41,0.92)")
693+
.style("background", "rgba(33,37,41,0.94)")
689694
.style("color", "#fff")
690695
.style("padding", "0.8rem 1.1rem")
691696
.style("border-radius", "6px")
692-
.style("font-size", "1.3rem");
697+
.style("font-size", "1.2rem")
698+
.style("line-height", "1.35")
699+
.style("z-index", 9999)
700+
.style("box-shadow", "0 8px 20px rgba(0,0,0,0.18)");
701+
702+
function showTooltip(event, d, labelOverride = null) {
703+
const [mx, my] = d3.pointer(event, container.node());
704+
705+
tooltip
706+
.style("opacity", 1)
707+
.style("left", `${mx + 18}px`)
708+
.style("top", `${my + 14}px`)
709+
.html(`
710+
<strong>${d.country}</strong><br>
711+
${labelOverride || niceGroupLabel(d.group)}<br>
712+
GDP per capita: ${formatValue(d.gdp)}<br>
713+
Internet users: ${formatValue(d.internet)}%
714+
`);
715+
}
716+
717+
function hideTooltip() {
718+
tooltip.style("opacity", 0);
719+
}
693720

694721
svg.append("g")
695722
.attr("transform", `translate(0,${height - margin.bottom})`)
@@ -714,17 +741,24 @@ document.addEventListener("DOMContentLoaded", function () {
714741
.attr("opacity", 0.72)
715742
.attr("stroke", "#fff")
716743
.attr("stroke-width", 1)
744+
.on("mouseenter", function (event, d) {
745+
d3.select(this)
746+
.attr("stroke", "#111")
747+
.attr("stroke-width", 2)
748+
.attr("r", 7.2);
749+
750+
showTooltip(event, d);
751+
})
717752
.on("mousemove", function (event, d) {
718-
tooltip
719-
.style("opacity", 1)
720-
.style("left", `${event.offsetX + 20}px`)
721-
.style("top", `${event.offsetY + 12}px`)
722-
.html(
723-
`<strong>${d.country}</strong><br>${niceGroupLabel(d.group)}<br>GDP per capita: ${formatValue(d.gdp)}<br>Internet users: ${formatValue(d.internet)}%`
724-
);
753+
showTooltip(event, d);
725754
})
726-
.on("mouseout", function () {
727-
tooltip.style("opacity", 0);
755+
.on("mouseleave", function () {
756+
d3.select(this)
757+
.attr("stroke", "#fff")
758+
.attr("stroke-width", 1)
759+
.attr("r", 5.8);
760+
761+
hideTooltip();
728762
});
729763

730764
svg.append("g")
@@ -741,17 +775,22 @@ document.addEventListener("DOMContentLoaded", function () {
741775
.attr("stroke", "#111")
742776
.attr("stroke-width", 2.2)
743777
.raise()
778+
.on("mouseenter", function (event, d) {
779+
d3.select(this)
780+
.attr("r", 12)
781+
.attr("stroke-width", 3);
782+
783+
showTooltip(event, d, "China (Source Country)");
784+
})
744785
.on("mousemove", function (event, d) {
745-
tooltip
746-
.style("opacity", 1)
747-
.style("left", `${event.offsetX + 20}px`)
748-
.style("top", `${event.offsetY + 12}px`)
749-
.html(
750-
`<strong>${d.country}</strong><br>China (Source Country)<br>GDP per capita: ${formatValue(d.gdp)}<br>Internet users: ${formatValue(d.internet)}%`
751-
);
786+
showTooltip(event, d, "China (Source Country)");
752787
})
753-
.on("mouseout", function () {
754-
tooltip.style("opacity", 0);
788+
.on("mouseleave", function () {
789+
d3.select(this)
790+
.attr("r", 10)
791+
.attr("stroke-width", 2.2);
792+
793+
hideTooltip();
755794
});
756795

757796
const legend = svg.append("g")

0 commit comments

Comments
 (0)