createCanvasChart = {
const width = 1000 * window.devicePixelRatio;
const radius = width / 2;
const halo = "#fff";
const haloWidth = 3;
const colorin = "#00f";
const colorout = "#f00";
const colornone = "#ccc";
const tree = d3.cluster()
.size([2 * Math.PI, radius - 100]);
const root = tree(customBilink(d3.hierarchy(createTree()))
.sort((a, b) => d3.ascending(a.height, b.height) || d3.ascending(a.data.name, b.data.name))
);
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = width;
canvas.style.maxWidth = '100%'
canvas.style.height = 'auto'
const context = canvas.getContext('2d');
const line = d3.lineRadial()
.curve(d3.curveBundle.beta(0.85))
.radius(d => d.y)
.angle(d => d.x)
.context(context);
function drawLinks() {
context.globalAlpha = 0.25;
context.strokeStyle = '#ccc';
context.lineWidth = 1;
root.descendants().flatMap(d => d.outgoing).forEach(([i, o]) =>
drawOneLink(i,o)
)
}
function drawOneLink(startNode, endNode) {
context.save();
context.translate(radius, radius);
context.beginPath();
line(startNode.path(endNode));
context.stroke();
context.restore();
}
// Function to draw nodes
function drawNodes() {
context.font = '12px sans-serif';
context.textAlign = 'center';
context.textBaseline = 'middle';
root.descendants()
.filter(d => d.incoming.length > 0 || d.outgoing.length > 0)
.forEach(d => {
context.save();
context.translate(radius, radius);
context.rotate(d.x - Math.PI/2);
context.translate(d.y, 0);
context.rotate(d.x < Math.PI ? 0 : Math.PI); // rotate half the labels that seem upside down
context.textAlign = d.x < Math.PI ? 'start' : 'end' // align edges of labels with circles' circumference
// .attr("x", d => d.x < Math.PI ? 6 : -6)
context.beginPath();
context.fillStyle = 'black';
context.strokeStyle = halo;
context.lineWidth = haloWidth;
context.strokeText(d.data.name, 0, 0);
context.fillText(d.data.name, 0, 0);
context.restore();
});
}
drawLinks();
let defaultLinksDrawing = context.getImageData(0,0,width,width)
function redrawLinks() {
context.putImageData(defaultLinksDrawing, 0, 0)
}
// drawNodes();
const svg = d3.create("svg")
.attr("width", width)
.attr("height", width)
.attr("viewBox", [-width / 2, -width / 2, width, width])
.attr("style", "max-width: 100%; height: auto; font: 12px sans-serif;");
const node = svg.append("g")
.selectAll()
.data(root.descendants()
.filter(d => d.incoming.length > 0 || d.outgoing.length > 0))
.join("g")
.attr("transform", d => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0)`)
.append("text")
.attr("dy", "0.31em")
.attr("x", d => d.x < Math.PI ? 6 : -6)
.attr("text-anchor", d => d.x < Math.PI ? "start" : "end")
.attr("transform", d => d.x >= Math.PI ? "rotate(180)" : null)
.attr("paint-order", "stroke")
.attr("stroke", halo)
.attr("stroke-width", haloWidth)
.text(d => d.data.name)
.each(function(d) { d.text = this; })
.on("mouseover", overed)
.on("mouseout", outed)
// .call(text => text.append("title").text(d => `a/b/c/sample/`));
.call(text => text.append("title").text(d => reconstructPath(d))); // tooltip text
function overed(event, d) {
// link.style("mix-blend-mode", null); // maybe 1/2 opacity?
d3.select(this).raise()
d3.select(this).attr("font-weight", "bold");
d3.select(this).style("cursor", "pointer");
context.globalAlpha = 0.5
// context.lineWidth = 2
context.strokeStyle = '#f542a7'
; d.outgoing.forEach(([i,o]) => drawOneLink(i,o))
context.strokeStyle = 'blue';
d.incoming.forEach(([i,o]) => drawOneLink(i,o))
// d3.selectAll(d.incoming.map(([d]) => d.text)).attr("fill", colorin).attr("font-weight", "bold");
//
// d3.selectAll(d.outgoing.map(([, d]) => d.text)).attr("fill", colorout).attr("font-weight", "bold"); // this is a slow operation, to change attrs on hundreds of svg nodes
}
function outed(event, d) {
// link.style("mix-blend-mode", "multiply");
d3.select(this).attr("font-weight", null);
// d3.selectAll(d.incoming.map(([d]) => d.text)).attr("fill", null).attr("font-weight", null);
//
// d3.selectAll(d.outgoing.map(([, d]) => d.text)).attr("fill", null).attr("font-weight", null); // this is a slow operation, to change attrs on hundreds of svg nodes
redrawLinks()
}
// return svg.node()
// return canvas
let container = document.createElement('div')
container.style.position = 'relative'
canvas.style.position = 'absolute'
canvas.style.left = '0'
canvas.style.top = '0'
canvas.style.zIndex = '-1' // make sure canvas is behind svg so svg can receive mouse events
let _svg = svg.node()
_svg.style.left = '0'
_svg.style.top = '0'
container.appendChild(_svg)
container.appendChild(canvas)
return container
}