From ef13c5134880fba05dd73c473e82eea89fee9382 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 18 May 2026 10:03:29 +0100 Subject: [PATCH] Restore readable compact graph cards --- .../src/lib/EpiphanyGraphViewer.tsx | 14 +- web/epiphany-graph-viewer/src/lib/layout.ts | 156 +++++++++++++++--- 2 files changed, 142 insertions(+), 28 deletions(-) diff --git a/web/epiphany-graph-viewer/src/lib/EpiphanyGraphViewer.tsx b/web/epiphany-graph-viewer/src/lib/EpiphanyGraphViewer.tsx index 3ac8117..76d56da 100644 --- a/web/epiphany-graph-viewer/src/lib/EpiphanyGraphViewer.tsx +++ b/web/epiphany-graph-viewer/src/lib/EpiphanyGraphViewer.tsx @@ -550,7 +550,7 @@ export function EpiphanyGraphViewer({ {node.badgeText} - {node.status?.trim() && node.width >= 128 && node.height >= 52 && activeTransform.scale > 0.66 && ( + {node.status?.trim() && node.width >= 112 && node.height >= 48 && activeTransform.scale > 0.66 && ( )} - {node.linkCount > 0 && node.width >= 72 && node.height >= 54 && ( + {node.linkCount > 0 && node.width >= 72 && node.height >= 50 && ( )} - = 112 && node.height >= 44 ? fadeBetween(activeTransform.scale, 0.5, 0.82) : 0} pointerEvents="none"> + = 92 && node.height >= 40 ? fadeBetween(activeTransform.scale, 0.42, 0.72) : 0} pointerEvents="none"> {renderTextLines( copyLayout.titleLines, 44, @@ -614,7 +614,7 @@ export function EpiphanyGraphViewer({ )} - = 180 && node.height >= 92 ? fadeBetween(activeTransform.scale, 1.02, 1.44) : 0} pointerEvents="none"> + = 136 && node.height >= 68 ? fadeBetween(activeTransform.scale, 1.02, 1.44) : 0} pointerEvents="none"> {renderTextLines( copyLayout.purposeLines, 16, @@ -627,7 +627,7 @@ export function EpiphanyGraphViewer({ )} - = 230 && node.height >= 125 ? fadeBetween(activeTransform.scale, 1.55, 1.92) : 0} pointerEvents="none"> + = 190 && node.height >= 96 ? fadeBetween(activeTransform.scale, 1.72, 2.18) : 0} pointerEvents="none"> {renderTextLines( copyLayout.mechanismLines, 16, @@ -1306,7 +1306,7 @@ function handleNativeWheel( const cursorY = event.clientY - rect.top; const current = transforms[graphKey]; const zoomFactor = event.deltaY < 0 ? 1.12 : 1 / 1.12; - const nextScale = clamp(current.scale * zoomFactor, 0.28, 2.8); + const nextScale = clamp(current.scale * zoomFactor, 0.28, 5.4); const worldX = (cursorX - current.x) / current.scale; const worldY = (cursorY - current.y) / current.scale; const nextX = cursorX - worldX * nextScale; @@ -1397,7 +1397,7 @@ function nudgeZoom( ...current, [graphKey]: { ...current[graphKey], - scale: clamp(current[graphKey].scale * factor, 0.28, 2.8), + scale: clamp(current[graphKey].scale * factor, 0.28, 5.4), userMoved: true, }, })); diff --git a/web/epiphany-graph-viewer/src/lib/layout.ts b/web/epiphany-graph-viewer/src/lib/layout.ts index ee7f69b..abde374 100644 --- a/web/epiphany-graph-viewer/src/lib/layout.ts +++ b/web/epiphany-graph-viewer/src/lib/layout.ts @@ -114,7 +114,7 @@ async function layoutGraph( stroke: nodeStroke(graphKey, node.status), }; }); - const separatedNodes = resizeNodesForSeparation(positionedNodes, algorithm); + const separatedNodes = normalizeNodeBounds(resizeNodesForSeparation(positionedNodes, algorithm)); const layoutEdgeLookup = new Map((layout.edges ?? []).map((edge, index) => [edge.id ?? `edge-${graphKey}-${index}`, edge])); const nodeLookup = new Map(separatedNodes.map((node) => [node.id, node])); @@ -139,42 +139,88 @@ async function layoutGraph( return { graphKey, - width: Math.max(720, layout.width ?? 720), - height: Math.max(520, layout.height ?? 520), + width: Math.max(720, layoutBounds(separatedNodes).width, layout.width ?? 720), + height: Math.max(520, layoutBounds(separatedNodes).height, layout.height ?? 520), nodes: separatedNodes, edges: positionedEdges, }; } +function normalizeNodeBounds(nodes: PositionedNode[]): PositionedNode[] { + const bounds = layoutBounds(nodes); + if (bounds.minX >= 0 && bounds.minY >= 0) { + return nodes; + } + + const offsetX = bounds.minX < 0 ? -bounds.minX + 24 : 0; + const offsetY = bounds.minY < 0 ? -bounds.minY + 24 : 0; + return nodes.map((node) => ({ + ...node, + x: node.x + offsetX, + y: node.y + offsetY, + })); +} + +function layoutBounds(nodes: PositionedNode[]) { + if (nodes.length === 0) { + return { minX: 0, minY: 0, width: 0, height: 0 }; + } + + const minX = Math.min(...nodes.map((node) => node.x)); + const minY = Math.min(...nodes.map((node) => node.y)); + const maxX = Math.max(...nodes.map((node) => node.x + node.width)); + const maxY = Math.max(...nodes.map((node) => node.y + node.height)); + return { + minX, + minY, + width: maxX - Math.min(0, minX) + 48, + height: maxY - Math.min(0, minY) + 48, + }; +} + 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); - } - } - } + const nearestDistances = nodes + .map((node, nodeIndex) => { + const center = nodeCenter(node); + let nearest = Number.POSITIVE_INFINITY; - if (centerDistances.length === 0) { + for (let otherIndex = 0; otherIndex < nodes.length; otherIndex += 1) { + if (otherIndex === nodeIndex) { + continue; + } + + const other = nodeCenter(nodes[otherIndex]); + const distance = Math.hypot(other.x - center.x, other.y - center.y); + if (Number.isFinite(distance) && distance > 1) { + nearest = Math.min(nearest, distance); + } + } + + return nearest; + }) + .filter((distance) => Number.isFinite(distance)) + .sort((left, right) => left - right); + + if (nearestDistances.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); + const separationSampleIndex = Math.min( + nearestDistances.length - 1, + Math.max(0, Math.floor(nearestDistances.length * 0.18)), + ); + const workingSeparation = nearestDistances[separationSampleIndex]; + const targetWidth = clamp(workingSeparation * 0.88, 112, 260); + const targetHeight = clamp(workingSeparation * 0.48, 56, 126); - return nodes.map((node) => { + const resizedNodes = nodes.map((node) => { const center = nodeCenter(node); - const width = Math.min(node.width, targetWidth); - const height = Math.min(node.height, targetHeight); + const width = clamp(targetWidth, 96, Math.max(260, node.width)); + const height = clamp(targetHeight, 48, Math.max(126, node.height)); return { ...node, @@ -184,6 +230,74 @@ function resizeNodesForSeparation(nodes: PositionedNode[], algorithm: string): P height, }; }); + + return relaxOverlappingNodes(resizedNodes); +} + +function relaxOverlappingNodes(nodes: PositionedNode[]): PositionedNode[] { + let relaxedNodes = nodes; + + for (let pass = 0; pass < 16; pass += 1) { + let moved = false; + const offsets = new Map( + relaxedNodes.map((node) => [node.id, { x: 0, y: 0 }]), + ); + + for (let leftIndex = 0; leftIndex < relaxedNodes.length; leftIndex += 1) { + const left = relaxedNodes[leftIndex]; + const leftCenter = nodeCenter(left); + + for (let rightIndex = leftIndex + 1; rightIndex < relaxedNodes.length; rightIndex += 1) { + const right = relaxedNodes[rightIndex]; + const rightCenter = nodeCenter(right); + const overlapX = (left.width + right.width) / 2 + 18 - Math.abs(rightCenter.x - leftCenter.x); + const overlapY = (left.height + right.height) / 2 + 14 - Math.abs(rightCenter.y - leftCenter.y); + + if (overlapX <= 0 || overlapY <= 0) { + continue; + } + + const pushAxis = overlapX < overlapY ? "x" : "y"; + const leftOffset = offsets.get(left.id); + const rightOffset = offsets.get(right.id); + if (!leftOffset || !rightOffset) { + continue; + } + + if (pushAxis === "x") { + const direction = rightCenter.x >= leftCenter.x ? 1 : -1; + const push = overlapX / 2; + leftOffset.x -= direction * push; + rightOffset.x += direction * push; + } else { + const direction = rightCenter.y >= leftCenter.y ? 1 : -1; + const push = overlapY / 2; + leftOffset.y -= direction * push; + rightOffset.y += direction * push; + } + moved = true; + } + } + + if (!moved) { + return relaxedNodes; + } + + relaxedNodes = relaxedNodes.map((node) => { + const offset = offsets.get(node.id); + if (!offset) { + return node; + } + + return { + ...node, + x: node.x + offset.x, + y: node.y + offset.y, + }; + }); + } + + return relaxedNodes; } function isCompactAlgorithm(algorithm: string) {