{
const width = 800;
const height = 500;
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);
const g = svg.append("g");
const tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("background", "rgba(0,0,0,0.7)")
.style("color", "#fff")
.style("padding", "6px 12px")
.style("border-radius", "4px")
.style("font-size", "12px")
.style("pointer-events", "none")
.style("visibility", "hidden");
g.selectAll("path")
.data(geoDataConBalance)
.join("path")
.attr("d", path)
.attr("fill", d => {
const props = d.properties;
if (props.tipo === 'donador') return colorDonador(props.donado);
if (props.tipo === 'receptor') return colorReceptor(props.recibido);
return '#ccc';
})
.attr("stroke", "#999")
.on("mouseover", (event, d) => {
tooltip
.style("visibility", "visible")
.html(`
<strong>${d.properties.NAME}</strong><br>
Donado: ${d3.format(",")(d.properties.donado)} USD<br>
Recibido: ${d3.format(",")(d.properties.recibido)} USD<br>
Tipo: ${d.properties.tipo}
`);
})
.on("mousemove", event => {
tooltip.style("top", (event.pageY - 10) + "px")
.style("left", (event.pageX + 10) + "px");
})
.on("mouseout", () => tooltip.style("visibility", "hidden"));
svg.call(d3.zoom()
.scaleExtent([1, 8])
.on("zoom", event => {
g.attr("transform", event.transform);
}));
const legendX = 20;
const legendY = height - 40;
const legendWidth = 300;
const legendHeight = 12;
const steps = 100;
const colorLegendScale = d3.scaleDiverging(d3.interpolateRdBu)
.domain([1, 0.5, 0]);
svg.selectAll("rect.legend")
.data(d3.range(steps))
.enter()
.append("rect")
.attr("x", (d, i) => legendX + i * (legendWidth / steps))
.attr("y", legendY)
.attr("width", legendWidth / steps)
.attr("height", legendHeight)
.attr("fill", (d, i) => colorLegendScale(i / (steps - 1)));
svg.append("text")
.attr("x", legendX)
.attr("y", legendY - 5)
.text("Receptor -- Donador")
.attr("font-size", "11px");
svg.append("text")
.attr("x", legendX)
.attr("y", legendY + legendHeight + 12)
.attr("font-size", "10px")
.text("Receptor");
svg.append("text")
.attr("x", legendX + legendWidth / 2)
.attr("y", legendY + legendHeight + 12)
.attr("text-anchor", "middle")
.attr("font-size", "10px")
svg.append("text")
.attr("x", legendX + legendWidth)
.attr("y", legendY + legendHeight + 12)
.attr("text-anchor", "end")
.attr("font-size", "10px")
.text("Donador");
return svg.node();
}