{
const svg = d3.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const x = d3.scaleLinear()
.domain(d3.extent(dataByYear, d => d.year))
.range([0, width]);
const y = d3.scaleLinear()
.domain([
0,
d3.max(dataByYear, d => Math.max(d.donado, d.recibido))
])
.nice()
.range([height, 0]);
const lineDonado = d3.line()
.x(d => x(d.year))
.y(d => y(d.donado));
const lineRecibido = d3.line()
.x(d => x(d.year))
.y(d => y(d.recibido));
g.append("path")
.datum(dataByYear)
.attr("fill", "none")
.attr("stroke", "#d62728")
.attr("stroke-width", 2)
.attr("d", lineDonado);
g.append("path")
.datum(dataByYear)
.attr("fill", "none")
.attr("stroke", "#1f77b4")
.attr("stroke-width", 2)
.attr("d", lineRecibido);
g.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x).tickFormat(d3.format("d")));
g.append("g")
.call(d3.axisLeft(y));
g.append("text")
.attr("x", width / 2)
.attr("y", -10)
.attr("text-anchor", "middle")
.style("font-weight", "bold")
.text(`Balance de donaciones y recepciones para ${countrySelect}`);
const legend = svg.append("g").attr("transform", `translate(${width - 50},${margin.top})`);
legend.append("rect").attr("x", -20).attr("width", 12).attr("height", 12).attr("fill", "#d62728");
legend.append("text").attr("x", -5).attr("y", 10).text("Donado").attr("font-size", "12px");
legend.append("rect").attr("x", -20).attr("y", 20).attr("width", 12).attr("height", 12).attr("fill", "#1f77b4");
legend.append("text").attr("x", -5).attr("y", 30).text("Recibido").attr("font-size", "12px");
return svg.node()
}