2014-02-11 34 views
0

我從Cytoscape 3.1測試版3使用內置導出器導出到JSON(cyjs)文件中導出了蛋白質蛋白質網絡。我試圖使用Cytoscape.js庫將這個cyjs文件導入到Web應用程序,但是我失敗了。導入Cytoscape桌面3.1測試版導出JSON文件(.cyjs)到Cytoscape.js

我昨天讀了gcpdev問的問題(Exporting and importing JSON data to Cytoscape.js),但它對我沒有幫助。

導入的JavaScript庫:jQuery-1.11,Cytoscape.js,Arbor,Arbor-tween。 我的代碼是:

<script type="text/javascript"> 

$('#cy').cytoscape({ 
    layout: { 
    name: 'arbor', 
    liveUpdate: true, 
    }, 

    ready: function(){ 

    window.cy = this; 
    var jsonfile = "src/inside_sc.cyjs"; 

    $(document).ready(function() { 

     $.getJSON(jsonfile, function(json) { 
     cy.add(JSON.parse(json)); 
     }); 

    }); 

    } 
}); 

</script> 

我總是得到一個錯誤消息,這樣在Chrome控制檯:

Uncaught SyntaxError: Unexpected token o  index.html:38 
    (anonymous function)      index.html:38 
    j           jquery-1.11.0.min.js:2 
    k.fireWith         jquery-1.11.0.min.js:2 
    x           jquery-1.11.0.min.js:4 
    b           jquery-1.11.0.min.js:4 

其中的index.html是我的文件和行:38:

cy.add(JSON.parse(json)); 

我得到了相同的錯誤消息,並進行了更改。如果我添加一些樣式和基本節點邊緣,也不能使用$.parseJSON()函數。

我想這行更改爲cy.add(json);結果是像上面提到的問題是相同的:

An element must be of type `nodes` or `edges`; you specified `undefined` 
Uncaught TypeError: Cannot read property 'single' of undefined 

當我嘗試顯示JSON信息到控制檯,像console.log(json);它工作正常。

我的腳本有什麼問題?我該怎麼辦?

回答

1

據我所知,從Cytoscape-desktop導出的JS不是替代cy.add()或init選項的替代品。您需要檢查由Cytoscape-desktop生成的文件,並在init選項(options.elements)中指定JSON中元素的路徑。

+0

對不起,但我是應用程序開發的初學者,我不太瞭解。那麼,我應該怎樣編碼才能將一個簡單的CyDesktop導出轉換爲真正的CyJS網絡? 我怎樣才能實現元素對象的新元素? – zoliqa

+0

您需要使用'options:{elements:json.path.to.elements}'或類似內容,其中'path.to.elements'是從Cytoscape導出的指向元素的JSON中的路徑。 – maxkfranz

+0

@maxkfranz我有一個變量'newdata',它包含一個指定'elements'的字符串。在'$('#cy')。cytoscape({'我可以做'elements:newdata'? –