{
const width = 400;
const height = 200;
const container = d3.create('svg')
.attr('width', width)
.attr('height', height);
const xscale = d3.scaleBand()
.domain(barData.map(d => d.key))
.range([0, width]);
const yscale = d3.scaleLinear()
.domain([0, d3.max(barData, d => d.value)])
.range([height, 0]);
container.selectAll('rect')
.data(barData)
.join('rect')
.attr('x', d => xscale(d.key))
.attr('y', d => yscale(d.value))
.attr('width', xscale.bandwidth())
.attr('height', d => height - yscale(d.value))
.style('fill', d => color(d.key))
.style('stroke', 'white');
container.selectAll('text')
.data(barData)
.join('text')
.attr('x', d => xscale(d.key) + xscale.bandwidth() / 2)
.attr('y', d => yscale(d.value))
.attr('dy', 15)
.attr('fill', 'white')
.style('font-size', 'small')
.style('text-anchor', 'middle')
.text(d => d.value);
return container.node();
}
// {
// const width = 400;
// const height = 200;
// const container = d3.create('svg')
// .attr('width', width)
// .attr('height', height);
// // Update the x scale here.
// const xscale = d3.scaleLinear()
// .domain([0, d3.max(barData, d => d.value)])
// .range([0, width]);
// // Update the y scale here.
// const yscale = d3.scaleBand()
// .domain(barData.map(d => d.key))
// .range([0, height]);
// container.selectAll('rect')
// .data(barData)
// .join('rect')
// .attr('x', 0) // Update the x position
// .attr('y', d => yscale(d.key)) // Update the y position
// .attr('width', d => xscale(d.value)) // Update the width
// .attr('height', yscale.bandwidth()) // Update the height
// .style('fill', d => color(d.key))
// .style('stroke', 'white');
// container.selectAll('text')
// .data(barData)
// .join('text')
// .attr('x', d => xscale(d.value)) // Update the x position
// .attr('y', d => yscale(d.key)) // Update the y position
// .attr('dx', -20) // Update the x offset
// .attr('dy', '1.5em') // Update the y offset
// .attr('fill', 'white')
// .style('font-size', 'small')
// .style('text-anchor', 'middle')
// .text(d => d.value);
// return container.node();
// }