{
const g = graph.copy();
const nodeRadius = 4;
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 400;
const ctx = canvas.getContext('2d');
var xExtent = d3.extent(
g.nodes()
.map(nid => {
let n = g.getNodeAttributes(nid)
return n.x
})
);
var yExtent = d3.extent(
g.nodes()
.map(nid => {
let n = g.getNodeAttributes(nid)
return n.y
})
);
let xRange = [0, canvas.width];
let yRange = [0, canvas.height];
const ratioX = (xExtent[1]-xExtent[0]) / canvas.width;
const ratioY = (yExtent[1]-yExtent[0]) / canvas.height;
if (ratioX < ratioY) {
const xMiddle = canvas.width / 2;
const newRange = canvas.height;
xRange = [xMiddle - (newRange/2), xMiddle + (newRange/2)];
} else {
const yMiddle = canvas.height / 2;
const newRange = canvas.width;
yRange = [yMiddle - (newRange/2), yMiddle + (newRange/2)];
}
const margin = nodeRadius + 12;
xRange[0] += margin;
xRange[1] -= margin;
yRange[0] += margin;
yRange[1] -= margin;
yRange.reverse();
var x = d3.scaleLinear()
.domain(xExtent)
.range(xRange);
var y = d3.scaleLinear()
.domain(yExtent)
.range(yRange);
g.edges().forEach(eid => {
let ns = g.getNodeAttributes(g.source(eid));
let nt = g.getNodeAttributes(g.target(eid));
let e = g.getEdgeAttributes(eid);
ctx.strokeStyle = e.color;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(x(ns.x), y(ns.y));
ctx.lineTo(x(nt.x), y(nt.y));
ctx.stroke();
})
g.nodes().forEach(nid => {
let n = g.getNodeAttributes(nid);
ctx.fillStyle = n.color;
ctx.beginPath();
ctx.arc(x(n.x), y(n.y), nodeRadius, 0, 2 * Math.PI, false);
ctx.fill();
});
let nodeKinds = {};
g.nodes().forEach(nid => {
let n = g.getNodeAttributes(nid);
nodeKinds[n.kind] = n.color;
});
ctx.textAlign = "left";
ctx.font = "24px sans-serif";
ctx.fillStyle = "#303040";
ctx.fillText(`Types de noeuds: ${
Object.keys(nodeKinds)
.map(kind => `${kind} (${ nodeKinds[kind] })`)
.join(", ")
}.`, 10, 30);
return canvas;
}