yangdx
commited on
Commit
·
9e8a018
1
Parent(s):
78c77fe
Add edge size handling for nodeexpansion
Browse files
lightrag_webui/src/hooks/useLightragGraph.tsx
CHANGED
@@ -646,10 +646,23 @@ const useLightrangeGraph = () => {
|
|
646 |
// Get degree maxDegree from existing graph for size calculations
|
647 |
const minDegree = 1;
|
648 |
let maxDegree = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
649 |
sigmaGraph.forEachNode(node => {
|
650 |
const degree = sigmaGraph.degree(node);
|
651 |
maxDegree = Math.max(maxDegree, degree);
|
652 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
653 |
|
654 |
// First identify connectable nodes (nodes connected to the expanded node)
|
655 |
for (const node of processedNodes) {
|
@@ -722,6 +735,7 @@ const useLightrangeGraph = () => {
|
|
722 |
const range = maxDegree - minDegree || 1; // Avoid division by zero
|
723 |
const scale = Constants.maxNodeSize - Constants.minNodeSize;
|
724 |
|
|
|
725 |
for (const nodeId of nodesWithDiscardedEdges) {
|
726 |
if (sigmaGraph.hasNode(nodeId)) {
|
727 |
let newDegree = sigmaGraph.degree(nodeId);
|
@@ -741,6 +755,25 @@ const useLightrangeGraph = () => {
|
|
741 |
}
|
742 |
}
|
743 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
744 |
|
745 |
// If no new connectable nodes found, show toast and return
|
746 |
if (nodesToAdd.size === 0) {
|
@@ -837,9 +870,18 @@ const useLightrangeGraph = () => {
|
|
837 |
continue;
|
838 |
}
|
839 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
840 |
// Add the edge to the sigma graph
|
841 |
newEdge.dynamicId = sigmaGraph.addDirectedEdge(newEdge.source, newEdge.target, {
|
842 |
-
label: newEdge.properties?.keywords || undefined
|
|
|
|
|
843 |
});
|
844 |
|
845 |
// Add the edge to the raw graph
|
@@ -861,9 +903,11 @@ const useLightrangeGraph = () => {
|
|
861 |
// Reset search engine to force rebuild
|
862 |
useGraphStore.getState().resetSearchEngine();
|
863 |
|
864 |
-
// Update sizes for all nodes
|
865 |
updateNodeSizes(sigmaGraph, nodesWithDiscardedEdges, minDegree, maxDegree);
|
|
|
866 |
|
|
|
867 |
if (sigmaGraph.hasNode(nodeId)) {
|
868 |
const finalDegree = sigmaGraph.degree(nodeId);
|
869 |
const limitedDegree = Math.min(finalDegree, maxDegree + 1);
|
|
|
646 |
// Get degree maxDegree from existing graph for size calculations
|
647 |
const minDegree = 1;
|
648 |
let maxDegree = 0;
|
649 |
+
|
650 |
+
// Initialize edge weight min and max values
|
651 |
+
let minWeight = Number.MAX_SAFE_INTEGER;
|
652 |
+
let maxWeight = 0;
|
653 |
+
|
654 |
+
// Calculate node degrees and edge weights from existing graph
|
655 |
sigmaGraph.forEachNode(node => {
|
656 |
const degree = sigmaGraph.degree(node);
|
657 |
maxDegree = Math.max(maxDegree, degree);
|
658 |
});
|
659 |
+
|
660 |
+
// Calculate edge weights from existing graph
|
661 |
+
sigmaGraph.forEachEdge(edge => {
|
662 |
+
const weight = sigmaGraph.getEdgeAttribute(edge, 'originalWeight') || 1;
|
663 |
+
minWeight = Math.min(minWeight, weight);
|
664 |
+
maxWeight = Math.max(maxWeight, weight);
|
665 |
+
});
|
666 |
|
667 |
// First identify connectable nodes (nodes connected to the expanded node)
|
668 |
for (const node of processedNodes) {
|
|
|
735 |
const range = maxDegree - minDegree || 1; // Avoid division by zero
|
736 |
const scale = Constants.maxNodeSize - Constants.minNodeSize;
|
737 |
|
738 |
+
// Update node sizes
|
739 |
for (const nodeId of nodesWithDiscardedEdges) {
|
740 |
if (sigmaGraph.hasNode(nodeId)) {
|
741 |
let newDegree = sigmaGraph.degree(nodeId);
|
|
|
755 |
}
|
756 |
}
|
757 |
};
|
758 |
+
|
759 |
+
// Helper function to update edge sizes
|
760 |
+
const updateEdgeSizes = (
|
761 |
+
sigmaGraph: DirectedGraph,
|
762 |
+
minWeight: number,
|
763 |
+
maxWeight: number
|
764 |
+
) => {
|
765 |
+
// Update edge sizes
|
766 |
+
const minEdgeSize = useSettingsStore.getState().minEdgeSize;
|
767 |
+
const maxEdgeSize = useSettingsStore.getState().maxEdgeSize;
|
768 |
+
const weightRange = maxWeight - minWeight || 1; // Avoid division by zero
|
769 |
+
const sizeScale = maxEdgeSize - minEdgeSize;
|
770 |
+
|
771 |
+
sigmaGraph.forEachEdge(edge => {
|
772 |
+
const weight = sigmaGraph.getEdgeAttribute(edge, 'originalWeight') || 1;
|
773 |
+
const scaledSize = minEdgeSize + sizeScale * Math.pow((weight - minWeight) / weightRange, 0.5);
|
774 |
+
sigmaGraph.setEdgeAttribute(edge, 'size', scaledSize);
|
775 |
+
});
|
776 |
+
};
|
777 |
|
778 |
// If no new connectable nodes found, show toast and return
|
779 |
if (nodesToAdd.size === 0) {
|
|
|
870 |
continue;
|
871 |
}
|
872 |
|
873 |
+
// Get weight from edge properties or default to 1
|
874 |
+
const weight = newEdge.properties?.weight !== undefined ? Number(newEdge.properties.weight) : 1;
|
875 |
+
|
876 |
+
// Update min and max weight values
|
877 |
+
minWeight = Math.min(minWeight, weight);
|
878 |
+
maxWeight = Math.max(maxWeight, weight);
|
879 |
+
|
880 |
// Add the edge to the sigma graph
|
881 |
newEdge.dynamicId = sigmaGraph.addDirectedEdge(newEdge.source, newEdge.target, {
|
882 |
+
label: newEdge.properties?.keywords || undefined,
|
883 |
+
size: weight, // Set initial size based on weight
|
884 |
+
originalWeight: weight // Store original weight for recalculation
|
885 |
});
|
886 |
|
887 |
// Add the edge to the raw graph
|
|
|
903 |
// Reset search engine to force rebuild
|
904 |
useGraphStore.getState().resetSearchEngine();
|
905 |
|
906 |
+
// Update sizes for all nodes and edges
|
907 |
updateNodeSizes(sigmaGraph, nodesWithDiscardedEdges, minDegree, maxDegree);
|
908 |
+
updateEdgeSizes(sigmaGraph, minWeight, maxWeight);
|
909 |
|
910 |
+
// Final update for the expanded node
|
911 |
if (sigmaGraph.hasNode(nodeId)) {
|
912 |
const finalDegree = sigmaGraph.degree(nodeId);
|
913 |
const limitedDegree = Math.min(finalDegree, maxDegree + 1);
|