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

标签 javascript graph-theory cytoscape.js cytoscape-web

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

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

知道怎么做吗? Cytoscape 文档似乎没有答案。

最佳答案

您可以在数据字段中指定节点名称。如果您想将该名称显示为节点标签,只需使用字段 label: "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
  • 关于javascript - 如何使用 Cytoscape.js 使节点的名称与其 ID 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54739085/

    相关文章:

    javascript - 将用户创建的 javascript 保存在数据库中是否安全?

    javascript - 细胞景观 : Graph is not displayed when page is loaded

    javascript - 是否有可能将 react 上下文注入(inject) react 中根外的div(在 body 下方)

    javascript - 复合节点布局问题 - cytoscape js

    algorithm - 如何从图中删除派系无法覆盖的顶点?

    javascript - jQuery img.attr ('width' ) 返回零.. IE7

    javascript - 是否有使用 Google Javascript API 进行 javascript 开发的 IDE?

    javascript - Mercury Editor 0.9 - 内容 iFrame 位于编辑器控件后面,高度错误

    用于删除最少边缘以强制增加未加权无向图中最短路径长度的算法

    algorithm - 寻找有向加权图中的最短顶点序列