如何在Cytoscape JS中给出节点的行和列位置

时间:2014-08-18 06:58:05

标签: javascript cytoscape.js

我有一些节点要放在网格中的特定位置。

在Cytoscape Js中,我将数据定义如下:

 nodes: [
      { data: { id: '1', name: 'Cytoscape1', row: '1', col:'1' } },
      { data: { id: '2', name: 'Cytoscape2', row: '2', col:'3' } },
      { data: { id: '3', name: 'Cytoscape3', row: '1', col:'3' } },
      { data: { id: '4', name: 'Cytoscape4', row: '1', col:'4' } }
    ]

我想使用col和row数据来设置节点位置。现在我在图形的以下网格布局选项中添加了:

layout: {
    name: 'grid',
    padding: 10,
    rows: 4, 
    columns: 4, 
    position: function( node ){ return {node.row, node.col}; } 
  }

我的问题涉及返回节点的行和列位置的函数。由于某种原因,这部分代码不起作用,我找不到任何使用此选项的示例。

在实例中测试它时会出现语法错误。现在我尝试了许多不同的方法来返回与语法相匹配的行和列,但是我只是变得更加困惑而且还没有任何工作。

到目前为止我尝试过但没有成功的例子是:

position: function ( node ) { return node.row, node.col }
position: function ( node ) { return node.data.row, node.data.col }
position: function ( node ) { return data:row, data:col }
position: function ( node ) { return {data:row, data:col}; }
position: function ( node ) { return node:row, node:col }
position: function ( node ) { return {node:row, node:col}; }

等等。

更新:经过一些搜索以获取对row和col值的访问权限后,我现在知道它必须是node.data('row')和node.data('col')。但是我仍然有如何返回row和col参数的问题。我试过了:

position: function ( node ) { return [node.data('row'), node.data('col')]; }

然而仍然没有运气:/

2 个答案:

答案 0 :(得分:1)

啊,谢谢Mrintunjay!正是我需要将它拼凑在一起。答案是:

position: function( node ){ return {row:node.data('row'), col:node.data('col')}; } 

答案 1 :(得分:0)

我不了解Cytoscap JS

但这就像在简单的js中给出错误

position: function( node ){ return {node.row, node.col}; } 

你没有给予要返回的对象的键,它应该是至少的。

position: function( node ){ return {row:node.row, col:node.col}; }

或者这个

position: function( node ){ return {row:node.data.row, col:node.data.col}; }

我不确定参数中的node是什么。