How to highlight the path between two nodes in CYTOSCAPE JS

前端 未结 3 903
南方客
南方客 2020-12-19 14:45

i can create a graph using cytoscape js library . i am following the this tutorial and i implement like this.

CODE:

$(function(){ /         


        
相关标签:
3条回答
  • 2020-12-19 15:24

    Using dijkstra algorithm method we can find the path between the nodes.

     var dijkstra = cy.elements().dijkstra('#e',function(){
              return this.data('weight');
            },false);
            var bfs = dijkstra.pathTo( cy.$('#i') );
    

    Complete CODE :

    $(function(){ // on dom ready
    
    $('#cy').cytoscape({
      style: cytoscape.stylesheet()
        .selector('node')
          .css({
            'content': 'data(id)'
          })
        .selector('edge')
          .css({
            'target-arrow-shape': 'triangle',
            'width': 4,
            'line-color': '#ffffd',
            'target-arrow-color': '#ffffd'
          })
        .selector('.highlighted')
          .css({
            'background-color': '#61bffc',
            'line-color': '#61bffc',
            'target-arrow-color': '#61bffc',
            'transition-property': 'background-color, line-color, target-arrow-color',
            'transition-duration': '0.5s'
          }),
    
      elements: {
          nodes: [
            { data: { id: 'a' } },
            { data: { id: 'b' } },
            { data: { id: 'c' } },
            { data: { id: 'd' } },
            { data: { id: 'e' } },
            { data: { id: 'f' } },
            { data: { id: 'g' } },
            { data: { id: 'h' } },
            { data: { id: 'i' } }
          ], 
    
          edges: [
            { data: { id: 'ab', weight: 1, source: 'a', target: 'b' } },
            { data: { id: 'ac', weight: 2, source: 'a', target: 'c' } },
            { data: { id: 'bd', weight: 3, source: 'b', target: 'd' } },
            { data: { id: 'be', weight: 4, source: 'b', target: 'e' } },
            { data: { id: 'cf', weight: 5, source: 'c', target: 'f' } },
            { data: { id: 'cg', weight: 6, source: 'c', target: 'g' } },
            { data: { id: 'ah', weight: 7, source: 'a', target: 'h' } },
            { data: { id: 'hi', weight: 8, source: 'h', target: 'i' } } 
          ]
        },
    
      layout: {
        name: 'breadthfirst',
        directed: true,
        roots: '#a',
        padding: 5
      },
    
      ready: function(){
        window.cy = this;
    
        var dijkstra = cy.elements().dijkstra('#e',function(){
          return this.data('weight');
        },false);
        var bfs = dijkstra.pathTo( cy.$('#i') );
        var x=0;
        var highlightNextEle = function(){
         var el=bfs[x];
          el.addClass('highlighted');
          if(x<bfs.length){
            x++;
            setTimeout(highlightNextEle, 500);
          }
           };
        highlightNextEle();
      }
    });
    
    }); // on dom ready
    
    0 讨论(0)
  • 2020-12-19 15:26

    Assuming you have picked two nodes and stored them in source_node and target_node, and you want to label everything in between with class 'path_element':

    p = cy.elements().aStar({root: source_node, goal: target_node, directed: true}).path;
    if (p) {
      p.filter(function(i,x) { return x != source_node && x != target_node; })
          .addClass('path_element');
    
      p.edgesWith(p)
          .addClass('path_element');
    }
    
    0 讨论(0)
  • 2020-12-19 15:44

    I you want to highlight all the possible paths

    event.target- is the starting node

    event.target.successors().animate({
          style: { lineColor: 'red' }
    });
    
    0 讨论(0)
提交回复
热议问题