【问题标题】:How to give the row and col positions of a node in Cytoscap JS如何在 Cytoscape JS 中给出节点的行和列位置
【发布时间】:2014-08-18 06:58:04
【问题描述】:

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

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

等等。

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

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

但是仍然没有运气:/

【问题讨论】:

    标签: javascript cytoscape.js


    【解决方案1】:

    啊,谢谢 Mrintunjay!正是我需要把它拼凑起来的。答案是:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-23
        • 1970-01-01
        相关资源
        最近更新 更多