编程语言
首页 > 编程语言> > javascript-如何使用Cytoscape.js使节点的名称与其ID不同?

javascript-如何使用Cytoscape.js使节点的名称与其ID不同?

作者:互联网

我正在使用Cytoscape.js构建一个Web应用程序,以可视化蛋白质相互作用数据.

蛋白质(节点)需要具有与代表其染色体位置的字符串相对应的ID,因为这是通用的.但是,我希望将它们可视化或与它们的通用名称(而不是其ID)一起显示.

任何想法如何做到这一点? Cytoscape文档似乎没有答案.

解决方法:

您可以在数据字段中指定节点名称.如果要将该名称用作节点标签,则只需使用字段标签:“ data(name)”:

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

  boxSelectionEnabled: false,
  autounselectify: true,

  style: [{
      selector: "node",
      css: {
        label: "data(name)", //access the nodes data with "data(...)"
        // label: "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",
          name: "Steve"
        }
      },
      {
        data: {
          id: "yes",
          faveColor: "#37a32d",
          name: "Larry"
        }
      },
      {
        data: {
          id: "no",
          faveColor: "#2763c4",
          name: "Kiwi"
        }
      },
      {
        data: {
          id: "Third",
          faveColor: "#2763c4",
          name: "Alex"
        }
      },
      {
        data: {
          id: "Fourth",
          faveColor: "#56a9f7",
          name: "Vader"
        }
      }
    ],
    edges: [{
        data: {
          source: "Top",
          target: "yes"
        }
      },
      {
        data: {
          source: "Top",
          target: "no"
        }
      },
      {
        data: {
          source: "no",
          target: "Third"
        }
      },
      {
        data: {
          source: "Third",
          target: "Fourth"
        }
      },
      {
        data: {
          source: "Fourth",
          target: "Third"
        }
      }
    ]
  },
  layout: {
    name: "dagre"
  }
}));
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>

您也可以在文档中找到它:

> Mapping with data()
> Label data field

标签:graph-theory,cytoscape-web,cytoscape-js,javascript
来源: https://codeday.me/bug/20191108/2006523.html