Skip to content

Commit 7d2009b

Browse files
committed
fixed x and y caching, used switch statement, making node pointer interaction better
1 parent 20a6b98 commit 7d2009b

File tree

1 file changed

+62
-50
lines changed

1 file changed

+62
-50
lines changed

nextjs/src/components/ForceGraph.js

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ function getCommitXAxis(gData, xMin) {
132132
return m;
133133
}
134134

135+
const getRandX = (node, min, max) => {
136+
if (node.randX && node.minX === min && node.maxX === max) {
137+
return node.randX;
138+
} else {
139+
node.randX = randomIntFromInterval(min, max);
140+
node.maxX = max;
141+
node.minX = min;
142+
return node.randX;
143+
}
144+
}
145+
146+
const getRandY = (node, min, max) => {
147+
if (node.randY && node.minY === min && node.maxY === max) {
148+
return node.randY;
149+
} else {
150+
node.randY = randomIntFromInterval(min, max);
151+
node.minY = min;
152+
node.maxY = max;
153+
return node.randY;
154+
}
155+
}
156+
135157
const ForceGraph = () => {
136158
const minNodeR = 5;
137159
const xMin = 0;
@@ -153,7 +175,6 @@ const ForceGraph = () => {
153175
onMessage: (e) => {
154176
let data = JSON.parse(e.data);
155177
let {gData, treeEntries} = processGitData(data, toObj(graphData.nodes, n => n.id));
156-
console.log(gData);
157178
setLinkData(gData);
158179
setCommitDatesToX(getCommitXAxis(gData, xMin));
159180
setGraphData(gData);
@@ -172,15 +193,17 @@ const ForceGraph = () => {
172193
},
173194
});
174195

175-
const drawNode = (node, ctx, globalScale) => {
196+
const drawNode = (node, ctx, globalScale, assignFillStyle) => {
176197
if (node.type === "ref") {
177198
const label = node.id;
178199
const fontSize = Math.max(minNodeR, 15 / globalScale);
179200
ctx.font = `${fontSize}px Sans-Serif`;
180201
const textWidth = ctx.measureText(label).width;
181202
const bckgDimensions = [textWidth, fontSize].map(n => n + fontSize * 0.2); // some padding
182203

183-
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
204+
if (assignFillStyle) {
205+
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
206+
}
184207
ctx.fillRect(node.x - bckgDimensions[0] / 2, node.y - bckgDimensions[1] / 2, ...bckgDimensions);
185208

186209
ctx.textAlign = 'center';
@@ -191,7 +214,9 @@ const ForceGraph = () => {
191214
} else {
192215
const r = Math.max(minNodeR, Math.min(15 / globalScale, 30));
193216
node.radius = r;
194-
ctx.fillStyle = node.color;
217+
if (assignFillStyle) {
218+
ctx.fillStyle = node.color;
219+
}
195220
ctx.beginPath();
196221
ctx.arc(node.x, node.y, r, 0, 2 * Math.PI, false);
197222
ctx.fill();
@@ -216,28 +241,10 @@ const ForceGraph = () => {
216241
setHighlightLinks(highlightLinks);
217242
};
218243

219-
const getRandX = (node, min, max) => {
220-
if (node.randX) {
221-
return node.randX;
222-
} else {
223-
node.randX = randomIntFromInterval(min, max);
224-
return node.randX;
225-
}
226-
}
227-
228-
const getRandY = (node, min, max) => {
229-
if (node.randY) {
230-
return node.randY;
231-
} else {
232-
node.randY = randomIntFromInterval(min, max);
233-
return node.randY;
234-
}
235-
}
236-
237244
useEffect(() => {
238245
let xMax = Math.max(...Object.values(commitDatesToX));
239246
if (!Number.isFinite(xMax)) {
240-
xMax = 10;
247+
xMax = 500;
241248
}
242249
const fg = fgRef.current;
243250
fg.d3Force("center", null);
@@ -263,33 +270,34 @@ const ForceGraph = () => {
263270
fg.d3Force("x",
264271
d3.forceX()
265272
.x(node => {
266-
if (node.type === "commit") {
267-
return commitDatesToX[node.value.object.commitTime];
268-
} else if (node.type === "tree") {
269-
const treeCommit = graphData.nodes.find(n => n.type === "commit" && n.id === node.hidden.commit);
270-
if (treeCommit) {
271-
return commitDatesToX[treeCommit.value.object.commitTime]
272-
} else {
273-
return getRandX(node, xMin, xMax)
274-
}
275-
} else if (node.type === "blob") {
276-
const blobCommit = graphData.nodes.find(n => n.type === "commit" && n.id === node.hidden.firstCommitRef);
277-
if (blobCommit) {
278-
return commitDatesToX[blobCommit.value.object.commitTime];
279-
} else {
280-
return getRandX(node, xMin, xMax);
281-
}
282-
} else if (node.type === "ref") {
283-
const refCommit = graphData.nodes.find(n => {
284-
return n.type === "commit" && n.id === node.value.object.commit
285-
});
286-
if (refCommit) {
287-
return commitDatesToX[refCommit.value.object.commitTime];
288-
} else {
273+
switch (node.type) {
274+
case "commit":
275+
return commitDatesToX[node.value.object.commitTime];
276+
case "tree":
277+
const treeCommit = graphData.nodes.find(n => n.type === "commit" && n.id === node.hidden.commit);
278+
if (treeCommit) {
279+
return commitDatesToX[treeCommit.value.object.commitTime]
280+
} else {
281+
return getRandX(node, xMin, xMax);
282+
}
283+
case "blob":
284+
const blobCommit = graphData.nodes.find(n => n.type === "commit" && n.id === node.hidden.firstCommitRef);
285+
if (blobCommit) {
286+
return commitDatesToX[blobCommit.value.object.commitTime];
287+
} else {
288+
return getRandX(node, xMin, xMax);
289+
}
290+
case "ref":
291+
const refCommit = graphData.nodes.find(n => {
292+
return n.type === "commit" && n.id === node.value.object.commit
293+
});
294+
if (refCommit) {
295+
return commitDatesToX[refCommit.value.object.commitTime];
296+
} else {
297+
return getRandX(node, xMin, xMax);
298+
}
299+
default:
289300
return getRandX(node, xMin, xMax);
290-
}
291-
} else {
292-
return getRandX(node, xMin, xMax);
293301
}
294302
}).strength(1)
295303
);
@@ -322,12 +330,16 @@ const ForceGraph = () => {
322330
node.fx = node.x;
323331
node.fy = node.y
324332
}}
325-
nodeCanvasObject={drawNode}
333+
nodeCanvasObject={(node, ctx, globalScale) => drawNode(node, ctx, globalScale, true)}
326334
onZoomEnd={(transform) => {
327335
if (fgRef.current) {
328336
fgRef.current.d3Force("collide", d3.forceCollide((node) => node.radius + 10));
329337
}
330338
}}
339+
nodePointerAreaPaint={(node, color, ctx, globalScale) => {
340+
ctx.fillStyle = color;
341+
drawNode(node, ctx, globalScale, false);
342+
}}
331343
/>
332344
<ObjectModal
333345
show={show}

0 commit comments

Comments
 (0)