{
const top2019 = data.filter(row => row["2019"] !== "")
.map(row => ({
country: row["Country Name"],
emissions: parseFloat(row["2019"])
}))
.sort((a, b) => b.emissions - a.emissions)
.slice(0,10);
const top1990 = data.filter(row => row["1990"] !== "")
.map(row => ({
country: row["Country Name"],
emissions: parseFloat(row["1990"])
}))
.sort((a, b) => b.emissions - a.emissions)
.slice(0,10);
const width = 400;
const height = 400;
const marginTop = 20;
const marginRight = 30;
const marginBottom = 50;
const marginLeft = 150;
const svg1990 = d3.create("svg")
.attr("width", width)
.attr("height", height);
const x = d3.scaleLinear()
.domain([0, d3.max(top2019, d => d.emissions)])
.range([marginLeft, width - marginRight]);
const y1990 = d3.scaleBand()
.domain(top1990.map(d => d.country))
.range([marginTop, height - marginBottom])
.padding(0.1);
svg1990.append("g")
.selectAll("rect")
.data(top1990)
.join("rect")
.attr("x", x(0))
.attr("y", d => y1990(d.country))
.attr("width", d => x(d.emissions) - x(0))
.attr("height", y1990.bandwidth())
.attr("fill", "green");
svg1990.append("g")
.attr("transform", `translate(0,${height-marginBottom})`)
.call(d3.axisBottom(x));
svg1990.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y1990));
svg1990.append("text")
.attr("text-anchor", "middle")
.attr("x", (width / 2) + marginBottom)
.attr("y", height - 10)
.text("1990 CO2 Emissions (metric tons per capita)")
.attr("font-size", "13.5px");
svg1990.append("text")
.attr("text-anchor", "middle")
.attr("transform", 'rotate(-90)')
.attr("x", -marginTop - (marginLeft))
.attr("y", 20)
.text("Country")
.attr("font-size", "13.5px")
svg1990.append("text")
.attr("text-anchor", "middle")
.attr("x", (width / 2) + marginBottom)
.attr("y", 15)
.text("Top CO2 Emitters in 1990")
.attr("font-size", "13.5px");
const svg2019 = d3.create("svg")
.attr("width", width)
.attr("height", height);
const y2019 = d3.scaleBand()
.domain(top2019.map(d => d.country))
.range([marginTop, height - marginBottom])
.padding(0.1);
svg2019.append("g")
.selectAll("rect")
.data(top2019)
.join("rect")
.attr("x", x(0))
.attr("y", d => y2019(d.country))
.attr("width", d => x(d.emissions) - x(0))
.attr("height", y2019.bandwidth())
.attr("fill", "darkorange");
svg2019.append("g")
.attr("transform", `translate(0,${height-marginBottom})`)
.call(d3.axisBottom(x));
svg2019.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y2019));
svg2019.append("text")
.attr("text-anchor", "middle")
.attr("x", (width / 2) + marginBottom)
.attr("y", height - 10)
.text("2019 CO2 Emissions (metric tons per capita)")
.attr("font-size", "13.5px");
svg2019.append("text")
.attr("text-anchor", "middle")
.attr("transform", 'rotate(-90)')
.attr("x", -marginTop - (marginLeft))
.attr("y", 20)
.text("Country")
.attr("font-size", "13.5px")
svg2019.append("text")
.attr("text-anchor", "middle")
.attr("x", (width / 2) + marginBottom)
.attr("y", 15)
.text("Top CO2 Emitters in 2019")
.attr("font-size", "13.5px");
const charts = document.createElement("div")
charts.style.display = "flex";
charts.style.gap = "10px";
charts.appendChild(svg1990.node());
charts.appendChild(svg2019.node());
return charts;
}