superstore_viz = {
const barMargin = { top: 15, right: 15, bottom: 40, left: 80 };
const barWidth = 800;
const barHeight = 600;
const barX = d3
.scaleBand()
.domain(superstore.map((d) => d.Region))
.range([barMargin.left, barWidth])
.padding(0.1);
const barXAxis = (g) =>
g
.attr("transform", `translate(0, ${barHeight})`)
.call(d3.axisBottom().scale(barX));
const barY = d3
.scaleLinear()
.domain([0, d3.max(superstore, (d) => d.Quantity) + 15])
.range([barHeight, barMargin.top]);
const barYAxis = (g) =>
g
.attr("transform", `translate(${barMargin.left}, 0)`)
.call(d3.axisLeft().scale(barY));
const svg = d3
.create("svg")
.attr("width", barWidth + barMargin.left + barMargin.right)
.attr("height", barHeight + barMargin.top + barMargin.bottom);
svg.append("g").call(barXAxis);
svg
.append("text")
.attr("x", barWidth / 2)
.attr("y", barHeight + barMargin.bottom + barMargin.top - 5)
.attr("text-anchor", "middle")
.attr("font-family", "sans serif")
.text("Region");
svg.append("g").call(barYAxis);
svg
.append("text")
.attr("font-family", "sans serif")
.attr(
"transform",
`translate(${barMargin.left / 2}, ${barHeight / 2})rotate(-90)`
)
.attr("text-anchor", "middle")
.text("Purchase Quantity");
svg
.append("g")
.attr("id", "bars")
.selectAll("rect")
.data(superstore)
.join("rect")
.attr("width", barX.bandwidth())
.attr("x", (d) => barX(d.Region))
.attr("height", 0)
.attr("y", barHeight)
.attr("height", (d) => barHeight - barY(d.Quantity))
.attr("y", (d) => barY(d.Quantity))
.attr("stroke", "black")
.attr("fill", "lightgreen");
return svg.node();
}