Добавьте специфицированный c цвет для некоторых дочерних узлов в Cytoscape. js - PullRequest
0 голосов
/ 29 апреля 2020

У меня есть несколько узлов, которые являются детьми родителей (группа узлов). Можно ли переопределить цвет фона для некоторых детей?

Я мог только переопределить цвет нормальных узлов следующим образом:

cy.nodes('[id = "start"]').style('background-color', '#FBFBFB'); 

Заранее спасибо.

[
  {
    "style": [
      {
        "selector": "node",
        "css": {
          "shape": "roundrectangle",
          "height": "40px",
          "background-color": "#58D68D",
          "label": "data(id)",
          "text-valign": "center",
          "border-width": "2",
          "border-color": "black"
        }
      },
      {
        "selector": ":parent",
        "css": {
          "background-opacity": "0.333",
          "text-halign": "center",
          "text-valign": "top"
        }
      },
]
]
}

1 Ответ

0 голосов
/ 01 мая 2020

Вы всегда можете просто указать цвет узлов при их инициализации:

var cy = (window.cy = cytoscape({
  container: document.getElementById("cy"),
  style: [{
      selector: "node",
      css: {
        "shape": "roundrectangle",
        "height": "40px",
        "background-color": function(node) {
          if (node.data("colored"))
            return "#FBFBFB";
          else
            return "#58D68D";
        },
        "label": "data(id)",
        "text-valign": "center",
        "border-width": "2",
        "border-color": "black"
      }
    },
    {
      selector: ':parent',
      css: {
        "background-opacity": "0.333",
        "text-halign": "center",
        "text-valign": "top"
      }
    },
    {
      selector: "edge",
      css: {
        label: "\u2B24",
        "curve-style": "bezier",
        "target-arrow-shape": "data(arrow)"
      }
    }
  ],

  elements: {
    nodes: [{
        data: {
          id: "n0",
          colored: true,
          parent: "n4"
        }
      },
      {
        data: {
          id: "n1",
          colored: false,
          parent: "n5"
        }
      },
      {
        data: {
          id: "n2",
          colored: true,
          parent: "n5"
        }
      },
      {
        data: {
          id: "n3",
          colored: true,
          parent: "n5"
        }
      },
      {
        data: {
          id: "n4",
          colored: false,
          parent: "n5"
        }
      },
      {
        data: {
          id: "n5",
          colored: false,
        }
      }
    ],
    edges: [{
        data: {
          source: "n0",
          target: "n1",
          arrow: "triangle"
        }
      },
      {
        data: {
          source: "n1",
          target: "n2",
          arrow: "triangle"
        }
      },
      {
        data: {
          source: "n1",
          target: "n3",
          arrow: "triangle"
        }
      }
    ]
  },

  layout: {
    name: "concentric",
    minNodeSpacing: 140
  }
}));
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 100%;
  width: 100%;
  left: 0;
  top: 0;
  float: left;
  position: absolute;
}

.cxtmenu-disabled {
  opacity: 0.333;
}
<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://cdn.jsdelivr.net/npm/cytoscape@3.10.1/dist/cytoscape.min.js"></script>
</head>

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

</html>
...