reactjscytoscape.js

How to add nodes of different shapes in react-cytoscape?


I am trying to have different nodes of different shapes based on the classes attribute

This is the elements:

const elements = [
    { data: { id: "p1", label: "Node 1", classes: "place" } },
    { data: { id: "t1", label: "Transition", classes: "transitions" } },
    { data: { id: "t2", label: "Transition2", classes: "transitions" } },
    { data: { id: "p2", label: "Place 2", classes: "place" } },
    { data: { id: "p3", label: "Place 3", classes: "place" } },
    { data: { source: "p1", target: "t1" } },
    { data: { source: "t1", target: "p2" } },
    { data: { source: "t1", target: "p3" } },
    { data: { source: "p2", target: "t2" } },
    { data: { source: "p3", target: "t2" } },
  ];

and this is my styles:

const cyStyle = [
    {
      selector: ".place", 
      style: {
        "background-color": "blue",
        shape: "ellipse",
      },
    },
    {
      selector: ".transitions",
      style: {
        "background-color": "red",
        shape: "rectangle",
      },
    },
    {
      selector: "edge",
      style: {
        width: 3,
        "line-color": "#ccc",
        "target-arrow-color": "#ccc",
        "target-arrow-shape": "triangle",
        "curve-style": "bezier",
      },
    },
  ];

The styles to the edges are applied, and if I add a selector: "nodes" it gets reflected on the nodes, however I want the two different classes of nodes that I have to appear in different shapes. Is there a way to achieve this?


Solution

  • You should write `classes` outside the `data` object.
    

    var cy = window.cy = cytoscape({
      container: document.getElementById('cy'),
      layout: {name: 'grid', rows: 2},
      style: [{
          selector: '.shape1',
          css: {
            'shape': 'ellipse'
          }
        },
        {
          selector: '.shape2',
          css: {
            'shape': 'triangle'
          }
        }    
      ],
      elements: {
        nodes: [{
            data: {
              id: 'n0'        
            },
            classes: 'shape1'
          },
          {
            data: {
              id: 'n1'
            },
            classes: 'shape1'
          },
          {
            data: {
              id: 'n2'
            },
            classes: 'shape2'
          },
          {
            data: {
              id: 'n3'
            },
            classes: 'shape2'
          }
        ],
        edges: [{
            data: {
              id: 'n0n1',
              source: 'n0',
              target: 'n1',
              weight: 3
            }
          },
          {
            data: {
              id: 'n1n2',        
              source: 'n1',
              target: 'n2',
              weight: 5
            }
          },
          {
            data: {
              id: 'n2n3',        
              source: 'n2',
              target: 'n3',
              weight: 7
            }
          }
        ]
      }
    });
    body {
      font: 14px helvetica neue, helvetica, arial, sans-serif;
    }
    
    #button {
      z-index = 1000;
    }
    
    #cy {
      height: 95%;
      width: 95%;
      left: 0;
      top: 50;
      z-index = 900;
      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.10.0/dist/cytoscape.min.js">
      </script>
    </head>
    
    <body>
      <div id="cy"></div>
    </body>
    
    </html>