chart1 = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width1 + margin.right, height1]);
const tooltip = d3.select("body")
.append("div")
.style("position", "absolute")
.style("font-family", "'Open Sans', sans-serif")
.style("font-size", "15px")
.style("z-index", "10")
.style("background-color", "#A7CDFA")
.style("color", "#000000")
.style("border", "solid")
.style("border-color", "#000000")
.style("padding", "5px")
.style("border-radius", "2px")
.style("visibility", "hidden");
let originalData = data3;
let filteredData = data3;
const bars = svg.append("g")
.selectAll("g")
.data(d3.stack().keys(columns)(filteredData))
.join("g")
.attr("fill", e => color(e.key))
.selectAll("rect")
.data(e => e, e => e.key)
.join("rect")
.attr("x", (e, i) => x1(e.data.year))
.attr("y", e => y1(e[1]))
.attr("height", e => y1(e[0]) - y1(e[1]))
.attr("width", x1.bandwidth())
.on("mouseover", (event, d) => {
const [xValue, yValue] = [d.data.year, d[1] - d[0]];
const category = d3.select(event.target.parentNode).datum().key;
tooltip.style("visibility", "visible")
.html(`Energy Sector: ${category}<br>Year: ${xValue}<br>Energy Generated: ${yValue} MWh`)
.style("left", `${event.pageX}px`)
.style("top", `${event.pageY}px`);
d3.select(event.target)
.style("opacity", 0.7)
.style("stroke", "black")
.style("stroke-width", "2px");
})
.on("mouseout", (event, d) => {
tooltip.style("visibility", "hidden");
d3.select(event.target)
.style("opacity", 1)
.style("stroke", "none");
});
svg.append("text")
.attr("x", width1 / 2)
.attr("y", height1 - margin.bottom / 100)
.attr("text-anchor", "middle")
.text("Year");
svg.append("text")
.attr("x", -height1 / 2)
.attr("y", margin.left / 3.5)
.attr("text-anchor", "middle")
.attr("transform", "rotate(-90)")
.text("Energy Generated MWh");
const legend = svg.selectAll(".legend")
.data([...columns, "all"])
.enter()
.append("g")
.attr("class", "legend")
.attr("transform", (d, i) => `translate(120, ${i * 20})`);
legend.append("rect")
.attr("x", width1 - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", d => (d === "all" ? "gray" : color(d)));
legend.append("text")
.attr("x", width1 - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(d => (d === "all" ? "All" : d));
svg.append("g")
.attr("transform", `translate(0,${height1 - margin.bottom})`)
.call(xAxis1)
.append("text")
.attr("x", width1 / 2)
.attr("y", margin.bottom / 2)
.attr("fill", "black")
.attr("text-anchor", "middle");
svg.append("g")
.attr("transform", `translate(${margin.left}, 0)`)
.call(yAxis1);
return svg.node();
}