【问题标题】:Cytoscape JS node thicknessCytoscape JS 节点厚度
【发布时间】:2018-03-08 01:14:59
【问题描述】:

我想用 Nodes 和 Nodes 之间的 Links 绘制一个简单的图形。问题是我希望根据变量设置链接的 thickness,因此我无法为此预先确定 CSS 类。

代码来自示例http://js.cytoscape.org/demos/dagre-layout/

var cy = window.cy = cytoscape({
  container: document.getElementById('cy'),

  boxSelectionEnabled: false,
  autounselectify: true,

  layout: {
    name: 'dagre'
  },

  style: [
    {
      selector: 'node',
      style: {
        'content': 'data(id)',
        'text-opacity': 0.5,
        'text-valign': 'center',
        'text-halign': 'right',
        'background-color': '#11479e'
      }
    },

    {
      selector: 'edge',
      style: {
        'curve-style': 'bezier',
        'width': 4,
        'target-arrow-shape': 'triangle',
        'line-color': '#9dbaea',
        'target-arrow-color': '#9dbaea'
      }
    }
  ],

  elements: {
    nodes: [
      { data: { id: 'n0' } },
      { data: { id: 'n1' } },
      { data: { id: 'n2' } }
    ],
    edges: [
      { data: { source: 'n0', target: 'n1' } },
      { data: { source: 'n1', target: 'n2' } },
    ]
  },
});

如何将这种功能添加到{ data }

【问题讨论】:

    标签: cytoscape.js cytoscape-web


    【解决方案1】:

    你总是可以像这样引用边缘的数据:

        // Initialize cytoscape
        cy = window.cy = cytoscape({
            container: $('.cy'),
            boxSelectionEnabled: false,
            autounselectify: true,
            layout: {
                name: 'yourLayout'
            },
            style: [
                {
                    selector: 'node',
                    style: {
                        'shape': 'data(faveShape)',
                        'content': 'data(DisplayName)',
                        'height': 'data(faveHeight)',
                        'width': 'data(faveWidth)',
                        'background-color': 'data(faveColor)',
                        'line-color': '#a8eae5',
                        'font-family': 'Segoe UI,Helvetica Neue,Helvetica,Arial,Verdana',
                        'font-size': '15px',
                    }
                },
    
                {
                    selector: 'edge',
                    style: {
                         'curve-style': 'bezier',
                         'width': data(myWidth),
                         'target-arrow-shape': 'triangle',
                         'line-color': '#9dbaea',
                         'target-arrow-color': '#9dbaea'
                    }
                }
            ],
        });
    

    当您定义节点的样式时,您使用 data(id) 作为节点名称,因此当您想要定义边缘的样式时,您始终可以使用相同的方法数据(whateverYouWantToGet)。

    当你定义边缘时,你可以这样做:

    var x = 0; // This is your variable, alter it like you want
    var i = 0;
    cy.add({
       data: {
          id: ('edge' + (i)),
          source: n0, // first node for example
          target: n1,
          myWidth: x
       },
       position: {},
       group: 'edges',
       removed: false,
       selected: false,
       selectable: true,
       locked: false,
       grabbed: false,
       grabbable: true,
       classes: 'autorotate'
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多