Improve node layout by using polar coordinates for positioning expanded nodes

This commit is contained in:
yangdx
2025-03-15 00:32:40 +08:00
parent 413d201525
commit fc4582b260

View File

@@ -503,13 +503,25 @@ const useLightrangeGraph = () => {
Constants.minNodeSize + scale * Math.pow((nodeDegree - minDegree) / range, 0.5) Constants.minNodeSize + scale * Math.pow((nodeDegree - minDegree) / range, 0.5)
); );
// Calculate position relative to expanded node // Get camera ratio from sigma instance for scale adjustment
const x = nodePositions[nodeId]?.x || const cameraRatio = useGraphStore.getState().sigmaInstance?.getCamera().ratio || 1;
(nodePositions[nodeToExpand.id].x + (Math.random() - 0.5) * 0.5);
const y = nodePositions[nodeId]?.y ||
(nodePositions[nodeToExpand.id].y + (Math.random() - 0.5) * 0.5);
// Add the new node to the sigma graph // Calculate spread factor based on node size and number of nodes
const spreadFactor = Math.max(
nodeToExpand.size * 4, // Base on node size
Math.sqrt(nodesToAdd.size) * 10 // Scale with number of nodes
) / cameraRatio; // Adjust for zoom level
// Calculate angle for polar coordinates
const angle = 2 * Math.PI * (Array.from(nodesToAdd).indexOf(nodeId) / nodesToAdd.size);
// Calculate final position
const x = nodePositions[nodeId]?.x ||
(nodePositions[nodeToExpand.id].x + Math.cos(angle) * spreadFactor);
const y = nodePositions[nodeId]?.y ||
(nodePositions[nodeToExpand.id].y + Math.sin(angle) * spreadFactor);
// Add the new node to the sigma graph with calculated position
sigmaGraph.addNode(nodeId, { sigmaGraph.addNode(nodeId, {
label: newNode.labels.join(', '), label: newNode.labels.join(', '),
color: newNode.color, color: newNode.color,