canvasChart = {
const container = document.createElement("div");
container.style.position = "relative";
container.style.width = `${width}px`;
container.style.height = `${height}px`;
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
container.appendChild(canvas);
const tooltip = new Tooltip();
document.body.appendChild(tooltip.node);
const root = pack(
d3
.hierarchy({
children: data.sort((a, b) => b[selected_value] - a[selected_value])
})
.sum((d) => d[selected_value])
);
const ctx = canvas.getContext("2d");
root.leaves().forEach((d) => {
ctx.beginPath();
ctx.fillStyle = colourSchemeZdc(d);
ctx.arc(d.x, d.y, d.r, 0, 2 * Math.PI);
ctx.fill();
ctx.closePath();
});
root.leaves().forEach((d) => {
const textLines = formatBubbleText(d);
const textContainer = document.createElement("div");
textContainer.style.position = "absolute";
textContainer.style.left = `${d.x}px`;
textContainer.style.top = `${d.y}px`;
textContainer.style.transform = "translate(-50%, -50%)";
container.appendChild(textContainer);
if (textLines.length > 0 && Array.isArray(textLines)) {
textLines.forEach((line, i) => {
const textElement = document.createElement("div");
textElement.textContent = line;
textElement.style.font =
i === textLines.length - 1
? "14px var(--trase-mono)"
: "bold 18px var(--trase-sans-serif)";
textElement.style.color = pickTextColorBasedOnBackground(
colourSchemeZdc(d)
);
textElement.style.textAlign = "center";
textElement.style.letterSpacing =
i === textLines.length - 1 ? "0.03em" : "normal";
textContainer.appendChild(textElement);
});
}
});
const tooltipDiv = document.createElement("div");
container.appendChild(tooltipDiv);
container.addEventListener("mousemove", function (event) {
const rect = canvas.getBoundingClientRect();
let xm = event.clientX - rect.left;
let ym = event.clientY - rect.top;
const hoveredNode = root.leaves().find((d) => {
const dx = xm - d.x;
const dy = ym - d.y;
return dx * dx + dy * dy < d.r * d.r;
});
if (hoveredNode) {
let [xPosition, yPosition] = d3.pointer(event, container);
tooltipDiv.style.position = "absolute";
tooltipDiv.style.left = `${xPosition}px`;
tooltipDiv.style.top = `${yPosition}px`;
tooltipDiv.style.transform = "translate(-50%, -50%)";
tooltipDiv.innerHTML = htl.html`
<div>
<p>${hoveredNode.data.key}</p>
</div>
`;
} else {
}
});
return container;
}