chart = {
const chartWidth = width - margin.left - margin.right;
const chartHeight = height - margin.top - margin.bottom;
const svg = d3
.create("svg")
.style("cursor", "default")
.attr("viewBox", [0, 0, chartWidth, chartHeight])
.attr("tabindex", 0)
.style("background", "white");
const g = svg
.selectAll(".waffle")
.data(wafflesData)
.join("g")
.attr("class", "waffle")
.attr("aria-label", (d, i) => `${data[i].category}-${data[i].value}%`);
const numPerRow = Math.floor(
chartWidth / (sizeOfWaffle + paddingBetweenWaffles.x)
);
g.attr("transform", (d, i) => {
const r = Math.floor(i / numPerRow);
const c = i - r * numPerRow;
return `translate(${c * (sizeOfWaffle + paddingBetweenWaffles.x)},${
r * (sizeOfWaffle + paddingBetweenWaffles.y)
})`;
});
const cellSize = scale.bandwidth();
const cells = g
.append("g")
.selectAll("rect")
.data((d) => d)
.join("rect")
.attr("fill", (d) => (d.index === -1 ? "#ddd" : "#eeba0b"));
cells
.attr("x", (d) => scale(d.x))
.attr("y", (d) => scale(d.y))
.attr("width", cellSize)
.attr("height", cellSize);
g.append("text")
.attr(
"transform",
`translate(${(sizeOfWaffle + paddingBetweenWaffles.x) / 2 - 10}, ${
sizeOfWaffle + 25
})`
)
.attr("text-anchor", "middle")
.text((d, i) => `${data[i].category} - ${data[i].value}%`)
.attr("font-size", "11pt")
.attr("font-family", "Manrope")
.style("font-weight", "bold")
.style("fill", "black");
return svg.node();
}