chart = {
let plot = Plot.plot({
y: {
padding: .3
},
marks: [
Plot.barX(longdata, {
x: "percent",
y: "Class",
title: d => `${d.Class} - ${d.category}: ${d.percent} percent`,
text: d => `${d.Class} - ${d.category}: ${d.percent} percent`,
fill: 'category'
}),
Plot.ruleX([0]),
Plot.textX(longdata,
Plot.stackX(
Plot.groupY(
{ x: "sum", text: "first"
},
{
y: "Class",
z: "category",
x: "percent",
text: (d) => `${d.percent} %`,
fill: (d) => d.category == 'Rent'?"white" : "black",
filter: (d) => d.percent > 1,
order: ["Rent", "Food", "Clothes", "Tax", "Other"]
}
)
)
),
],
color: {
domain: ["Rent", "Food", "Clothes", "Tax", "Other"],
range: ["#181815", "#775c7c","#f5a590", "#aea7a1", "#c4b5a6"],
},
z: {
domain: ["Rent", "Food", "Clothes", "Tax", "Other"],
},
y: {
domain: rawdata.map(d => d.Class)
},
height: 500,
marginLeft: 100
});
d3.select(plot)
.selectAll("rect")
.on("pointerenter", function () {
d3.select(plot)
.selectAll("rect")
.attr("opacity", 0.2);
d3.selectAll(`rect[fill='${d3.select(this).attr("fill")}']`)
.attr("opacity", 1)
});
d3.select(plot).on("pointerleave", function () {
d3.select(plot).selectAll("rect").attr("opacity", 1);
d3.select(this)
});
return plot;
}