chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
let selected = -1
svg
.on("mouseover", function(d, i) {
svg
.select(".main")
.selectAll("rect")
.transition()
.duration(100)
.attr("opacity", (d, i) => i !== selected ? "0.5" : "1")
})
.on("mouseout", function(d) {
svg.select(".main").selectAll("rect").transition().duration(200).attr("opacity", "1")
})
svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d, i) => x(i))
.attr("y", d => y(MAX) + margin.top)
.attr("fill", "#f8fbf4")
.attr("height", d => height/2 - margin.top)
.attr("width", x.bandwidth());
svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d, i) => x(i))
.attr("y", d => y(0))
.attr("fill", "#fdf4f5")
.attr("height", d => height/2 - margin.bottom)
.attr("width", x.bandwidth());
svg.append("g")
.attr("class", "main")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d, i) => x(i))
.attr("y", d => {
if (d.value < 0) {
return y(0)
}
return y(d.value) + margin.top
})
.attr("height", d => {
if (d.value < 0) {
return y(d.value) - y(0) - margin.bottom
}
return y(0) - y(d.value) - margin.top
})
.attr("fill", d => d.value > 0 ? "#75b211" : "#e1191d")
.attr("width", x.bandwidth())
.on("mouseover", function(d, i) {
selected = i
})
.on("mouseout", () => selected = -1)
svg.append("g")
.call(xAxis);
return svg.node()
}