|
| 1 | +import * as d3 from "d3"; |
| 2 | +// import "@fortawesome/fontawesome-free/css/all.min.css"; |
| 3 | + |
| 4 | +export function runForceGraph( |
| 5 | + container, |
| 6 | + linksData, |
| 7 | + nodesData, |
| 8 | + // nodeHoverTooltip, |
| 9 | +) { |
| 10 | + // Specify the dimensions of the chart. |
| 11 | + const width = 500; |
| 12 | + const height = 500; |
| 13 | + |
| 14 | + // Specify the color scale. |
| 15 | + const color = d3.scaleOrdinal(d3.schemeCategory10); |
| 16 | + |
| 17 | + // The force simulation mutates links and nodes, so create a copy |
| 18 | + // so that re-evaluating this cell produces the same result. |
| 19 | + const links = linksData.map((d) => ({ ...d })); |
| 20 | + const nodes = nodesData.map((d) => ({ ...d, fy: 250 + 50 * d.depth })); |
| 21 | + |
| 22 | + // Create a simulation with several forces. |
| 23 | + const simulation = d3 |
| 24 | + .forceSimulation(nodes) |
| 25 | + .force( |
| 26 | + "link", |
| 27 | + d3.forceLink(links).id((d) => d.id), |
| 28 | + ) |
| 29 | + .force("charge", d3.forceManyBody()) |
| 30 | + .force("center", d3.forceCenter(width / 2, height / 2)) |
| 31 | + .on("tick", draw); |
| 32 | + |
| 33 | + // Create the canvas. |
| 34 | + const dpi = devicePixelRatio; // _e.g._, 2 for retina screens |
| 35 | + const canvas = d3 |
| 36 | + .select(container) |
| 37 | + .attr("width", dpi * width) |
| 38 | + .attr("height", dpi * height) |
| 39 | + .attr("style", `width: ${width}px; max-width: 100%; height: auto;`) |
| 40 | + .node(); |
| 41 | + |
| 42 | + const context = canvas.getContext("2d"); |
| 43 | + context.scale(dpi, dpi); |
| 44 | + |
| 45 | + function draw() { |
| 46 | + context.clearRect(0, 0, width, height); |
| 47 | + |
| 48 | + context.save(); |
| 49 | + context.globalAlpha = 0.6; |
| 50 | + context.strokeStyle = "#999"; |
| 51 | + context.beginPath(); |
| 52 | + links.forEach(drawLink); |
| 53 | + context.stroke(); |
| 54 | + context.restore(); |
| 55 | + |
| 56 | + context.save(); |
| 57 | + context.strokeStyle = "#fff"; |
| 58 | + context.globalAlpha = 1; |
| 59 | + nodes.forEach((node) => { |
| 60 | + context.beginPath(); |
| 61 | + drawNode(node); |
| 62 | + context.fillStyle = color(node.depth % 10); |
| 63 | + context.strokeStyle = "#fff"; |
| 64 | + context.fill(); |
| 65 | + context.stroke(); |
| 66 | + }); |
| 67 | + context.restore(); |
| 68 | + } |
| 69 | + |
| 70 | + function drawLink(d) { |
| 71 | + context.moveTo(d.source.x, d.source.y); |
| 72 | + context.lineTo(d.target.x, d.target.y); |
| 73 | + } |
| 74 | + |
| 75 | + function drawNode(d) { |
| 76 | + context.moveTo(d.x + 5, d.y); |
| 77 | + context.arc(d.x, d.y, 5, 0, 2 * Math.PI); |
| 78 | + } |
| 79 | + |
| 80 | + // Add a drag behavior. The _subject_ identifies the closest node to the pointer, |
| 81 | + // conditional on the distance being less than 20 pixels. |
| 82 | + d3.select(canvas).call( |
| 83 | + d3 |
| 84 | + .drag() |
| 85 | + .subject((event) => { |
| 86 | + const [px, py] = d3.pointer(event, canvas); |
| 87 | + return d3.least(nodes, ({ x, y }) => { |
| 88 | + const dist2 = (x - px) ** 2 + (y - py) ** 2; |
| 89 | + if (dist2 < 400) return dist2; |
| 90 | + }); |
| 91 | + }) |
| 92 | + .on("start", dragstarted) |
| 93 | + .on("drag", dragged) |
| 94 | + .on("end", dragended), |
| 95 | + ); |
| 96 | + |
| 97 | + // Reheat the simulation when drag starts, and fix the subject position. |
| 98 | + function dragstarted(event) { |
| 99 | + if (!event.active) simulation.alphaTarget(0.3).restart(); |
| 100 | + event.subject.fx = event.subject.x; |
| 101 | + // event.subject.fy = event.subject.y; |
| 102 | + } |
| 103 | + |
| 104 | + // Update the subject (dragged node) position during drag. |
| 105 | + function dragged(event) { |
| 106 | + event.subject.fx = event.x; |
| 107 | + // event.subject.fy = event.y; |
| 108 | + } |
| 109 | + |
| 110 | + // Restore the target alpha so the simulation cools after dragging ends. |
| 111 | + // Unfix the subject position now that it’s no longer being dragged. |
| 112 | + function dragended(event) { |
| 113 | + if (!event.active) simulation.alphaTarget(0); |
| 114 | + event.subject.fx = null; |
| 115 | + // event.subject.fy = null; |
| 116 | + } |
| 117 | + |
| 118 | + // When this cell is re-run, stop the previous simulation. (This doesn’t |
| 119 | + // really matter since the target alpha is zero and the simulation will |
| 120 | + // stop naturally, but it’s a good practice.) |
| 121 | + // invalidation.then(() => simulation.stop()); |
| 122 | + |
| 123 | + return { |
| 124 | + destroy: () => { |
| 125 | + simulation.stop(); |
| 126 | + }, |
| 127 | + }; |
| 128 | +} |
0 commit comments