2017-12-11 6 views
0

私は端末でnodejsを使ってcytoscapejsを使って簡単なjavascriptを書いています。私の目的は、ノードとエッジリストとレイアウトが与えられたすべてのノードの位置を取得することです。私はコードの下に実行します。私は位置の結果を見るとCytoscapejs layputはすべてのノードに0を与えます

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を正しく使用する方法は?ありがとうございました。

答えて

1

あなたは、連続的/非同期的レイアウトを実行しており、離散/同期レイアウトのように終了すると考えています。

レイアウトが完了するまで待ちます。 initでレイアウトを指定する場合はcy.ready()を使用するか、を作成してlayoutstopにバインドする - つまりlayout.on('layoutstop',() => { /* ... */ })を使用することをおすすめします。

+0

Cytoscapeが以前に非同期を実行していることがわかりませんでした。今私はそれを解決しました。 – Bharata

関連する問題