viewof lineChart = {
if (!selectedState) return html`<div>Click a state to view data</div>`;
const margin = { top: 30, right: 50, bottom: 60, left: 90 };
const width = 600 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
const stateData = co2.find(d => d["Row Labels"] === selectedState);
if (!stateData) return html`<div>No data for this state</div>`;
const data = years.map(year => ({
year: +year,
value: +stateData[year] || 0
}));
const div = html`<div></div>`;
const svg = d3.select(div)
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const x = d3.scaleLinear()
.domain(d3.extent(data, d => d.year))
.range([0, width]);
svg.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x).tickFormat(d3.format("d")));
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([height, 0]);
svg.append("g").call(d3.axisLeft(y));
svg.append("text")
.attr("x", width / 2)
.attr("y", height + 40)
.attr("text-anchor", "middle")
.attr("font-size", "14px")
.text("Years");
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("y", -60)
.attr("text-anchor", "middle")
.attr("font-size", "14px")
.text("CO₂ Emissions per Million Forest Acres (ppm)");
const line = d3.line()
.x(d => x(d.year))
.y(d => y(d.value));
svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "#ff5722")
.attr("stroke-width", 2)
.attr("d", line);
svg.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => x(d.year))
.attr("cy", d => y(d.value))
.attr("r", 4)
.attr("fill", "#ff5722");
svg.append("text")
.attr("x", width / 2)
.attr("y", -10)
.attr("text-anchor", "middle")
.attr("font-size", "16px")
.attr("font-weight", "bold")
.text(`${selectedState} CO₂ Emissions per Million Forest Acres (2013-2022)`);
return div;
}