chart = {
const container = html`<div class="tooltip-demo">
<style>
/*
* Be sure to set the tooltip's DOM element's styles!
*/
div.tooltip-demo > div.tooltip {
position: fixed;
display: none;
padding: 12px 6px;
background: #fff;
border: 1px solid #333;
pointer-events: none;
}
</style>
</div>`;
const width = 920;
const height = 560;
const projection = d3.geoAlbersUsa().fitSize([width, height], states);
const path = d3.geoPath().projection(projection);
const svg = d3.create("svg").attr("viewBox", [0, 0, 975, 610]);
svg
.selectAll(".state")
.data(states.features)
.join("path")
.attr("d", path)
.attr("class", "state")
.style("fill", function (d) {
const value = stateBuddhistProportions.get(d.properties.NAME);
if (value) {
return colorScale(value);
} else {
return "#ccc";
}
})
.attr("fill-opacity", 1)
.attr("stroke", "black");
svg
.append("g")
.attr("transform", "translate(520,-5)")
.append(() =>
legend({
color,
title: "Proportion of Buddhists, thousands",
width: 340
})
);
const div = d3.select(container).append("div").classed("tooltip", true);
const tooltip = new Tooltip(div);
function tooltipContents(datum) {
const { x, y } = datum;
return `x: ${x}, y: ${y}`;
}
svg
.selectAll(".state")
.on("mouseover", (event, d) => {
tooltip.display(
d,
`${stateBuddhistProportions.get(d.properties.NAME)} ${
d.properties.NAME
}, ${data.get(d.value)}`
);
d3.select(this).attr("stroke", "gray").raise();
});
return svg.node();
}