barchart = {
const svg = d3.create("svg")
.attr("width", chartWidth)
.attr("height", chartHeight);
svg.selectAll("rect")
.data(gapminder2000)
.join("rect")
.attr("x", x(0))
.attr("y", d => y(d.country))
.attr("height", y.bandwidth())
.attr("width", d => x(d.fertility) - x(0))
.attr("fill", d => selectedData !== null && selectedData !== d ? "lightgrey" : "steelblue")
.on("click", (event, d) => {
if(selectedData === d) {
mutable selectedData = null;
}
else mutable selectedData = d;
});
svg.append("g")
.call(d3.axisLeft(y))
.attr("transform", `translate(${margin.left}, 0)`);
svg.append("g")
.call(d3.axisBottom(x))
.attr("transform", `translate(0, ${chartHeight - margin.bottom})`);
return svg.node();
}