chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width + 200, height + 50]);
svg.append("g")
.attr("fill", "orange")
.selectAll("rect")
.data(filteredData)
.join("rect")
.attr("x", x(0))
.attr("y", (d, i) => y(i))
.attr("width", d => x(d.value) - x(0))
.attr("height", y.bandwidth());
svg.append("g")
.attr("fill", "black")
.attr("text-anchor", "start")
.attr("dx", +15)
.attr("font-family", "sans-serif")
.attr("font-size", 18)
.selectAll("text")
.data(filteredData)
.join("text")
.attr("x", d => x(d.value))
.attr("y", (d, i) => y(i) + y.bandwidth() / 2)
.attr("dy", "0.35em")
.attr("dx", +5)
.text(d => format(d.value))
.attr("font-size", 18)
.call(text => text.filter(d => x(d.value) - x(0) < 20)
.attr("font-size", 18)
.attr("dx", +5)
.attr("fill", "black")
.attr("text-anchor", "start"))
.on("mouseover", function(d) {
d3.select(this).attr("fill", "magenta");
var yPosition =
parseFloat(d3.select(this).attr("y")) +
y.bandwidth() / 2;
var xPosition =
parseFloat(d3.select(this).attr("x")) / 2 + width / 2;
tooltip
.html(
`<div> <strong>Name: </strong>${d.name}</div><div><strong>Proportion:</strong> ${d.name_1}% </div><div><strong>Catgeory:</strong> ${d.name_2}</div>`
)
.style('visibility', 'visible');
})
.on('mousemove', function () {
tooltip
.style('top', d3.event.pageY - 10 + 'px')
.style('left', d3.event.pageX + 10 + 'px');
})
.on('mouseout', function () {
tooltip.html(``).style('visibility', 'hidden');
d3.select(this).transition().attr('fill', "black");
});
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
return svg.node();
}