Layout compound nodes in cytoscape JS

518 views Asked by At

Is it possible to get rid of the marked area? The yellow and green nodes are inner nodes of the grey parent node. I am using a compound node and the cola layout.

compound node

1

There are 1 answers

0
Stephan T. On

You can define a nodes padding by adding the following line to your stylesheet:

selector: "node",
css: {
    ...,
    padding: 0,
    ...
}

var cy = (window.cy = cytoscape({
  container: document.getElementById("cy"),
  style: [{
      selector: "node",
      css: {
        content: "data(id)",
        padding: 0,
        "text-valign": "center",
        "text-halign": "center",
        height: "60px",
        width: "60px"
      }
    },
    {
      selector: ":parent",
      css: {
        shape: "rectangle"
        //shape: 'cutrectangle',
        //shape: 'roundrectangle',
      }
    },
    {
      selector: "edge",
      css: {
        label: "\u2B24",
        "curve-style": "bezier",
        "target-arrow-shape": "data(arrow)"
      }
    },
    {
      selector: ".selectedNode",
      style: {
        "border-width": 8,
        "border-color": "#5da963"
      }
    }
  ],

  elements: {
    nodes: [{
        data: {
          id: "n0",
          parent: "n4"
        }
      },
      {
        data: {
          id: "n1",
          parent: "n5"
        }
      },
      {
        data: {
          id: "n2",
          parent: "n5"
        }
      },
      {
        data: {
          id: "n3",
          parent: "n5"
        }
      },
      {
        data: {
          id: "n4",
          parent: "n5"
        }
      },
      {
        data: {
          id: "n5"
        }
      }
    ],
    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
  }
}));

cy.unbind("click");
cy.bind("click", "nodes", (evt) => {
  cy.elements().removeClass("selectedNode");
  evt.target.addClass("selectedNode");
});

cy.bind("click", "edge", function() {
  console.log("clone:");
  console.log(cy.elements().clone());
  console.log("json:");
  console.log(cy.elements().jsons());
});
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/[email protected]/dist/cytoscape.min.js"></script>
</head>

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

</html>