如何在cytocap JS中给出节点的行和冷位置

How to give the row and col positions of a node in Cytoscap JS

本文关键字:节点 位置 cytocap JS      更新时间:2023-09-26

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

在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}; } 
  }

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

在实际示例中测试时,它给出了一个语法错误。现在,我已经尝试了许多不同的方法来返回与语法匹配的行和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}; }

等等

更新:经过更多的搜索以访问行和col值,我现在知道它必须是node.data('row')和node.data('col')。然而,我仍然有如何返回行和冷参数的问题。我试着:

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

然而仍然没有运气:/

啊,谢谢你!正是我需要拼凑的东西。答案是:

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

我不知道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是什么