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 rect = g.selectAll('rect').data(data).join(
(enter) => {
const rect_enter = enter.append('rect').attr('x', 0);
rect_enter.append('title');
return rect_enter;
},
(update) => update,
(exit) => exit.remove()
);
rect
.attr('height', barHeight)
.attr('width', (d) => d * 7)
.attr('y', (d, i) => i * (barHeight + 5));
rect.select('title').text((d) => `value: ${d}`);
return svg.node();
}