function barChart() {
const margin = {top: 10, right: 20, bottom: 50, left: 50};
const width = 300 + margin.left + margin.right;
const height = 200 + margin.top + margin.bottom;
const svg = d3.create('svg')
.attr('width', width)
.attr('height', height + margin.top + margin.bottom);
const x = d3.scaleLinear()
.range([margin.left, width - margin.right]);
const y = d3.scaleBand()
.domain(carColor.domain())
.range([margin.top, height - margin.bottom])
.padding(0.2);
const xAxis = d3.axisBottom(x).tickSizeOuter(0);
const xAxisGroup = svg.append("g")
.attr("transform", `translate(0, ${height - margin.bottom})`);
xAxisGroup.append("text")
.attr('x', margin.left + (width - margin.left - margin.right) / 2)
.attr("y", 40)
.attr("fill", "black")
.attr("text-anchor", "middle")
.text("Count");
const yAxis = d3.axisLeft(y);
const yAxisGroup = svg.append("g")
.attr("transform", `translate(${margin.left})`)
.call(yAxis)
.call(g => g.select(".domain").remove());
let barsGroup = svg.append("g");
function update(data) {
const originCounts = d3.rollup(
data,
group => group.length,
d => d.origin
);
x.domain([0, d3.max(originCounts.values())]).nice()
const t = svg.transition()
.ease(d3.easeLinear)
.duration(200);
xAxisGroup
.transition(t)
.call(xAxis);
barsGroup.selectAll("rect")
.data(originCounts, ([origin, count]) => origin)
.join("rect")
.attr("fill", ([origin, count]) => carColor(origin))
.attr("height", y.bandwidth())
.attr("x", x(0))
.attr("y", ([origin, count]) => y(origin))
.transition(t)
.attr("width", ([origin, count]) => x(count) - x(0))
}
return Object.assign(svg.node(), { update });;
}