Change to undirected graph

This commit is contained in:
yangdx
2025-04-07 01:17:44 +08:00
parent fe7b5ac13b
commit ad96bed7ac
2 changed files with 13 additions and 14 deletions

View File

@@ -31,7 +31,7 @@ import '@react-sigma/graph-search/lib/style.css'
const defaultSigmaSettings: Partial<SigmaSettings> = { const defaultSigmaSettings: Partial<SigmaSettings> = {
allowInvalidContainer: true, allowInvalidContainer: true,
defaultNodeType: 'default', defaultNodeType: 'default',
defaultEdgeType: 'curvedArrow', defaultEdgeType: 'curvedNoArrow',
renderEdgeLabels: false, renderEdgeLabels: false,
edgeProgramClasses: { edgeProgramClasses: {
arrow: EdgeArrowProgram, arrow: EdgeArrowProgram,

View File

@@ -1,4 +1,4 @@
import Graph, { DirectedGraph } from 'graphology' import Graph, { UndirectedGraph } from 'graphology'
import { useCallback, useEffect, useRef } from 'react' import { useCallback, useEffect, useRef } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { errorMessage } from '@/lib/utils' import { errorMessage } from '@/lib/utils'
@@ -303,7 +303,7 @@ const createSigmaGraph = (rawGraph: RawGraph | null) => {
} }
// Create new graph instance // Create new graph instance
const graph = new DirectedGraph() const graph = new UndirectedGraph()
// Add nodes from raw graph data // Add nodes from raw graph data
for (const rawNode of rawGraph?.nodes ?? []) { for (const rawNode of rawGraph?.nodes ?? []) {
@@ -329,10 +329,11 @@ const createSigmaGraph = (rawGraph: RawGraph | null) => {
// Get weight from edge properties or default to 1 // Get weight from edge properties or default to 1
const weight = rawEdge.properties?.weight !== undefined ? Number(rawEdge.properties.weight) : 1 const weight = rawEdge.properties?.weight !== undefined ? Number(rawEdge.properties.weight) : 1
rawEdge.dynamicId = graph.addDirectedEdge(rawEdge.source, rawEdge.target, { rawEdge.dynamicId = graph.addEdge(rawEdge.source, rawEdge.target, {
label: rawEdge.properties?.keywords || undefined, label: rawEdge.properties?.keywords || undefined,
size: weight, // Set initial size based on weight size: weight, // Set initial size based on weight
originalWeight: weight, // Store original weight for recalculation originalWeight: weight, // Store original weight for recalculation
type: 'curvedNoArrow' // Explicitly set edge type to no arrow
}) })
} }
@@ -486,7 +487,7 @@ const useLightrangeGraph = () => {
// Check if data is empty or invalid // Check if data is empty or invalid
if (!data || !data.nodes || data.nodes.length === 0) { if (!data || !data.nodes || data.nodes.length === 0) {
// Create a graph with a single "Graph Is Empty" node // Create a graph with a single "Graph Is Empty" node
const emptyGraph = new DirectedGraph(); const emptyGraph = new UndirectedGraph();
// Add a single node with "Graph Is Empty" label // Add a single node with "Graph Is Empty" label
emptyGraph.addNode('empty-graph-node', { emptyGraph.addNode('empty-graph-node', {
@@ -726,7 +727,7 @@ const useLightrangeGraph = () => {
// Helper function to update node sizes // Helper function to update node sizes
const updateNodeSizes = ( const updateNodeSizes = (
sigmaGraph: DirectedGraph, sigmaGraph: UndirectedGraph,
nodesWithDiscardedEdges: Set<string>, nodesWithDiscardedEdges: Set<string>,
minDegree: number, minDegree: number,
maxDegree: number maxDegree: number
@@ -758,7 +759,7 @@ const useLightrangeGraph = () => {
// Helper function to update edge sizes // Helper function to update edge sizes
const updateEdgeSizes = ( const updateEdgeSizes = (
sigmaGraph: DirectedGraph, sigmaGraph: UndirectedGraph,
minWeight: number, minWeight: number,
maxWeight: number maxWeight: number
) => { ) => {
@@ -866,9 +867,6 @@ const useLightrangeGraph = () => {
if (sigmaGraph.hasEdge(newEdge.source, newEdge.target)) { if (sigmaGraph.hasEdge(newEdge.source, newEdge.target)) {
continue; continue;
} }
if (sigmaGraph.hasEdge(newEdge.target, newEdge.source)) {
continue;
}
// Get weight from edge properties or default to 1 // Get weight from edge properties or default to 1
const weight = newEdge.properties?.weight !== undefined ? Number(newEdge.properties.weight) : 1; const weight = newEdge.properties?.weight !== undefined ? Number(newEdge.properties.weight) : 1;
@@ -878,10 +876,11 @@ const useLightrangeGraph = () => {
maxWeight = Math.max(maxWeight, weight); maxWeight = Math.max(maxWeight, weight);
// Add the edge to the sigma graph // Add the edge to the sigma graph
newEdge.dynamicId = sigmaGraph.addDirectedEdge(newEdge.source, newEdge.target, { newEdge.dynamicId = sigmaGraph.addEdge(newEdge.source, newEdge.target, {
label: newEdge.properties?.keywords || undefined, label: newEdge.properties?.keywords || undefined,
size: weight, // Set initial size based on weight size: weight, // Set initial size based on weight
originalWeight: weight // Store original weight for recalculation originalWeight: weight, // Store original weight for recalculation
type: 'curvedNoArrow' // Explicitly set edge type to no arrow
}); });
// Add the edge to the raw graph // Add the edge to the raw graph
@@ -935,7 +934,7 @@ const useLightrangeGraph = () => {
}, [nodeToExpand, sigmaGraph, rawGraph, t]); }, [nodeToExpand, sigmaGraph, rawGraph, t]);
// Helper function to get all nodes that will be deleted // Helper function to get all nodes that will be deleted
const getNodesThatWillBeDeleted = useCallback((nodeId: string, graph: DirectedGraph) => { const getNodesThatWillBeDeleted = useCallback((nodeId: string, graph: UndirectedGraph) => {
const nodesToDelete = new Set<string>([nodeId]); const nodesToDelete = new Set<string>([nodeId]);
// Find all nodes that would become isolated after deletion // Find all nodes that would become isolated after deletion
@@ -1063,7 +1062,7 @@ const useLightrangeGraph = () => {
// If no graph exists yet, create a new one and store it // If no graph exists yet, create a new one and store it
console.log('Creating new Sigma graph instance') console.log('Creating new Sigma graph instance')
const graph = new DirectedGraph() const graph = new UndirectedGraph()
useGraphStore.getState().setSigmaGraph(graph) useGraphStore.getState().setSigmaGraph(graph)
return graph as Graph<NodeType, EdgeType> return graph as Graph<NodeType, EdgeType>
}, [sigmaGraph]) }, [sigmaGraph])