chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, size.width, size.height])
.attr("width", size.width)
.attr("height", size.height)
.attr("style", "max-width: 100%; height: auto;");
const bars = svg.selectAll('.bars')
.data(data)
.enter()
.append('rect')
.attr('class', 'bars')
.attr('x', xScale(0))
.attr('y', (d) => yScale(d.name))
.attr('width', (d) => xScale(d.value) - xScale(0))
.attr('height', yScale.bandwidth())
.style('fill', 'skyblue');
const texts = svg.selectAll('.texts')
.data(data)
.enter()
.append('text')
.attr('class', 'texts')
.attr('x', xScale(0))
.attr('y', (d) => yScale(d.name) + yScale.bandwidth() / 2)
.attr('dx', '.5em')
.attr('dy', 1)
.attr('text-anchor', 'start')
.attr('dominant-baseline', 'middle')
.text((d) => d.value);
const guide = svg.append('text')
.attr('class', 'guide')
.attr('x', size.width - margin.right)
.attr('y', 0)
.attr('dx', '-1em')
.attr('dy', '.5em')
.attr('text-anchor', 'end')
.attr('dominant-baseline', 'text-before-edge');
svg.append('g')
.attr('class', 'axis-y')
.attr('transform', `translate(${xScale(0)}, 0)`)
.call(yAxis);
bars.on('click', () => {
yScale.domain(d3.shuffle(yScale.domain()));
d3.select('.axis-y')
.transition().duration(500)
.call(yAxis);
bars.transition()
.duration(500)
.attr('y', (d) => yScale(d.name));
texts.transition()
.duration(500)
.attr('y', (d) => yScale(d.name) + yScale.bandwidth() / 2);
});
bars.on('mouseover', (event, d) => {
d3.select(event.currentTarget)
.transition().duration(500)
.style('fill', (d) => fillScale(d.value));
guide.style('display', 'inline')
.text(d.name);
});
bars.on('mouseout', (event, d) => {
d3.select(event.currentTarget)
.transition().duration(500)
.style('fill', 'skyblue');
guide.style('display', 'none');
});
return svg.node();
}