如何在CYTOSCAPE JS中突出显示两个节点之间的路径 [英] How to highlight the path between two nodes in CYTOSCAPE JS

查看:294
本文介绍了如何在CYTOSCAPE JS中突出显示两个节点之间的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 cytoscape js 库创建图。我遵循教程,并且实现了像这样。

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

代码:

$(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': '#ddd',
        'target-arrow-color': '#ddd'
      })
    .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' } }
      ], 

      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' } }       
      ]
    },

  layout: {
    name: 'breadthfirst',
    directed: true,
    roots: '#a',
    padding: 5
  },

  ready: function(){
    window.cy = this;

    var bfs = cy.elements().bfs('#a', function(){}, true);

    var i = 0;
    var highlightNextEle = function(){
      bfs.path[i].addClass('highlighted');

      if( i < bfs.path.length ){
        i++;
        setTimeout(highlightNextEle, 1000);
      }
    };

    // kick off first highlight
    highlightNextEle();
  }
});

}); // on dom ready

在我的实现中,我需要突出显示节点d和g之间的路径。

on my implementation i need to highlight the path between the nodes d and g.

如何找到节点之间的路径并突出显示它们?

How to find the path between the nodes and highlight them?

推荐答案

使用dijkstra算法方法,我们可以找到节点之间的路径。

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') );

完整代码:

$(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': '#ddd',
        'target-arrow-color': '#ddd'
      })
    .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

这篇关于如何在CYTOSCAPE JS中突出显示两个节点之间的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆