Skip to content

Commit

Permalink
Update graph.js
Browse files Browse the repository at this point in the history
  • Loading branch information
imaitland authored Feb 7, 2024
1 parent 8a1f467 commit db7f123
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions js/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,49 @@ function delay(milliseconds){
// Assume that there is a js object with name 'graph_data' already present...

const Graph = ForceGraph()(document.getElementById("graph"))
// Contain nodes to a box.
.graphData(graph_data)
.warmupTicks(60)
.cooldownTicks(30)
.nodeId("id")
.nodeVal("val")
.nodeLabel("title")
.nodeAutoColorBy("id")
.nodeCanvasObject((node, ctx, globalScale) => {
let label = emojimode ? node.icon : node.title;
let fontSize = emojimode ? 26 : 5;

ctx.font = `${fontSize}px Sans-Serif`;

//const textWidth = ctx.measureText(node_dimensions).width;
const textWidth = ctx.measureText(label).width;
const bckgDimensions = [textWidth, fontSize].map((n) => n + fontSize * 0.2); // some padding

ctx.fillStyle = "rgba(255, 255, 255, 0.0)";

ctx.fillRect(
node.x - bckgDimensions[0] / 2,
node.y - bckgDimensions[1] / 2,
...bckgDimensions
);

ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = node.color;
ctx.fillText(label, node.x, node.y);

node.__bckgDimensions = bckgDimensions; // to re-use in nodePointerAreaPaint
})
.nodePointerAreaPaint((node, color, ctx) => {
ctx.fillStyle = color;
const bckgDimensions = node.__bckgDimensions;
bckgDimensions &&
ctx.fillRect(
node.x - bckgDimensions[0] / 2,
node.y - bckgDimensions[1] / 2,
...bckgDimensions
);
})
.linkSource("source")
.linkTarget("target")
.linkWidth(1.3)
Expand Down Expand Up @@ -86,7 +123,6 @@ const Graph = ForceGraph()(document.getElementById("graph"))
Graph.centerAt(rootNode.x, rootNode.y, 1000);
Graph.zoom(8, 2000);
}
})
});


// Contain nodes to a box.
.graphData(graph_data);

0 comments on commit db7f123

Please sign in to comment.