function BarChart(data, {
chartWidth = 800,
chartHeight = 600,
marginTop = 40,
marginBottom = 10,
marginLeft = 120,
marginRight = 20,
barHeight = 25
} = {}) {
const width = chartWidth - marginLeft - marginRight;
const height = chartHeight - marginTop - marginBottom;
const svg = d3.create("svg")
.attr('width', width + marginLeft + marginRight)
.attr('height', height + marginTop + marginBottom);
const g = svg.append('g').attr('transform', `translate(${marginLeft},${marginTop})`);
const xScale = d3.scaleLinear().range([0, width]).domain([0, d3.max(data, (d) => d.temperature)]);
const yScale = d3.scaleBand().rangeRound([0, height]).paddingInner(0.1).domain(data.map((d) => d.location.city));
const xAxis = d3.axisTop().scale(xScale);
g.append('g').attr('class','x axis').call(xAxis);
const yAxis = d3.axisLeft().scale(yScale);
g.append('g').attr('class','y axis').call(yAxis);
const rect = g.selectAll('rect').data(data, (d) => d.location.city).join(
(enter) => {
const rect_enter = enter.append('rect').attr('x', 0);
rect_enter.append('title');
return rect_enter;
},
(update) => update,
(exit) => exit.remove()
);
rect.transition()
.attr('height', yScale.bandwidth())
.attr('width', (d) => xScale(d.temperature))
.attr('y', (d) => yScale(d.location.city));
rect.select('title').text((d) => d.location.city);
return svg.node();
}