chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const dataWithLine = data.map(item => ({
...item,
line: 38.5
}));
const bar = svg.append("g").selectAll("rect").data(dataWithLine);
bar
.join("rect")
.attr("x", (d, i) => x(i))
.attr("y", d => y(d.points))
.attr("height", d => y(0) - y(d.points))
.attr("width", x.bandwidth())
.attr("fill", (d) => d.points > d.line ? "#10B981" : "#EF4444")
;
bar
.join("text")
.text((d) => `${d.homeOrAway === 'home' ? '' : '@'}${d.oppTeam}`)
.attr("x", function (d, i) {
return x(i) + x.bandwidth() / 2;
})
.attr("y", (d) => height - margin.bottom - 15)
.attr("fill", "white")
.attr("font-size", "18px")
.style("text-anchor", "middle");
bar
.join("text")
.text((d) => d.points)
.attr("x", function (d, i) {
return x(i) + x.bandwidth() / 2;
})
.attr("y", (d) => y(d.points) + 25)
.attr("fill", "white")
.attr("font-size", "18px")
.style("text-anchor", "middle");
svg
.append("path")
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-miterlimit", 1)
.attr("stroke-width", 1)
.attr("class", "line-path")
.attr("d", line(dataWithLine));
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
return svg.node();
}