【问题标题】:Change edge color, when clicking node in cytoscape.js单击 cytoscape.js 中的节点时更改边缘颜色
【发布时间】:2019-03-05 05:46:41
【问题描述】:

我想通过 cytoscape.js 在单击 sample.png 中的 nodeC 时将连接节点(nodeA->nodeB 和 nodeB->nodeC)的边缘的颜色更改为红色。请帮帮我。

cy.on("tap", "node", (evt) => {evt.cyTarget.connectedEdges().animate({ style: {lineColor: "red"} }) })`

当我使用这段代码时,它改变了连接到被点击节点的所有边的颜色。

【问题讨论】:

  • 所以你希望进入 C 的边缘被涂成红色,而不是离开 c 的边缘?
  • 对不起,我的意思是进入 C 的边被染成红色。
  • 好的,在你的例子中,只有 B -> C 边缘是红色的?
  • 对不起,如果我错了,因为我第一次使用 cytoscape.js。我想红色 A->B->C
  • 没问题,我现在明白了,请查看我的回答,看看是否对您有帮助:)

标签: javascript graph cytoscape.js


【解决方案1】:

Cytoscape.js 提供了一些简洁的功能来过滤节点:

  • incomers() 让边缘(及其来源)进入集合中的节点
  • predecessors() 递归获取边缘(及其来源)进入集合中的节点(即收入者、收入者的收入者......)
  • edges() 获取图中与指定选择器匹配的边

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: "100px",
        shape: "rectangle",
        "background-color": "data(faveColor)"
      }
    },
    {
      selector: "edge",
      css: {
        "curve-style": "bezier",
        "control-point-step-size": 40,
        "target-arrow-shape": "triangle"
      }
    }
  ],

  elements: {
    nodes: [{
        data: {
          id: "Top",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "yes",
          faveColor: "#37a32d"
        }
      },
      {
        data: {
          id: "no",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "Third",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "Fourth",
          faveColor: "#56a9f7"
        }
      }
    ],
    edges: [{
        data: {
          source: "Top",
          target: "yes"
        }
      },
      {
        data: {
          source: "Top",
          target: "no"
        }
      },
      {
        data: {
          source: "no",
          target: "Third"
        }
      },
      {
        data: {
          source: "Third",
          target: "Fourth"
        }
      }
    ]
  },
  layout: {
    name: "dagre"
  }
}));

cy.unbind('click');
cy.bind('click', 'node', function(node) {
  console.log(node.target.predecessors().edges());
  node.target.predecessors().edges().animate({
    style: {
      lineColor: "red"
    }
  });
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 100%;
  width: 100%;
  left: 0;
  top: 0;
  float: left;
  position: absolute;
}
<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://unpkg.com/cytoscape@3.3.0/dist/cytoscape.min.js">
  </script>
  <!-- cyposcape dagre -->
  <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>

【讨论】:

    猜你喜欢
    • 2018-04-02
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 2015-04-16
    • 1970-01-01
    • 2015-11-13
    相关资源
    最近更新 更多