function graph2(data) {
const marks = [
Plot.ruleY([0]),
];
variablesY.forEach((variableY, index) => {
const color = coloresY[index % coloresY.length];
marks.push(
Plot.lineY(data, { x: variableX, y: variableY, stroke: color, tip: true })
);
});
const containerWidth = 900;
const containerHeight = 650;
const container = document.createElement("div");
container.style.position = "relative";
container.style.width = containerWidth + "px";
container.style.height = containerHeight + "px";
container.style.fontFamily = "'Source Sans Pro', sans-serif";
const filterAndLegendContainer = document.createElement("div");
filterAndLegendContainer.style.display = "flex";
filterAndLegendContainer.style.alignItems = "center";
filterAndLegendContainer.style.justifyContent = "center";
filterAndLegendContainer.style.marginBottom = "20px";
const selectContainer = document.createElement("div");
selectContainer.style.marginRight = "20px";
selectContainer.appendChild(selectDiv());
filterAndLegendContainer.appendChild(selectContainer);
// Crear la leyenda
const legend = d3.select(document.createElement("div"))
.style("display", "flex")
.style("align-items", "center");
variablesY.forEach((variableY, index) => {
const color = coloresY[index % coloresY.length]; // Obtener el color de la lista de colores
const legendItem = legend.append("div")
.style("display", "flex")
.style("align-items", "center")
.style("margin-right", "10px"); // Espacio entre los elementos de la leyenda
legendItem.append("div")
.style("width", "12px")
.style("height", "12px")
.style("background-color", color)
.style("margin-right", "5px");
legendItem.append("span")
.text(variableY);
});
// Agregar la leyenda al contenedor flexbox
filterAndLegendContainer.appendChild(legend.node());
// Agregar el contenedor flexbox al contenedor principal
container.appendChild(filterAndLegendContainer);
// Agregar el gráfico al contenedor principal
container.appendChild(Plot.plot({
width: 850, // Ancho del gráfico
height: 500, // Alto del gráfico
marginLeft: 50,
x: {
label: 'Año', // Etiqueta del eje X
tickFormat: d3.format(".0f") // Formato personalizado para el eje X
},
y: {
label: 'Nuevo hogares', // Etiqueta del eje Y
grid: true,
tickFormat: d => d3.format(",.0f")(d).replace(/,/g, ".") // Formato para el eje Y
},
marks: marks
}));
// Agregar el logo "Powered by Unholster"
const unholsterLogoLink = document.createElement("a");
unholsterLogoLink.setAttribute("href", "https://unholster.com");
unholsterLogoLink.setAttribute("target", "_blank"); // Abrir enlace en una nueva pestaña
unholsterLogoLink.style.position = "absolute"; // Establecer posición absoluta para el logo
unholsterLogoLink.style.bottom = "40px"; // Posicionar el logo 10px desde el borde inferior
unholsterLogoLink.style.right = "50px"; // Posicionar el logo 10px desde el borde derecho
const unholsterLogo = document.createElement("img");
unholsterLogo.setAttribute("src", "https://d2v5qzywcd0fzd.cloudfront.net/viz-decidechile/30annos/logo-unholster/Powered%20by%20Unholster%202.png");
unholsterLogo.setAttribute("height", "20");
unholsterLogoLink.appendChild(unholsterLogo);
container.appendChild(unholsterLogoLink);
// Agregar el texto en la esquina inferior izquierda
const bottomLeftText = document.createElement("div");
bottomLeftText.innerText = "Fuente: Elaboración propia basado en datos INE-CEPAL"; // Aquí puedes cambiar el texto a lo que necesites
bottomLeftText.style.position = "absolute"; // Establecer posición absoluta para el texto
bottomLeftText.style.bottom = "40px"; // Posicionar el texto 0px desde el borde inferior
bottomLeftText.style.left = "20px"; // Posicionar el texto 50px desde el borde izquierdo
bottomLeftText.style.fontSize = "14px"; // Tamaño de fuente del texto
bottomLeftText.style.color = "#707070"; // Color del texto (puedes cambiarlo según tus necesidades)
bottomLeftText.style.fontFamily = "'Source Sans Pro', sans-serif"; // Aplica la fuente Source Sans Pro
container.appendChild(bottomLeftText); // Agregar el texto al contenedor principal
// Retornar el contenedor que incluye el gráfico y la leyenda
return container;
}