{
const width = 400;
const height = 400;
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width);
svg
.append("g")
.selectAll("rect")
.data(dataset)
.join("rect")
.attr("x", (d, i) => i * (width / dataset.length))
.attr("y", (d) => height - d[fruit] * 4)
.attr("width", width / dataset.length - 1)
.attr("height", (d) => d[fruit] * 4)
.attr("fill", fruit === "apple" ? "red" : "orange");
svg
.append("g")
.selectAll("text")
.data(dataset)
.join("text")
.text((d) => d[fruit])
.attr("x", (d, i) => (i + 0.5) * (width / dataset.length))
.attr("y", (d) => height - d[fruit] * 4)
.attr("dy", -4)
.attr("text-anchor", "middle")
.attr("fill", fruit === "apple" ? "red" : "orange");
return svg.node();
}