javascript - 叶节点 cytoscape 的不同行为

标签 javascript html tree cytoscape.js

What the output should look like 我需要使用 cytoscape.js 为我的学士论文创建特定的树(系统发育树),对于这些树,将内部节点的颜色设置为与子节点不同的颜色(或叶节点 = 没有任何后代的节点,参见图片)是有益的。我是 html 的新手,所以在回答时请记住这一点。我查看了Cytoscape的手册,但没有找到解决方案。 使用选择节点

cy.nodes(':child').not(':parent');

不知何故不起作用。非常感谢您的回答!

编辑:它现在在 klay 中工作,但可视化并不完美,我想要得到像图片中那样的东西。

最佳答案

您可以只使用内置函数 cy.nodes().leaves().addClass(...) ,其中 ... 是样式表中定义所需 CSS 的类。

编辑

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

    // demo your layout
    layout: {
      name: "klay"

      // some more options here...
    },

    style: [{
        selector: "node",
        style: {
          "background-color": "#dd4de2"
        }
      },
      {
        selector: ".leaf",
        style: {
          "background-color": "#000"
        }
      },
      {
        selector: "edge",
        style: {
          "curve-style": "bezier",
          "target-arrow-shape": "triangle",
          "line-color": "#dd4de2",
          "target-arrow-color": "#dd4de2",
          opacity: 0.5
        }
      }
    ],
    elements: {
      nodes: [{
          data: {
            id: "n0"
          }
        },
        {
          data: {
            id: "n1"
          }
        },
        {
          data: {
            id: "n2"
          }
        },
        {
          data: {
            id: "n3"
          }
        },
        {
          data: {
            id: "n4"
          }
        },
        {
          data: {
            id: "n5"
          }
        },
        {
          data: {
            id: "n6"
          }
        },
        {
          data: {
            id: "n7"
          }
        },
        {
          data: {
            id: "n8"
          }
        },
        {
          data: {
            id: "n9"
          }
        },
        {
          data: {
            id: "n10"
          }
        },
        {
          data: {
            id: "n11"
          }
        },
        {
          data: {
            id: "n12"
          }
        },
        {
          data: {
            id: "n13"
          }
        },
        {
          data: {
            id: "n14"
          }
        },
        {
          data: {
            id: "n15"
          }
        }
      ],
      edges: [{
          data: {
            source: "n0",
            target: "n1"
          }
        },
        {
          data: {
            source: "n1",
            target: "n2"
          }
        },
        {
          data: {
            source: "n1",
            target: "n3"
          }
        },
        {
          data: {
            source: "n2",
            target: "n4"
          }
        },
        {
          data: {
            source: "n4",
            target: "n5"
          }
        },
        {
          data: {
            source: "n4",
            target: "n6"
          }
        },
        {
          data: {
            source: "n6",
            target: "n7"
          }
        },
        {
          data: {
            source: "n6",
            target: "n8"
          }
        },
        {
          data: {
            source: "n8",
            target: "n9"
          }
        },
        {
          data: {
            source: "n8",
            target: "n10"
          }
        },
        {
          data: {
            source: "n10",
            target: "n11"
          }
        },
        {
          data: {
            source: "n11",
            target: "n12"
          }
        },
        {
          data: {
            source: "n12",
            target: "n13"
          }
        },
        {
          data: {
            source: "n13",
            target: "n14"
          }
        },
        {
          data: {
            source: "n13",
            target: "n15"
          }
        }
      ]
    }
  }));

  cy.nodes().leaves().addClass("leaf");
});
body {
  font-family: helvetica neue, helvetica, liberation sans, arial, sans-serif;
  font-size: 14px;
}

#cy {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  z-index: 999;
}
<html>

<head>
  <script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
  <script src="https://unpkg.com/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddb6b1bca4b7ae9dedf3e9f3ec" rel="noreferrer noopener nofollow">[email protected]</a>/klay.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64071d100b1707051401490f08051d24574a554a57" rel="noreferrer noopener nofollow">[email protected]</a>/cytoscape-klay.min.js"></script>
</head>

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

</html>

关于javascript - 叶节点 cytoscape 的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62608477/

相关文章:

php - 在未初始化的变量上使用 .= 运算符有多安全?

html - 如何在css中将一个div分成多行和多列?

sql - 如何查询数据库字段但忽略 HTML 标记?

c++ - 如何在 C++ 中释放包含指向这些结构的指针数组的结构?

algorithm - 我们可以有一个没有任何红色节点的红黑树吗?

javascript - RequireJS - 自己的模块,有两种加载方式

c# - Ajax 发布到 ASP.net MVC Controller - 对象属性为空

javascript - react native 导航 v2 : Get current route

javascript - 使用 JavaScript 阅读 KMZ

sql - 使用树顺序从表中选择行