在cytoscape.js中单击节点时更改边缘颜色

问题描述 投票:1回答:1

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

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

当我使用此代码时,它更改了连接到单击节点的所有边的颜色。

javascript graph cytoscape.js
1个回答
3
投票

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/[email protected]/dist/cytoscape.min.js">
  </script>
  <!-- cyposcape dagre -->
  <script src="https://unpkg.com/[email protected]/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>
© www.soinside.com 2019 - 2024. All rights reserved.