{
const width = 600;
const height = 400;
const margin = { top: 20, right: 20, bottom: 40, left: 50 };
const data = [
{ pais: "Brasil", valor: 50, hombres: 35, mujeres: 15 },
{ pais: "Uruguay", valor: 40, hombres: 5, mujeres: 35 },
{ pais: "Argentina", valor: 35, hombres: 15, mujeres: 20 },
{ pais: "Chile", valor: 28, hombres: 22, mujeres: 6 },
{ pais: "Paraguay", valor: 22, hombres: 20, mujeres: 2 }
];
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);
const x = d3.scaleBand()
.domain(data.map(d => d.pais))
.range([margin.left, width - margin.right])
.padding(0.1);
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.valor)])
.range([height - margin.bottom, margin.top]);
svg.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x));
svg.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y));
let seleccionada = null;
function render(data, seleccionada) {
const groups = svg.selectAll(".barra-grupo")
.data(data, d => d.pais)
.join("g")
.attr("class", "barra-grupo")
.attr("transform", d => `translate(${x(d.pais)},0)`);
groups.selectAll("*").remove();
groups.each(function(d) {
const g = d3.select(this);
const barWidth = x.bandwidth();
if (d.pais === seleccionada) {
const total = d.valor;
const h = d.hombres;
const m = d.mujeres;
const yH = y(h);
const yM = y(h + m);
const heightH = y(0) - yH;
const heightM = y(0) - y(m);
g.append("rect")
.attr("class", "apilada")
.attr("y", yH)
.attr("width", barWidth)
.attr("height", heightH)
.attr("fill", "#f1a1bc")
.on("click", () => manejarClick(d));
g.append("text")
.attr("x", barWidth / 2)
.attr("y", yH + heightH / 2)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle")
.style("font-size", "12px")
.style("fill", "black")
.style("font-family", '-apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif')
.style("font-weight", "bold")
.text(Math.round((h / total) * 100) + "%");
g.append("rect")
.attr("class", "apilada")
.attr("y", yM)
.attr("width", barWidth)
.attr("height", heightM)
.attr("fill", "#7bbfa7")
.on("click", () => manejarClick(d));
g.append("text")
.attr("x", barWidth / 2)
.attr("y", yM + heightM / 2)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle")
.style("font-size", "12px")
.style("fill", "black")
.style("font-family", '-apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif')
.style("font-weight", "bold")
.text(Math.round((m / total) * 100) + "%");
} else {
g.append("rect")
.attr("class", "barra")
.attr("y", y(d.valor))
.attr("width", barWidth)
.attr("height", y(0) - y(d.valor))
.attr("fill", "steelblue")
.on("mouseover", function() {
if (seleccionada !== d.pais)
d3.select(this).attr("fill", "orange");
})
.on("mouseout", function() {
if (seleccionada !== d.pais)
d3.select(this).attr("fill", "steelblue");
})
.on("click", () => manejarClick(d));
}
});
}
function manejarClick(d) {
if (seleccionada === d.pais) {
seleccionada = null;
} else {
seleccionada = d.pais;
}
render(data, seleccionada);
}
render(data, seleccionada);
return svg.node();
}