Cytoscape.js,在边缘顶部显示圆形

时间:2019-09-27 15:57:27

标签: cytoscape.js

我有一个cytoscape.js模型,其中节点之间的边缘代表不同的关系。

我希望在边缘的中间显示圆形或按钮之类的东西,用户可以单击并弹出以更改关系类型。

到目前为止,我看到基本包装中没有显示圆形的选项。

我正在使用dagre布局,这是我当前的边缘配置:

      {
        selector: "edge",
        style: {
          width: 1,
          "font-size": "20px",
          //opacity: "0.5",
          label: "data(type)",
          color: function(ele) {
            return getEdgeColor(ele.data("type"));
          },

          "line-color": function(ele) {
            return getEdgeColor(ele.data("type"));
          },
          "target-arrow-color": function(ele) {
            return getEdgeColor(ele.data("type"));
          },
          "curve-style": "straight",
          "target-arrow-shape": "triangle"
        }
      },

您能推荐我一些简单的解决方案吗?

1 个答案:

答案 0 :(得分:2)

我对此有一种解决方法(编辑在整个边缘上都有效,圆点仅用于外观:

var cy = window.cy = cytoscape({
  container: document.getElementById('cy'),
  style: [{
      selector: 'node',
      css: {
        'content': 'data(id)',
        'text-valign': 'center',
        'text-halign': 'center',
        'height': '60px',
        'width': '60px'
      }
    },
    {
      selector: 'edge',
      css: {
      'font-size': "40px",
        'label': "\u2022",
        'curve-style': 'bezier',
        'target-arrow-shape': 'data(arrow)'
      }
    }
  ],

  elements: {
    nodes: [{
        data: {
          id: 'n0'
        }
      },
      {
        data: {
          id: 'n1'
        }
      },
      {
        data: {
          id: 'n2'
        }
      },
      {
        data: {
          id: 'n3'
        }
      }
    ],
    edges: [{
        data: {
          source: 'n0',
          target: 'n1',
          arrow: 'triangle'
        }
      },
      {
        data: {
          source: 'n1',
          target: 'n2',
          arrow: 'triangle'
        }
      },
      {
        data: {
          source: 'n1',
          target: 'n3',
          arrow: 'triangle'
        }
      },
    ]
  },

  layout: {
    name: 'concentric',
    minNodeSpacing: 140,
  }
});

cy.cxtmenu({
  selector: 'edge',
  menuRadius: 90,
  commands: [{
    content: 'Direction',
    select: function(edge) {
      edge.move({
        source: edge.target().id(),
        target: edge.source().id()
      });
    }
  }]
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 100%;
  width: 100%;
  left: 0;
  top: 0;
  float: left;
  position: absolute;
}

.cxtmenu-disabled {
  opacity: 0.333;
}
<html>

<head>
  <meta charset=utf-8 />
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
  <script src="https://cdn.jsdelivr.net/npm/cytoscape@3.10.1/dist/cytoscape.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/cytoscape-cxtmenu@3.1.1/cytoscape-cxtmenu.min.js"></script>
</head>

<body>
  <div id="cy"></div>
</body>

</html>

我在这里使用ctx-menu扩展名(右键单击边缘以打开菜单),而点只是Unicode字符。