Fit compact graph nodes to nearest separation

This commit is contained in:
Meta Vens 2026-05-18 09:16:44 +01:00
commit acafadfbba
2 changed files with 58 additions and 6 deletions

View file

@ -541,7 +541,7 @@ export function EpiphanyGraphViewer({
{node.badgeText}
</text>
{node.status?.trim() && activeTransform.scale > 0.66 && (
{node.status?.trim() && node.width >= 112 && activeTransform.scale > 0.66 && (
<g transform={`translate(${node.width - 78} 10)`} opacity={fadeBetween(activeTransform.scale, 0.62, 0.9)}>
<rect
x={0}
@ -567,7 +567,7 @@ export function EpiphanyGraphViewer({
</g>
)}
{node.linkCount > 0 && (
{node.linkCount > 0 && node.width >= 52 && node.height >= 42 && (
<g transform={`translate(${node.width - 30} ${node.height - 26})`}>
<circle
cx={0}
@ -591,7 +591,7 @@ export function EpiphanyGraphViewer({
</g>
)}
<g opacity={fadeBetween(activeTransform.scale, 0.5, 0.82)} pointerEvents="none">
<g opacity={node.width >= 86 ? fadeBetween(activeTransform.scale, 0.5, 0.82) : 0} pointerEvents="none">
{renderTextLines(
copyLayout.titleLines,
44,

View file

@ -114,13 +114,14 @@ async function layoutGraph(
stroke: nodeStroke(graphKey, node.status),
};
});
const separatedNodes = resizeNodesForSeparation(positionedNodes, algorithm);
const layoutEdgeLookup = new Map((layout.edges ?? []).map((edge, index) => [edge.id ?? `edge-${graphKey}-${index}`, edge]));
const nodeLookup = new Map(positionedNodes.map((node) => [node.id, node]));
const nodeLookup = new Map(separatedNodes.map((node) => [node.id, node]));
const positionedEdges: PositionedEdge[] = graph.edges.map((source, index) => {
const resolvedId = resolveEdgeId(source, index);
const layoutEdge = layoutEdgeLookup.get(resolvedId);
const points = edgeSectionsToPoints(layoutEdge?.sections ?? []);
const points = isCompactAlgorithm(algorithm) ? [] : edgeSectionsToPoints(layoutEdge?.sections ?? []);
const fallbackPoints = points.length > 0 ? points : straightEdgePoints(source, nodeLookup);
return {
...source,
@ -140,11 +141,62 @@ async function layoutGraph(
graphKey,
width: Math.max(720, layout.width ?? 720),
height: Math.max(520, layout.height ?? 520),
nodes: positionedNodes,
nodes: separatedNodes,
edges: positionedEdges,
};
}
function resizeNodesForSeparation(nodes: PositionedNode[], algorithm: string): PositionedNode[] {
if (!isCompactAlgorithm(algorithm) || nodes.length < 2) {
return nodes;
}
const centerDistances: number[] = [];
for (let leftIndex = 0; leftIndex < nodes.length; leftIndex += 1) {
const left = nodeCenter(nodes[leftIndex]);
for (let rightIndex = leftIndex + 1; rightIndex < nodes.length; rightIndex += 1) {
const right = nodeCenter(nodes[rightIndex]);
const distance = Math.hypot(right.x - left.x, right.y - left.y);
if (Number.isFinite(distance) && distance > 1) {
centerDistances.push(distance);
}
}
}
if (centerDistances.length === 0) {
return nodes;
}
const minimumSeparation = Math.min(...centerDistances);
const targetWidth = clamp(minimumSeparation * 0.72, 34, 220);
const targetHeight = clamp(minimumSeparation * 0.42, 24, 104);
return nodes.map((node) => {
const center = nodeCenter(node);
const width = Math.min(node.width, targetWidth);
const height = Math.min(node.height, targetHeight);
return {
...node,
x: center.x - width / 2,
y: center.y - height / 2,
width,
height,
};
});
}
function isCompactAlgorithm(algorithm: string) {
return algorithm === "org.eclipse.elk.stress" || algorithm === "org.eclipse.elk.force";
}
function nodeCenter(node: { x: number; y: number; width: number; height: number }) {
return {
x: node.x + node.width / 2,
y: node.y + node.height / 2,
};
}
function layoutOptionsFor(graphKey: GraphKey, algorithm: string): Record<string, string> {
if (algorithm === "org.eclipse.elk.stress") {
return {