最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Cytoscape Js cose layout为所有节点赋予0

IT培训 admin 6浏览 0评论

Cytoscape Js cose layout为所有节点赋予0

我正在使用终端中的nodejs使用cytoscapejs编写一个简单的javascript。我的目标是获得给定节点和边列表和布局的所有节点的位置。我运行下面的代码:

var cytoscape = require('cytoscape');

var cy = cytoscape({

  elements: [ // list of graph elements to start with
    { // node a
      data: { id: 'a' }
    },
    { // node b
      data: { id: 'b' }
    },
    { // node b
      data: { id: 'c' }
    },
    { // node b
      data: { id: 'd' }
    },
    { // node b
      data: { id: 'e' }
    },
    { // node b
      data: { id: 'f' }
    },
    { // node b
      data: { id: 'g' }
    },
    { // node b
      data: { id: 'h' }
    },
    { // edge ab
      data: { id: 'ab', source: 'a', target: 'b' }
    },
    { // edge ab
      data: { id: 'bc', source: 'b', target: 'c' }
    },
    { // edge ab
      data: { id: 'cd', source: 'c', target: 'd' }
    },
    { // edge ab
      data: { id: 'de', source: 'd', target: 'e' }
    },
    { // edge ab
      data: { id: 'ef', source: 'e', target: 'f' }
    },
    { // edge ab
      data: { id: 'fg', source: 'f', target: 'g' }
    },
    { // edge ab
      data: { id: 'gh', source: 'g', target: 'h' }
    }
  ],

  style: [ // the stylesheet for the graph
    {
      selector: 'node',
      style: {
        'background-color': '#666',
        'label': 'data(id)'
      }
    },

    {
      selector: 'edge',
      style: {
        'width': 3,
        'line-color': '#ccc',
        'target-arrow-color': '#ccc',
        'target-arrow-shape': 'triangle'
      }
    }
  ],
  layout: {name: "cose",randomize: true}
});

cy.nodes().forEach(function(s){
    console.log(s.position().x)
    console.log(s.position().y)
});

当我看到位置的结果时,它为所有节点给出0。如果我使用其他类型的布局,它会给出除0以外的一些结果。如何正确使用cose进行布局?谢谢。

回答如下:

您正在运行连续/异步布局,并期望它像离散/同步布局一样完成。

等待布局完成。如果你想在init指定布局,或者最好创建一个cy.ready()并绑定到layout - 即layoutstop,请使用layout.on('layoutstop', () => { /* ... */ })

Cytoscape Js cose layout为所有节点赋予0

我正在使用终端中的nodejs使用cytoscapejs编写一个简单的javascript。我的目标是获得给定节点和边列表和布局的所有节点的位置。我运行下面的代码:

var cytoscape = require('cytoscape');

var cy = cytoscape({

  elements: [ // list of graph elements to start with
    { // node a
      data: { id: 'a' }
    },
    { // node b
      data: { id: 'b' }
    },
    { // node b
      data: { id: 'c' }
    },
    { // node b
      data: { id: 'd' }
    },
    { // node b
      data: { id: 'e' }
    },
    { // node b
      data: { id: 'f' }
    },
    { // node b
      data: { id: 'g' }
    },
    { // node b
      data: { id: 'h' }
    },
    { // edge ab
      data: { id: 'ab', source: 'a', target: 'b' }
    },
    { // edge ab
      data: { id: 'bc', source: 'b', target: 'c' }
    },
    { // edge ab
      data: { id: 'cd', source: 'c', target: 'd' }
    },
    { // edge ab
      data: { id: 'de', source: 'd', target: 'e' }
    },
    { // edge ab
      data: { id: 'ef', source: 'e', target: 'f' }
    },
    { // edge ab
      data: { id: 'fg', source: 'f', target: 'g' }
    },
    { // edge ab
      data: { id: 'gh', source: 'g', target: 'h' }
    }
  ],

  style: [ // the stylesheet for the graph
    {
      selector: 'node',
      style: {
        'background-color': '#666',
        'label': 'data(id)'
      }
    },

    {
      selector: 'edge',
      style: {
        'width': 3,
        'line-color': '#ccc',
        'target-arrow-color': '#ccc',
        'target-arrow-shape': 'triangle'
      }
    }
  ],
  layout: {name: "cose",randomize: true}
});

cy.nodes().forEach(function(s){
    console.log(s.position().x)
    console.log(s.position().y)
});

当我看到位置的结果时,它为所有节点给出0。如果我使用其他类型的布局,它会给出除0以外的一些结果。如何正确使用cose进行布局?谢谢。

回答如下:

您正在运行连续/异步布局,并期望它像离散/同步布局一样完成。

等待布局完成。如果你想在init指定布局,或者最好创建一个cy.ready()并绑定到layout - 即layoutstop,请使用layout.on('layoutstop', () => { /* ... */ })

发布评论

评论列表 (0)

  1. 暂无评论