yangdx commited on
Commit
c92508a
·
1 Parent(s): 9e0ee27

Improve node size calculation logic for node expansion to prevent oversized

Browse files
lightrag_webui/src/hooks/useLightragGraph.tsx CHANGED
@@ -533,16 +533,21 @@ const useLightrangeGraph = () => {
533
  sigmaGraph: DirectedGraph,
534
  nodesWithDiscardedEdges: Set<string>,
535
  minDegree: number,
536
- range: number,
537
- scale: number
538
  ) => {
 
 
 
 
539
  for (const nodeId of nodesWithDiscardedEdges) {
540
  if (sigmaGraph.hasNode(nodeId)) {
541
  let newDegree = sigmaGraph.degree(nodeId);
542
  newDegree += 1; // Add +1 for discarded edges
 
 
543
 
544
  const newSize = Math.round(
545
- Constants.minNodeSize + scale * Math.pow((newDegree - minDegree) / range, 0.5)
546
  );
547
 
548
  const currentSize = sigmaGraph.getNodeAttribute(nodeId, 'size');
@@ -556,7 +561,7 @@ const useLightrangeGraph = () => {
556
 
557
  // If no new connectable nodes found, show toast and return
558
  if (nodesToAdd.size === 0) {
559
- updateNodeSizes(sigmaGraph, nodesWithDiscardedEdges, minDegree, range, scale);
560
  toast.info(t('graphPanel.propertiesView.node.noNewNodes'));
561
  return;
562
  }
@@ -585,8 +590,10 @@ const useLightrangeGraph = () => {
585
  const nodeDegree = nodeDegrees.get(nodeId) || 0;
586
 
587
  // Calculate node size
 
 
588
  const nodeSize = Math.round(
589
- Constants.minNodeSize + scale * Math.pow((nodeDegree - minDegree) / range, 0.5)
590
  );
591
 
592
  // Calculate angle for polar coordinates
@@ -661,7 +668,7 @@ const useLightrangeGraph = () => {
661
  useGraphStore.getState().resetSearchEngine();
662
 
663
  // Update sizes for all nodes with discarded edges
664
- updateNodeSizes(sigmaGraph, nodesWithDiscardedEdges, minDegree, range, scale);
665
 
666
  } catch (error) {
667
  console.error('Error expanding node:', error);
 
533
  sigmaGraph: DirectedGraph,
534
  nodesWithDiscardedEdges: Set<string>,
535
  minDegree: number,
536
+ maxDegree: number
 
537
  ) => {
538
+ // Calculate derived values inside the function
539
+ const range = maxDegree - minDegree || 1; // Avoid division by zero
540
+ const scale = Constants.maxNodeSize - Constants.minNodeSize;
541
+
542
  for (const nodeId of nodesWithDiscardedEdges) {
543
  if (sigmaGraph.hasNode(nodeId)) {
544
  let newDegree = sigmaGraph.degree(nodeId);
545
  newDegree += 1; // Add +1 for discarded edges
546
+ // Limit newDegree to maxDegree + 1 to prevent nodes from being too large
547
+ const limitedDegree = Math.min(newDegree, maxDegree + 1);
548
 
549
  const newSize = Math.round(
550
+ Constants.minNodeSize + scale * Math.pow((limitedDegree - minDegree) / range, 0.5)
551
  );
552
 
553
  const currentSize = sigmaGraph.getNodeAttribute(nodeId, 'size');
 
561
 
562
  // If no new connectable nodes found, show toast and return
563
  if (nodesToAdd.size === 0) {
564
+ updateNodeSizes(sigmaGraph, nodesWithDiscardedEdges, minDegree, maxDegree);
565
  toast.info(t('graphPanel.propertiesView.node.noNewNodes'));
566
  return;
567
  }
 
590
  const nodeDegree = nodeDegrees.get(nodeId) || 0;
591
 
592
  // Calculate node size
593
+ // Limit nodeDegree to maxDegree + 1 to prevent new nodes from being too large
594
+ const limitedDegree = Math.min(nodeDegree, maxDegree + 1);
595
  const nodeSize = Math.round(
596
+ Constants.minNodeSize + scale * Math.pow((limitedDegree - minDegree) / range, 0.5)
597
  );
598
 
599
  // Calculate angle for polar coordinates
 
668
  useGraphStore.getState().resetSearchEngine();
669
 
670
  // Update sizes for all nodes with discarded edges
671
+ updateNodeSizes(sigmaGraph, nodesWithDiscardedEdges, minDegree, maxDegree);
672
 
673
  } catch (error) {
674
  console.error('Error expanding node:', error);