parallelCoordinates = {
const width = 800, height = 500, margin = {top: 50, right: 150, bottom: 50, left: 60};
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height]);
const dimensions = [1990, 2000, 2010, 2019];
const color = d3.scaleOrdinal(d3.schemeCategory10)
.domain(processedData.map(d => d.country));
const y = {};
for (const dim of dimensions) {
y[dim] = d3.scaleLinear()
.domain([0, d3.max(processedData, d => d[dim])])
.range([height - margin.bottom, margin.top]);
}
const x = d3.scalePoint()
.domain(dimensions)
.range([margin.left, width - margin.right - 70]);
svg.append("g")
.selectAll("g")
.data(dimensions)
.join("g")
.attr("transform", d => `translate(${x(d)})`)
.each(function(d) { d3.select(this).call(d3.axisLeft(y[d])); })
.append("text")
.attr("y", margin.top - 30)
.attr("text-anchor", "middle")
.attr("fill", "black")
.attr("font-size", 12)
.text(d => d);
const lineGen = d3.line()
.x(d => d[0])
.y(d => d[1]);
const lineGroup = svg.append("g");
const lines = lineGroup.selectAll("path")
.data(processedData)
.join("path")
.attr("fill", "none")
.attr("stroke", d => color(d.country))
.attr("stroke-width", 2)
.attr("stroke-opacity", 0.7)
.attr("d", d => lineGen(dimensions.map(p => [x(p), y[p](d[p])])))
.on("mouseover", function(event, d) {
lines.attr("stroke-opacity", 0.1);
d3.select(this)
.attr("stroke-opacity", 1)
.attr("stroke-width", 4);
})
.on("mouseout", function(event, d) {
lines.attr("stroke-opacity", 0.7)
.attr("stroke-width", 2);
});
const legend = svg.append("g")
.attr("transform", `translate(${width - margin.right + 20},${margin.top})`);
processedData.forEach((country, i) => {
const legendRow = legend.append("g")
.attr("transform", `translate(0, ${i * 20})`);
legendRow.append("rect")
.attr("width", 12)
.attr("height", 12)
.attr("fill", color(country.country));
legendRow.append("text")
.attr("x", 18)
.attr("y", 10)
.text(country.country)
.attr("font-size", 12)
.attr("alignment-baseline", "middle");
});
svg.append("text")
.attr("x", (width - margin.left - margin.right) / 2 + margin.left)
.attr("y", 30)
.attr("text-anchor", "middle")
.attr("font-size", 18)
.attr("font-weight", "bold")
.text("CO₂ Emissions Across Years (Parallel Coordinates)");
return svg.node();
}