yangdx commited on
Commit
7f62f8d
·
1 Parent(s): 2f9681a

Improve node layout by using polar coordinates for positioning expanded nodes

Browse files
lightrag_webui/src/hooks/useLightragGraph.tsx CHANGED
@@ -503,13 +503,25 @@ const useLightrangeGraph = () => {
503
  Constants.minNodeSize + scale * Math.pow((nodeDegree - minDegree) / range, 0.5)
504
  );
505
 
506
- // Calculate position relative to expanded node
 
 
 
 
 
 
 
 
 
 
 
 
507
  const x = nodePositions[nodeId]?.x ||
508
- (nodePositions[nodeToExpand.id].x + (Math.random() - 0.5) * 0.5);
509
  const y = nodePositions[nodeId]?.y ||
510
- (nodePositions[nodeToExpand.id].y + (Math.random() - 0.5) * 0.5);
511
 
512
- // Add the new node to the sigma graph
513
  sigmaGraph.addNode(nodeId, {
514
  label: newNode.labels.join(', '),
515
  color: newNode.color,
 
503
  Constants.minNodeSize + scale * Math.pow((nodeDegree - minDegree) / range, 0.5)
504
  );
505
 
506
+ // Get camera ratio from sigma instance for scale adjustment
507
+ const cameraRatio = useGraphStore.getState().sigmaInstance?.getCamera().ratio || 1;
508
+
509
+ // Calculate spread factor based on node size and number of nodes
510
+ const spreadFactor = Math.max(
511
+ nodeToExpand.size * 4, // Base on node size
512
+ Math.sqrt(nodesToAdd.size) * 10 // Scale with number of nodes
513
+ ) / cameraRatio; // Adjust for zoom level
514
+
515
+ // Calculate angle for polar coordinates
516
+ const angle = 2 * Math.PI * (Array.from(nodesToAdd).indexOf(nodeId) / nodesToAdd.size);
517
+
518
+ // Calculate final position
519
  const x = nodePositions[nodeId]?.x ||
520
+ (nodePositions[nodeToExpand.id].x + Math.cos(angle) * spreadFactor);
521
  const y = nodePositions[nodeId]?.y ||
522
+ (nodePositions[nodeToExpand.id].y + Math.sin(angle) * spreadFactor);
523
 
524
+ // Add the new node to the sigma graph with calculated position
525
  sigmaGraph.addNode(nodeId, {
526
  label: newNode.labels.join(', '),
527
  color: newNode.color,