Cytoscape.js:在多重图形中重叠边缘标签

时间:2018-12-05 11:11:17

标签: cytoscape.js

在Cytoscape.js中有几条关于边或边标签重叠的帖子,但是那里给出的答案,例如标签较小和节点间距较大,对于多图无济于事,即,其中两个节点可以具有多个边的图他们之间。

是否有一种方法可以分散边缘标签,即使它们都属于同一两个节点之间的边缘?边缘对在相同方向和相反方向都出现。

由于我的图形很大,由于性能已经很低,我宁愿使用干草堆边缘的解决方案。

1 个答案:

答案 0 :(得分:0)

好吧,您可以在标签css中使用“ text-rotation”:“ autorotate”来旋转文本。您还可以为每个节点本身定义“ text-margin-x / y”,但是您打算如何定义每个标签的位置?让我们假设您做得很完美:仅移动一个节点后,您仍然需要执行重新定位事件,因为届时偏移将被关闭:

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

  boxSelectionEnabled: false,
  autounselectify: true,

  style: [{
      selector: "node",
      css: {
        content: "data(id)",
        "text-valign": "center",
        "text-halign": "center",
        height: "60px",
        width: "60px",
        "border-color": "black",
        "border-opacity": "1",
        "border-width": "10px"
      }
    },
    {
      selector: "$node > node",
      css: {
        "padding-top": "10px",
        "padding-left": "10px",
        "padding-bottom": "10px",
        "padding-right": "10px",
        "text-valign": "top",
        "text-halign": "center",
        "background-color": "#bbb"
      }
    },
    {
      selector: "edge",
      css: {
        "target-arrow-shape": "triangle"
      }
    },
    {
      selector: "edge[label]",
      css: {
        "label": "data(label)",
        "text-rotation": "autorotate",
        "text-margin-x": "data(xalign)",
        "text-margin-y": "data(yalign)"
      }
    },
    {
      selector: ":selected",
      css: {
        "background-color": "black",
        "line-color": "black",
        "target-arrow-color": "black",
        "source-arrow-color": "black"
      }
    }
  ],
  layout: {
    name: "circle"
  }
}));

var info = [{
    name: "Peter",
    next_op_name: "Claire"
  },
  {
    name: "Claire",
    next_op_name: "Mike"
  },
  {
    name: "Mike",
    next_op_name: "Rosa"
  },
  {
    name: "Rosa",
    next_op_name: "Peter"
  }
];

cy.ready(function() {
  var array = [];
  // iterate over info once
  for (var i = 0; i < info.length; i++) {
    array.push({
      group: "nodes",
      data: {
        id: info[i].name, // id is name!!!
        label: info[i].name
      }
    });
    array.push({
      group: "edges",
      data: {
        id: "e" + i,
        source: info[i].name,
        target: info[i].next_op_name,
        label: "e" + i,
        xalign: (i == 0 || i == 1 ? '15px' : '-15px'),
        yalign: (i == 0 || i == 3 ? '15px' : '-15px')
      }
    });
  }
  cy.add(array);
  cy.layout({
    name: "circle"
  }).run();
});
cy.on("mouseover", "node", function(event) {
  var node = event.target;
  node.qtip({
      content: "hello",
      show: {
        event: event.type,
        ready: true
      },
      hide: {
        event: "mouseout unfocus"
      }
    },
    event
  );
});

cy.on("tapdrag", "node", function(event) {
  // update all relevant labels
  var labels = event.target.connectedEdges();
  for (var i = 0; i < labels.length; i++) {
    // render with the right positions?
  }
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 100%;
  width: 75%;
  position: absolute;
  left: 0;
  top: 0;
  float: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.2.17/cytoscape.min.js"></script>
  <!-- qtip imports -->
  <script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.0/jquery.qtip.min.js"></script>
  <link href="http://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.0/jquery.qtip.min.css" rel="stylesheet" type="text/css" />
  <script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-qtip/2.7.0/cytoscape-qtip.js"></script>

  <!-- dagre imports -->
  <script src="https://unpkg.com/dagre@0.7.4/dist/dagre.js"></script>
  <script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
</head>

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

</html>

问题可以归结为:任务看起来很简单,但是跟踪所有节点和边缘的aaaa以及渲染标签的位置是一个sisyphean任务。