HTMLGraph logo HTMLGraph

Accessing Graph Structure

The read-only graph structure can be accessed via the graph property of the canvas.

    
const element = document.getElementById('canvas');

const canvas = new CanvasBuilder(element)
  .build();

canvas.graph.getAllNodeIds().forEach(nodeId => {
  console.log(canvas.graph.getNode(nodeId));
});

  

The graph object provides all the necessary methods to retrieve the structure of the current graph.

Nodes

  1. get the state of a node

        
    const node = canvas.graph.getNode("node-1");
    
      
    This method returns null if the node does not exist.

  2. get the IDs of all nodes

        
    const nodeIds = canvas.graph.getAllNodeIds();
    
      

  3. get the IDs of all ports for a specific node

        
    const portIds = canvas.graph.getNodePortIds("node-1");
    
      
    This method returns null if the node does not exist.

  4. get the IDs of incoming edges for a specific node

        
    const edgeIds = canvas.graph.getNodeIncomingEdgeIds("node-1");
    
      
    This method returns null if the node does not exist.

  5. get the IDs of outgoing edges for a specific node

        
    const edgeIds = canvas.graph.getNodeOutgoingEdgeIds("node-1");
    
      
    This method returns null if the node does not exist.

  6. get the IDs of cycle edges for a specific node

        
    const edgeIds = canvas.graph.getNodeCycleEdgeIds("node-1");
    
      
    This method returns null if the node does not exist.

  7. get the IDs of adjacent edges for a specific node

        
    const edgeIds = canvas.graph.getNodeAdjacentEdgeIds("node-1");
    
      
    This method returns null if the node does not exist.

  8. get node ID for a specific HTMLElement

        
    const nodeId = canvas.graph.getElementNodeId(nodeElement);
    
      
    This method returns null if specified element is not a node.


Ports

  1. get the state of a port

        
    const port = canvas.graph.getPort("port-1");
    
      
    This method returns null if the port does not exist.

  2. get the IDs of all ports

        
    const portIds = canvas.graph.getAllPortIds();
    
      

  3. get the IDs of incoming edges for a specific port

        
    const edgeIds = canvas.graph.getPortIncomingEdgeIds("port-1");
    
      
    This method returns null if the port does not exist.

  4. get the IDs of outgoing edges for a specific port

        
    const edgeIds = canvas.graph.getPortOutgoingEdgeIds("port-1");
    
      
    This method returns null if the port does not exist.

  5. get the IDs of cycle edges for a specific port

        
    const edgeIds = canvas.graph.getPortCycleEdgeIds("port-1");
    
      
    This method returns null if the port does not exist.

  6. get the IDs of adjacent edges for a specific port

        
    const edgeIds = canvas.graph.getPortAdjacentEdgeIds("port-1");
    
      
    This method returns null if the port does not exist.

  7. get all port IDs attached to a specific HTMLElement

        
    const portIds = canvas.graph.getElementPortIds(portElement);
    
      


Edges

  1. get the IDs of all edges

        
    const edgeIds = canvas.graph.getAllEdgeIds();
    
      

  2. get the state of an edge

        
    const edge = canvas.graph.getEdge("edge-1");
    
      
    This method returns null if the edge does not exist.